This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Dost SEFEROĞLU
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define vvi vector<vi>
const int N = 4e5+1,inf = 2e18,B = 23,MOD = 998244353,LIM = 1e9;
struct Node {
int sm,wp,ws,ans;
};
Node merge(Node a,Node b) {
Node ret;
ret.sm = a.sm+b.sm;
ret.wp = max(a.wp,a.sm+b.wp);
ret.ws = max(b.ws,b.sm+a.ws);
ret.ans = max({a.ans+b.ws,b.ans+a.wp,a.wp+b.ws});
return ret;
};
struct ST {
vector<Node> t;
ST(int nn) {
t.assign(4*nn+1,{0,0,0,0});
}
void update(int node,int l,int r,int p,int v) {
if (l == r) {
t[node] = {v,max(0ll,v),max(0ll,v),max(0ll,v)};
return;
}
int m = (l+r) >> 1;
if (p <= m) update(2*node,l,m,p,v);
else update(2*node+1,m+1,r,p,v);
t[node] = merge(t[2*node],t[2*node+1]);
}
Node query(int node,int l,int r,int L,int R) {
if (l > R || r < L) return {0,0,0,0};
if (l >= L && r <= R) return t[node];
int m = (l+r) >> 1;
return merge(query(2*node,l,m,L,R),query(2*node+1,m+1,r,L,R));
}
};
void solve() {
int n;
cin >> n;
string s;
cin >> s;
ST st(n);
for (int i=1;i<=n;i++) {
if (s[i-1] == 'C') st.update(1,1,n,i,-1);
else st.update(1,1,n,i,1);
}
int q;
cin >> q;
while (q--) {
int l,r;
cin >> l >> r;
cout << st.query(1,1,n,l,r).ans << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |