#include "bits/stdc++.h"
using namespace std;
const int N=5e5;
int n, q;
string s;
struct Node{
int pre, suf, sum, ans;
Node operator+(Node b){
Node ret;
ret.pre=max(pre, sum+b.pre);
ret.suf=max(b.suf, b.sum+suf);
ret.sum=sum+b.sum;
ret.ans=max({ans+b.sum, b.ans+sum, pre+b.suf});
return ret;
}
} seg[4*N];
Node qry(int l, int r, int x=0, int lx=0, int rx=n){
if(lx>=l && rx<=r) return seg[x];
if(lx>=r || rx<=l) return {0, 0, 0, 0};
int m=(lx+rx)/2;
return qry(l, r, 2*x+1, lx, m)+qry(l, r, 2*x+2, m, rx);
}
void build(int x=0, int lx=0, int rx=n){
if(rx-lx==1){
if(s[lx]=='C') seg[x]={0, 0, -1, 0};
else seg[x]={1, 1, 1, 1};
return;
}
int m=(lx+rx)/2;
build(2*x+1, lx, m);
build(2*x+2, m, rx);
seg[x]=seg[2*x+1]+seg[2*x+2];
// cout << "seg[" << lx << " " << rx << "] " << seg[x].pre << " " << seg[x].suf << " " << seg[x].sum << " " << seg[x].ans << "\n";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
build();
cin >> q;
while(q--){
int l,r; cin >> l >> r;
cout << qry(l-1,r).ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
48 ms |
5808 KB |
Output is correct |
7 |
Correct |
45 ms |
5712 KB |
Output is correct |
8 |
Correct |
47 ms |
5724 KB |
Output is correct |
9 |
Correct |
41 ms |
5728 KB |
Output is correct |
10 |
Correct |
45 ms |
5692 KB |
Output is correct |
11 |
Correct |
45 ms |
5888 KB |
Output is correct |
12 |
Correct |
47 ms |
5764 KB |
Output is correct |
13 |
Correct |
48 ms |
5844 KB |
Output is correct |
14 |
Correct |
45 ms |
5744 KB |
Output is correct |
15 |
Correct |
47 ms |
5736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
48 ms |
5808 KB |
Output is correct |
7 |
Correct |
45 ms |
5712 KB |
Output is correct |
8 |
Correct |
47 ms |
5724 KB |
Output is correct |
9 |
Correct |
41 ms |
5728 KB |
Output is correct |
10 |
Correct |
45 ms |
5692 KB |
Output is correct |
11 |
Correct |
45 ms |
5888 KB |
Output is correct |
12 |
Correct |
47 ms |
5764 KB |
Output is correct |
13 |
Correct |
48 ms |
5844 KB |
Output is correct |
14 |
Correct |
45 ms |
5744 KB |
Output is correct |
15 |
Correct |
47 ms |
5736 KB |
Output is correct |
16 |
Correct |
436 ms |
27112 KB |
Output is correct |
17 |
Correct |
386 ms |
26532 KB |
Output is correct |
18 |
Correct |
403 ms |
26912 KB |
Output is correct |
19 |
Correct |
403 ms |
26276 KB |
Output is correct |
20 |
Correct |
437 ms |
26096 KB |
Output is correct |
21 |
Correct |
441 ms |
27852 KB |
Output is correct |
22 |
Correct |
432 ms |
27720 KB |
Output is correct |
23 |
Correct |
459 ms |
28028 KB |
Output is correct |
24 |
Correct |
460 ms |
27584 KB |
Output is correct |
25 |
Correct |
460 ms |
27176 KB |
Output is correct |