#include <bits/stdc++.h>
using namespace std;
string s;
const int mxN = 5e5;
int n;
struct node {
int l, r, s, a;
};
node segtree[4*mxN+1];
void comb(node &a, node &b, node &c){
c.a = max(a.l+b.r, max(a.s+b.a, a.a + b.s));
c.l = max(a.l,a.s+b.l);
c.r = max(a.r+b.s,b.r);
c.s = a.s+b.s;
}
void build(int l = 0, int r = n-1, int idx = 0){
if (l == r){
if (s[l] == 'C'){
segtree[idx].s = -1;
} else {
segtree[idx].s = 1;
}
segtree[idx].l = max(0,segtree[idx].s);
segtree[idx].r = max(0,segtree[idx].s);
segtree[idx].a = 0;
if (segtree[idx].s > 0) segtree[idx].a = 1;
return;
}
int m = (l+r)/2;
build(l,m,2*idx+1);
build(m+1,r,2*idx+2);
comb(segtree[2*idx+1],segtree[2*idx+2],segtree[idx]);
segtree[idx].a = max(segtree[idx].a,0);
//cout << l << "," << r << ": " << segtree[idx].l << " " << segtree[idx].r << " " << segtree[idx].s << " " << segtree[idx].a << '\n';
}
void query(int tl, int tr, node &res, int l = 0, int r = n-1, int idx = 0){
if (l > tr || r < tl){
return;
}
if (l >= tl && r <= tr){
comb(res,segtree[idx],res);
return;
}
int m = (l+r)/2;
query(tl,tr,res,l,m,2*idx+1);
query(tl,tr,res,m+1,r,2*idx+2);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin >> n >> s >> q;
build();
int l,r;
node res;
for (int i =0; i < q; i++){
cin >> l >> r;
l -= 1;
r -= 1;
res.l = 0;
res.r = 0;
res.s = 0;
res.a = 0;
query(l,r,res);
cout << res.a << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
388 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
388 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
34 ms |
6108 KB |
Output is correct |
7 |
Correct |
32 ms |
5980 KB |
Output is correct |
8 |
Correct |
31 ms |
5968 KB |
Output is correct |
9 |
Correct |
28 ms |
5972 KB |
Output is correct |
10 |
Correct |
33 ms |
5976 KB |
Output is correct |
11 |
Correct |
33 ms |
5948 KB |
Output is correct |
12 |
Correct |
33 ms |
6028 KB |
Output is correct |
13 |
Correct |
33 ms |
6196 KB |
Output is correct |
14 |
Correct |
33 ms |
5972 KB |
Output is correct |
15 |
Correct |
34 ms |
5976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
388 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
34 ms |
6108 KB |
Output is correct |
7 |
Correct |
32 ms |
5980 KB |
Output is correct |
8 |
Correct |
31 ms |
5968 KB |
Output is correct |
9 |
Correct |
28 ms |
5972 KB |
Output is correct |
10 |
Correct |
33 ms |
5976 KB |
Output is correct |
11 |
Correct |
33 ms |
5948 KB |
Output is correct |
12 |
Correct |
33 ms |
6028 KB |
Output is correct |
13 |
Correct |
33 ms |
6196 KB |
Output is correct |
14 |
Correct |
33 ms |
5972 KB |
Output is correct |
15 |
Correct |
34 ms |
5976 KB |
Output is correct |
16 |
Correct |
307 ms |
27108 KB |
Output is correct |
17 |
Correct |
268 ms |
27032 KB |
Output is correct |
18 |
Correct |
300 ms |
27160 KB |
Output is correct |
19 |
Correct |
254 ms |
26748 KB |
Output is correct |
20 |
Correct |
294 ms |
26364 KB |
Output is correct |
21 |
Correct |
317 ms |
28072 KB |
Output is correct |
22 |
Correct |
301 ms |
27956 KB |
Output is correct |
23 |
Correct |
301 ms |
28132 KB |
Output is correct |
24 |
Correct |
319 ms |
27948 KB |
Output is correct |
25 |
Correct |
300 ms |
27432 KB |
Output is correct |