#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (int)(a.size())
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll A,ll B) {if(!B)return A;return gcd(B,A%B);}
const int MAXN = 555555;
struct node{
ll sum=0,ans=0;
ll pmx=-MAXN,smx=-MAXN;
};
vector<ll>s(MAXN);
node merge(node a,node b){
node c;
c.sum = a.sum+b.sum;
c.pmx = max(a.pmx,a.sum+b.pmx);
c.smx = max(b.smx,a.smx+b.sum);
c.ans = max(a.pmx+b.smx,max(a.ans+b.sum,b.ans+a.sum));
c.ans = max(0LL,c.ans);
return c;
}
vector<node>t(4*MAXN);
void build(int v,int l,int r){
//cout<<l<<" "<<r<<'\n';
if(l == r){
t[v].sum = t[v].smx = t[v].pmx = s[l];
t[v].ans = s[l]==1?0:1;
return;
}
int mid = (l+r)/2;
build(2*v,l,mid);
build(2*v+1,mid+1,r);
t[v] = merge(t[2*v],t[2*v+1]);
}
node query(int v,int l,int r,int tl,int tr){
if(l > r){
node a;
a.pmx = a.smx = 0;
return a;
}
if(tl == l && r == tr)return t[v];
int tm = (tl+tr)/2;
node a = query(2*v,l,min(tm,r),tl,tm);
node b = query(2*v+1,max(l,tm+1),r,tm+1,tr);
return merge(a,b);
}
int main()
{
owo
int n,q;
cin>>n;
for(int i=1;i<=n;i++){
char c;cin>>c;
s[i] = c=='C'?-1:1;
}
build(1,1,n);
//cout<<t[1].ans<<'\n';
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
node ans = query(1,l,r,1,n);
cout<<ans.ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
74220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
74220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
74220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |