This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define ld long double
#define ar array
#define vt vector
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define lla(v) v.rbegin(), v.rend()
ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while(lb<rb) {
ll mb=(lb+rb)/2;
f(mb)?rb=mb:lb=mb+1;
}
return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while(lb<rb) {
ll mb=(lb+rb+1)/2;
f(mb)?lb=mb:rb=mb-1;
}
return lb;
}
const ll INF=1000000000000000000LL;
const ll MOD=1e9+7;
const ll M=(1ll<<61)-1;
//const ll MOD=998244353; //sometimes need to be used
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
const ll base=uniform_int_distribution<ll>(0, M-1)(rng);
ll bpow(ll a, ll b, ll DOM) {
a%=DOM;
ll res=1;
while(b>0) {
if(b&1)
res=res*a%DOM;
a=a*a%DOM;
b>>=1;
}
return res;
}
int gcd_x, gcd_y, gcd_d;
int gcd(int a, int b) {
if (!b) {
gcd_x=1;
gcd_y=0;
return a;
}
gcd_d=gcd(b, a%b);
int tmp=gcd_x;
gcd_x=gcd_y;
gcd_y=tmp-(a/b)*gcd_y;
return gcd_d;
}
ll floor_div(ll x, ll y) {
assert(y!=0);
if (y<0) {
y=-y;
x=-x;
}
if (x>=0) return x/y;
return (x+1)/y-1;
}
ll ceil_div(ll x, ll y) {
assert(y!=0);
if (y<0) {
y=-y;
x=-x;
}
if (x<=0) {
return x/y;
}
return (x-1)/y+1;
}
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
const int mxN=5e5+1;
int n, q;
string s;
struct Nodes {
int mx_pref, mx_suf;
int sum;
int ans;
} ST[1<<20];
// 1: Cap
// 0: Tony
Nodes combine(const Nodes& a, const Nodes& b) {
Nodes res;
res.mx_pref=max(a.mx_pref, a.sum+b.mx_pref);
res.mx_suf=max(b.mx_suf, a.mx_suf+b.sum);
res.sum=a.sum+b.sum;
res.ans=max(max(a.ans+b.sum, a.sum+b.ans), a.mx_pref+b.mx_suf);
return res;
}
void upd(int pos, bool val, int id=1, int l=1, int r=n) {
if(pos<l||pos>r) {
return;
}
if(l==r) {
// +1: Tony
// -1: Cap
if(!val) {
ST[id].mx_pref=1;
ST[id].mx_suf=1;
ST[id].sum=1;
ST[id].ans=1;
}
else {
ST[id].mx_pref=0;
ST[id].mx_suf=0;
ST[id].sum=-1;
ST[id].ans=0;
}
return;
}
int mid=(l+r)/2;
upd(pos, val, 2*id, l, mid);
upd(pos, val, 2*id+1, mid+1, r);
ST[id]=combine(ST[2*id], ST[2*id+1]);
}
Nodes qury(int u, int v, int id=1, int l=1, int r=n) {
if(u>r||l>v) {
return {0, 0, 0, 0};
}
if(l>=u&&v>=r) {
return ST[id];
}
int mid=(l+r)/2;
return combine(qury(u, v, 2*id, l, mid), qury(u, v, 2*id+1, mid+1, r));
}
void solve() {
//solve here
cin >> n;
cin >> s;
s=" "+s;
for(int i=1; i<=n; ++i) {
if(s[i]=='C') {
upd(i, 1);
}
else {
upd(i, 0);
}
}
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
cout << qury(l, r).ans << "\n";
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("template.inp", "r", stdin);
//freopen("template.out", "w", stdout);
int TC=1;
//cin >> TC;
while(TC--) {
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... |