#include <bits/stdc++.h>
using namespace std;
#define int long long
#define OYY LLONG_MAX
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR for(int i=1;i<=n;i++)
#define mid (start+end)/2
#define lim 500005
#define fi first
#define se second
array<int,4> bos;
int arr[lim];
int sum[4*lim],ans[4*lim],le[4*lim],ri[4*lim];
inline void build(int node,int start,int end){
if(start==end){
sum[node]=le[node]=ri[node]=ans[node]=arr[start];
le[node]=ri[node]=max(le[node],0ll);
ans[node]=max(ans[node],0ll);
return ;
}
build(node*2,start,mid),build(node*2+1,mid+1,end);
sum[node]=sum[node*2]+sum[node*2+1];
le[node]=max({le[node*2],sum[node*2]+le[node*2+1],0ll});
ri[node]=max({ri[node*2+1],sum[node*2+1]+ri[node*2],0ll});
ans[node]=max({ans[node*2]+sum[node*2+1],ans[node*2+1]+sum[node*2],le[node*2]+ri[node*2+1],0ll});
}
inline array<int,4> query(int node,int start,int end,int l,int r){
if(start>end || start>r || end<l)return bos;
if(start>=l && end<=r){
array<int,4> tut;
tut[0]=sum[node];
tut[1]=le[node];
tut[2]=ri[node];
tut[3]=ans[node];
return tut;
}
array<int,4> tut;
array<int,4> left=query(node*2,start,mid,l,r);
array<int,4> right=query(node*2+1,mid+1,end,l,r);
tut[0]=left[0]+right[0];
tut[1]=max({left[1],left[0]+right[1],0ll});
tut[2]=max({right[2],right[0]+left[2],0ll});
tut[3]=max({left[3]+right[0],right[3]+left[0],left[1]+right[2],0ll});
return tut;
}
inline void debug(int node,int start,int end){
cout<<node<<" "<<start<<" "<<end<<" "<<sum[node]<<" "<<le[node]<<" "<<ri[node]<<" "<<ans[node]<<endl;
if(start==end)return ;
debug(node*2,start,mid),debug(node*2+1,mid+1,end);
}
int32_t main(){
faster
int n;cin>>n;
string s;cin>>s;
FOR{
if(s[i-1]=='C')arr[i]=-1;
else arr[i]=1;
}
build(1,1,n);
//debug(1,1,n);
int q;cin>>q;
while(q--){
int x,y;cin>>x>>y;
array<int,4> tut=query(1,1,n,x,y);
cout<<tut[3]<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
52 ms |
10596 KB |
Output is correct |
7 |
Correct |
46 ms |
10332 KB |
Output is correct |
8 |
Correct |
48 ms |
10324 KB |
Output is correct |
9 |
Correct |
44 ms |
10356 KB |
Output is correct |
10 |
Correct |
48 ms |
10320 KB |
Output is correct |
11 |
Correct |
55 ms |
10576 KB |
Output is correct |
12 |
Correct |
53 ms |
10580 KB |
Output is correct |
13 |
Correct |
55 ms |
10636 KB |
Output is correct |
14 |
Correct |
47 ms |
10580 KB |
Output is correct |
15 |
Correct |
46 ms |
10580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
52 ms |
10596 KB |
Output is correct |
7 |
Correct |
46 ms |
10332 KB |
Output is correct |
8 |
Correct |
48 ms |
10324 KB |
Output is correct |
9 |
Correct |
44 ms |
10356 KB |
Output is correct |
10 |
Correct |
48 ms |
10320 KB |
Output is correct |
11 |
Correct |
55 ms |
10576 KB |
Output is correct |
12 |
Correct |
53 ms |
10580 KB |
Output is correct |
13 |
Correct |
55 ms |
10636 KB |
Output is correct |
14 |
Correct |
47 ms |
10580 KB |
Output is correct |
15 |
Correct |
46 ms |
10580 KB |
Output is correct |
16 |
Correct |
470 ms |
47336 KB |
Output is correct |
17 |
Correct |
391 ms |
46964 KB |
Output is correct |
18 |
Correct |
441 ms |
47136 KB |
Output is correct |
19 |
Correct |
355 ms |
46560 KB |
Output is correct |
20 |
Correct |
473 ms |
46564 KB |
Output is correct |
21 |
Correct |
467 ms |
48352 KB |
Output is correct |
22 |
Correct |
474 ms |
48140 KB |
Output is correct |
23 |
Correct |
467 ms |
48284 KB |
Output is correct |
24 |
Correct |
481 ms |
48104 KB |
Output is correct |
25 |
Correct |
478 ms |
47648 KB |
Output is correct |