#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define endl "\n"
// #define int ll
using ull=unsigned long long;
using ll=long long;
using pii=pair<int,int>;
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int mxn=5e5+100;
struct Node{
int sum;
int mxl;
int mxr;
int ans;
} tree[4*mxn];
int arr[mxn];
Node merge(Node a,Node b){
Node c;
c.sum=a.sum+b.sum;
c.mxl=max(a.mxl,a.sum+b.mxl);
c.mxr=max(b.mxr,b.sum+a.mxr);
c.ans=max({a.mxl+b.mxr,a.sum+b.ans,b.sum+a.ans});
return c;
}
void build(int node,int l,int r){
if(l==r){
int x=max(0,arr[l]);
tree[node]={arr[l],x,x,x};
return;
}
int mid=(l+r)/2;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
tree[node]=merge(tree[node<<1],tree[node<<1|1]);
}
Node query(int node,int l,int r,int ql,int qr){
if(ql>r||qr<l) return {0,0,0,0};
if(ql<=l&&r<=qr) return tree[node];
int mid=(l+r)/2;
return merge(
query(node<<1,l,mid,ql,qr),
query(node<<1|1,mid+1,r,ql,qr)
);
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n;
cin>>n;
string s;
cin>>s;
// int arr[n];
for(int i=0;i<n;i++){
if(s[i]=='C') arr[i]=-1;
else arr[i]=1;
}
build(1,0,n-1);
int q;
cin>>q;
while(q--){
int l,r;
cin>>l>>r;
cout<<query(1,0,n-1,l-1,r-1).ans<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
39 ms |
5204 KB |
Output is correct |
7 |
Correct |
35 ms |
5212 KB |
Output is correct |
8 |
Correct |
33 ms |
5212 KB |
Output is correct |
9 |
Correct |
30 ms |
5160 KB |
Output is correct |
10 |
Correct |
33 ms |
4952 KB |
Output is correct |
11 |
Correct |
34 ms |
5208 KB |
Output is correct |
12 |
Correct |
35 ms |
5104 KB |
Output is correct |
13 |
Correct |
36 ms |
5140 KB |
Output is correct |
14 |
Correct |
37 ms |
5220 KB |
Output is correct |
15 |
Correct |
37 ms |
5188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
39 ms |
5204 KB |
Output is correct |
7 |
Correct |
35 ms |
5212 KB |
Output is correct |
8 |
Correct |
33 ms |
5212 KB |
Output is correct |
9 |
Correct |
30 ms |
5160 KB |
Output is correct |
10 |
Correct |
33 ms |
4952 KB |
Output is correct |
11 |
Correct |
34 ms |
5208 KB |
Output is correct |
12 |
Correct |
35 ms |
5104 KB |
Output is correct |
13 |
Correct |
36 ms |
5140 KB |
Output is correct |
14 |
Correct |
37 ms |
5220 KB |
Output is correct |
15 |
Correct |
37 ms |
5188 KB |
Output is correct |
16 |
Correct |
300 ms |
28388 KB |
Output is correct |
17 |
Correct |
271 ms |
28532 KB |
Output is correct |
18 |
Correct |
268 ms |
28884 KB |
Output is correct |
19 |
Correct |
282 ms |
28340 KB |
Output is correct |
20 |
Correct |
298 ms |
28132 KB |
Output is correct |
21 |
Correct |
326 ms |
29864 KB |
Output is correct |
22 |
Correct |
362 ms |
29664 KB |
Output is correct |
23 |
Correct |
314 ms |
29924 KB |
Output is correct |
24 |
Correct |
328 ms |
29668 KB |
Output is correct |
25 |
Correct |
295 ms |
29148 KB |
Output is correct |