Submission #378760

#TimeUsernameProblemLanguageResultExecution timeMemory
378760thecodingwizardElection (BOI18_election)C++11
0 / 100
21 ms492 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ii pair<int, int> #define f first #define s second #define pb push_back #define mp make_pair #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() #define F0R(i, n) for (int i = 0; i < n; i++) #define FOR(i, a, b) for (int i = a; i < b; i++) #define inf 1000000010 #define maxn 500010 int ans[maxn]; int tPS[maxn]; struct BIT { int n; vector<int> A; void init(int _n) { n = _n; A.assign(n+10, 0); } int qry(int x) { int s = 0; while (x) { s += A[x]; x -= x&-x; } return s; } void upd(int k, int v) { while (k <= n) { A[k] += v; k += k&-k; } } } matched; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; string s; cin >> s; s = " " + s; for (int i = 1; i <= n; i++) { tPS[i] = tPS[i-1] + (s[i] == 'T'); } matched.init(n); int q; cin >> q; vector<pair<ii, int>> queries; F0R(i, q) { int l, r; cin >> l >> r; queries.pb(mp(mp(l, r), i)); } sort(all(queries), [](pair<ii, int> a, pair<ii, int> b) { return a.f.s < b.f.s; }); int idx = 0; stack<int> stuff; int tCtr = 0, maxT = 0; for (int i = 1; i <= n; i++) { if (s[i] == 'C') { stuff.push(i); tCtr = max(tCtr-1, 0); } else { if (!stuff.empty()) { matched.upd(stuff.top(), 1); stuff.pop(); tCtr++; maxT = max(maxT, tCtr + (int)stuff.size()); } } while (idx < q && queries[idx].f.s == i) { // process query idx stack<int> stuff; int tCtr = 0, maxT = 0; int wtmoo = 0; for (int i = 1; i <= queries[idx].f.s; i++) { if (i == queries[idx].f.f) { wtmoo = tCtr + (int)stuff.size(); } if (s[i] == 'C') { stuff.push(i); tCtr = max(tCtr-1, 0); } else { if (!stuff.empty()) { stuff.pop(); tCtr++; maxT = max(maxT, tCtr + (int)stuff.size()); } } } int subtract = max(0, maxT - (int)stuff.size() - wtmoo); ans[queries[idx].s] = matched.qry(n) - matched.qry(queries[idx].f.f-1); ans[queries[idx].s] -= subtract; ans[queries[idx].s] = tPS[queries[idx].f.s] - tPS[queries[idx].f.f-1] - ans[queries[idx].s]; idx++; } } F0R(i, q) cout << ans[i] << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...