#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];
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 |
672 KB |
Output is correct |
3 |
Correct |
1 ms |
604 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 |
672 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
53 ms |
10576 KB |
Output is correct |
7 |
Correct |
50 ms |
10576 KB |
Output is correct |
8 |
Correct |
52 ms |
10320 KB |
Output is correct |
9 |
Correct |
43 ms |
10340 KB |
Output is correct |
10 |
Correct |
53 ms |
10472 KB |
Output is correct |
11 |
Correct |
55 ms |
10576 KB |
Output is correct |
12 |
Correct |
57 ms |
10580 KB |
Output is correct |
13 |
Correct |
53 ms |
10580 KB |
Output is correct |
14 |
Correct |
58 ms |
10576 KB |
Output is correct |
15 |
Correct |
59 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
672 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
53 ms |
10576 KB |
Output is correct |
7 |
Correct |
50 ms |
10576 KB |
Output is correct |
8 |
Correct |
52 ms |
10320 KB |
Output is correct |
9 |
Correct |
43 ms |
10340 KB |
Output is correct |
10 |
Correct |
53 ms |
10472 KB |
Output is correct |
11 |
Correct |
55 ms |
10576 KB |
Output is correct |
12 |
Correct |
57 ms |
10580 KB |
Output is correct |
13 |
Correct |
53 ms |
10580 KB |
Output is correct |
14 |
Correct |
58 ms |
10576 KB |
Output is correct |
15 |
Correct |
59 ms |
10576 KB |
Output is correct |
16 |
Correct |
494 ms |
47328 KB |
Output is correct |
17 |
Correct |
411 ms |
47160 KB |
Output is correct |
18 |
Correct |
454 ms |
47376 KB |
Output is correct |
19 |
Correct |
382 ms |
46824 KB |
Output is correct |
20 |
Incorrect |
495 ms |
46564 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |