#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=5e5;
int n, q, ans[mxN];
string s;
ar<int, 2> st[1<<20];
vector<ar<int, 2>> queries[mxN];
vector<int> stk;
ar<int, 2> cmb(ar<int, 2> a, ar<int, 2> b) {
return {a[0]+b[0], min(a[1], a[0]+b[1])};
}
void upd(int i, int x, int u=1, int l=0, int r=n-1) {
if (l==r) {
st[u][0]=x;
st[u][1]=min(0, st[u][0]);
return;
}
int mid=(l+r)/2;
i<=mid?upd(i, x, 2*u, l, mid):upd(i, x, 2*u+1, mid+1, r);
st[u]=cmb(st[2*u], st[2*u+1]);
}
ar<int, 2> qry(int ql, int qr, int u=1, int l=0, int r=n-1) {
if (l>qr||r<ql)
return {};
if (ql<=l&&r<=qr)
return st[u];
int mid=(l+r)/2;
return cmb(qry(ql, qr, 2*u, l, mid), qry(ql, qr, 2*u+1, mid+1, r));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> q;
for (int i=0; i<q; ++i) {
int l, r;
cin >> l >> r, --l, --r;
queries[r].push_back({l, i});
}
for (int i=0; i<n; ++i) {
if (s[i]=='C') {
upd(i, 1);
if (stk.size()) {
upd(stk.back(), -1);
stk.pop_back();
}
} else {
upd(i, 0);
stk.push_back(i);
}
for (auto [l, ind] : queries[i]) {
int x=stk.end()-lower_bound(stk.begin(), stk.end(), l);
ans[ind]=x-qry(l, i)[1];
}
}
for (int i=0; i<q; ++i)
cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12116 KB |
Output is correct |
3 |
Correct |
8 ms |
12092 KB |
Output is correct |
4 |
Correct |
7 ms |
12092 KB |
Output is correct |
5 |
Correct |
8 ms |
12092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12116 KB |
Output is correct |
3 |
Correct |
8 ms |
12092 KB |
Output is correct |
4 |
Correct |
7 ms |
12092 KB |
Output is correct |
5 |
Correct |
8 ms |
12092 KB |
Output is correct |
6 |
Correct |
61 ms |
16948 KB |
Output is correct |
7 |
Correct |
58 ms |
16600 KB |
Output is correct |
8 |
Correct |
56 ms |
16660 KB |
Output is correct |
9 |
Correct |
58 ms |
16860 KB |
Output is correct |
10 |
Correct |
82 ms |
16852 KB |
Output is correct |
11 |
Correct |
67 ms |
17088 KB |
Output is correct |
12 |
Correct |
62 ms |
17040 KB |
Output is correct |
13 |
Correct |
63 ms |
17192 KB |
Output is correct |
14 |
Correct |
69 ms |
16976 KB |
Output is correct |
15 |
Correct |
58 ms |
16952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12116 KB |
Output is correct |
3 |
Correct |
8 ms |
12092 KB |
Output is correct |
4 |
Correct |
7 ms |
12092 KB |
Output is correct |
5 |
Correct |
8 ms |
12092 KB |
Output is correct |
6 |
Correct |
61 ms |
16948 KB |
Output is correct |
7 |
Correct |
58 ms |
16600 KB |
Output is correct |
8 |
Correct |
56 ms |
16660 KB |
Output is correct |
9 |
Correct |
58 ms |
16860 KB |
Output is correct |
10 |
Correct |
82 ms |
16852 KB |
Output is correct |
11 |
Correct |
67 ms |
17088 KB |
Output is correct |
12 |
Correct |
62 ms |
17040 KB |
Output is correct |
13 |
Correct |
63 ms |
17192 KB |
Output is correct |
14 |
Correct |
69 ms |
16976 KB |
Output is correct |
15 |
Correct |
58 ms |
16952 KB |
Output is correct |
16 |
Correct |
528 ms |
41760 KB |
Output is correct |
17 |
Correct |
403 ms |
38856 KB |
Output is correct |
18 |
Correct |
458 ms |
40116 KB |
Output is correct |
19 |
Correct |
441 ms |
41420 KB |
Output is correct |
20 |
Correct |
502 ms |
40792 KB |
Output is correct |
21 |
Correct |
561 ms |
43160 KB |
Output is correct |
22 |
Correct |
516 ms |
42720 KB |
Output is correct |
23 |
Correct |
538 ms |
43660 KB |
Output is correct |
24 |
Correct |
516 ms |
42904 KB |
Output is correct |
25 |
Correct |
498 ms |
41976 KB |
Output is correct |