voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int n; cin >> n; string str; str.reserve(n); cin >> str; map<char, int> cnt; for (int i = 0; i < n; ++i) ++cnt[str[i]]; int ans = 0; for (int i = n - 1; i >= 0; --i) { ans += static_cast<longlong>(cnt.size()); if (constauto iter = cnt.find(str[i]); iter->second == 1) cnt.erase(iter); else --iter->second; } cout << ans << endl; } }
voidsolve(){ int _; cin >> _; for (int tc = 0; tc < _; ++tc) { int n, k, d; cin >> n >> k >> d; vector<int> data(n), v(k); for (auto& i: data) cin >> i; for (auto& i: v) cin >> i; // init ans int ans = 0; for (int i = 0; i < n; ++i) if (data[i] == i + 1) ++ans; ans += (d - 1) / 2; for (int i = 0; i < min(2 * n, d - 1); ++i) { int tmp = 0; for (int j = 0; j < n; ++j) { if (j < v[i % k]) ++data[j]; if (data[j] == j + 1) ++tmp; } tmp += (d - 2 - i) / 2; ans = max(ans, tmp); } cout << ans << endl; } }
voidsolve(){ int _; cin >> _; vector<vector<int>> tree(2e5 * 2), next(2e5 * 2); vector<int> end(2e5 * 2); for (auto& i: tree) i.resize(20, 0); for (auto& i: next) i.resize(2, 0);
for (int tc = 0; tc < _; ++tc) { int n, k; cin >> n >> k; vector<int> p(n), q(k), cache(k); for (auto& i: p) cin >> i; for (auto& i: q) cin >> i;
int root, tail = 0; auto build = [&] { for (int i = 0; i < 20; ++i) tree[tail][i] = 0; next[tail][0] = next[tail][1] = -1; end[tail] = 0; assert(tail < tree.size()); return tail++; };
root = build();
auto move = [&](constint x, constint v) { int i = 20; while (i >= 0 && (x & 1 << i) == 0) --i; int cur = root; while (i >= 0) { constint flag = x & 1 << i ? 1 : 0; if (next[cur][flag] == -1) next[cur][flag] = build(); cur = next[cur][flag]; tree[cur][i] += v; --i; } end[cur] += v; };
for (constauto& pi: p) move(pi, 1);
ll ans = 0; constexpr ll mod = 998244353; auto pos = [&](constint gap, constint cnt) { if (gap >= k) return; const ll ps = 1ll * (k - gap + 1) * (k - gap) / 2 % mod; const ll tmp = ps * cnt % mod; ans = (ans + tmp) % mod; };
for (constauto& pi: p) { move(pi, -1); int i = 20; while (i >= 0 && (pi & 1 << i) == 0) --i; int cur = root; while (i >= 0) { constint flag = pi & 1 << i ? 1 : 0;
if (constint other = next[cur][flag ^ 1]; other != -1) for (int ind = 0; ind < 20; ++ind) if (tree[other][ind] > 0) { constint gap = ind + (flag ^ 1) - i; pos(max(0, gap), tree[other][ind]); if (gap < 0) neg(-gap, tree[other][ind]); }
cur = next[cur][flag]; constint gap = max(-i, -k + 1); pos(max(0, gap), end[cur]); if (gap < 0) neg(-gap, end[cur]); --i; }
auto cal = [&](constint node) { if (node == -1) return; for (int ind = 0; ind < 20; ++ind) if (tree[node][ind] > 0) { constint gap = ind + 2; pos(max(0, gap), tree[node][ind]); if (gap < 0) neg(-gap, tree[node][ind]); } }; cal(next[cur][0]); cal(next[cur][1]); }