#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 1223372036854775807
#define INF2 122337203
#define MOD 998244353
#define cint(x) int x;cin>>x;
#define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i];
#define coutarr(a) for (auto d:a)cout<<d<<" "; cout<<endl;
#define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl;
#define BERKAY_TUP ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
#define mid (start+end)/2
#define vvi vector<vector<int>>
int t=1;
int interactive=0;
int usaco=0;
int testCase=0;
int tree[2000023][2];
int z[500023];
int cc(int l,int r){
return z[r]-z[l-1];
}
int tt(int l,int r){
return (r-l+1-cc(l,r));
}
string s;
void build(int n,int start,int end){
if (start==end){
if(s[start-1]=='C'){
tree[n][0]=0;
tree[n][1]=0;
}
else{
tree[n][0]=1;
tree[n][1]=1;
}
return;
}
build(n*2,start,mid);
build(n*2+1,mid+1,end);
int k=cc(start,mid)-tt(start,mid)+tree[n*2][0];
tree[n][0]=max(tree[n*2+1][0]-k,0ll)+tree[n*2][0];
int g=tree[n*2+1][1]-tree[n*2+1][0]+min(tree[n*2+1][0],k)+max(tree[n*2+1][0]-k,0ll);
int k2=cc(mid+1,end)-tt(mid+1,end)+g;
int g2=max(tree[n*2][1]-tree[n*2][0]-k2,0ll)+tree[n*2][0];
tree[n][1]=g+g2;
//cout<<n<<" "<<start<<" "<<end<<" "<<tree[n][0]<<" "<<tree[n][1]<<endl;
}
pii query(int n,int start,int end,int l,int r){
if (start>end || start>r || end<l) return {-1,-1};
if (start>=l && end<=r) return {tree[n][0],tree[n][1]};
pii c1=query(n*2,start,mid,l,r);
pii c2=query(n*2+1,mid+1,end,l,r);
if (c1.fi==-1) return c2;
if (c2.fi==-1) return c1;
int k=cc(l,mid)-tt(l,mid)+c1.fi;
int ans1=max(c2.se-k,0ll)+c1.fi;
int g=c2.se-c2.fi+min(c2.se,k)+max(c2.fi-k,0ll);
int k2=cc(mid+1,r)-tt(mid+1,r)+g;
int g2=max(c1.se-c1.fi-k2,0ll)+c1.fi;
int ans2=g+g2;
return {ans1,ans2};
}
void solve(){
int n;
cin>>n;
cin>>s;
int sm=0;
for (int i=1;i<=n;i++){
if (s[i-1]=='C') sm++;
z[i]=sm;
}
build(1,1,n);
int q;
cin>>q;
while (q--){
int l,r;
cin>>l>>r;
pii z=query(1,1,n,l,r);
cout<<z.se<<endl;
}
}
int32_t main(){
BERKAY_TUP;
if (usaco){
freopen("team.in", "r", stdin);
freopen("team.out", "w", stdout);
}
if (!interactive){
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out2.txt", "w", stdout);
//freopen("wormsort.out", "w", stdout);
#endif
}
if (t==1) solve();
else{
cin>>t;
while (t--){testCase++;solve();}
}
return 0;
}
Compilation message (stderr)
election.cpp: In function 'int32_t main()':
election.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen("team.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
election.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen("team.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |