voidsolve(){ int N, Q; cin >> N >> Q; vc<array<int, 26>> A(N+1); vc<array<int, 26>> B(N+1); string a, b; cin >> a >> b; rep(i, N) { A[i + 1] = A[i]; A[i+1][a[i]-'a']++; B[i+1] = B[i]; B[i+1][b[i]-'a']++; }
rep(Q){ int l, r; cin >> l >> r; --l; int ans = 0; rep(j, 26) { int aa = A[r][j] - A[l][j]; int bb = B[r][j] - B[l][j]; ans += abs(aa - bb); } cout << ans / 2 << "\n"; } }
D.
考虑到可以先放缩。
弱化条件,$ab \leq N, a \leq X$,这样我们可以确定,枚举 $(a,b)$ 的复杂度不会超过调和级数 $O(N \log N)$,然后 $c$ 必然是连续的一段,直接计算即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
voidsolve(){ int N, X; cin >> N >> X;
ll ans = 0; for (int a = 1; a <= X; a++) { for (int b = 1; b <= N / a; b++) { if (a + b >= X) { break; } int rem = N - a * b; int c = min(X - a - b, rem / (a + b)); ans += c; } } cout << ans << "\n"; }
voidsolve(){ ll N, K; cin >> N >> K; vl A(N), B(N); rep(N) cin >> A[i]; rep(N) cin >> B[i];
auto check = [&](int x) { ll cur = 0; rep(i, N) { if (A[i] >= x) cur += (A[i] - x) / B[i] + 1; } return cur; };
ll l = 0, r = INF + 2; while (r - l > 1) { int m = (l + r) / 2; if (check(m) >= K) { l = m; } else { r = m; } } ll cur = check(l);
ll ans = 0; rep(i, N) { if (A[i] < l) continue; int c = (A[i] - l) / B[i]; int aa = A[i] - c * B[i]; int bb = A[i]; ll t = 1ll * (aa + bb) * (c + 1) / 2; ans += t; } if (cur >= K) { ans -= l * (cur - K); }
voidsolve(){ int N, M; cin >> N >> M; vc<vc<pii>> g(N * 2); rep(i, M) { int a, b; cin >> a >> b; --a; --b; g[b].pb(a, b); g[a + N].pb(b, a); g[a + N].pb(b, a + N); g[b + N].pb(a + N, b); g[b + N].pb(a + N, b + N); } build(0, N * 2, 1); int ans = N; rep(cur, N * 2) { for (auto [i, j] : g[cur]) { if (i < j) { upd(i + 1, j, 0, N * 2, 1, 1); } else { upd(j + 1, i, 0, N * 2, 1, -1); } } if (cur >= N - 1) { auto [x, y] = query(0, cur, 0, N * 2, 1); if (x > 0) { y = 0; } cmin(ans, cur + 1 - y); } }