#include<bits/stdc++.h>
using namespace std;
struct vale{
int S, mir, mal, ans;
};
vale Z, BL;
vale CO(vale X, vale Y){
if(X.ans == 1e9) return Y;
if(Y.ans == 1e9) return X;
Z.S = X.S+ Y.S;
Z.mir = min(Y.mir + X.S, X.mir);
Z.mal = max(X.mal, Y.mal + X.S);
Z.ans = max(X.ans, Y.ans);
Z.ans = max(Z.ans, Y.mal + X.S - X.mir);
return Z;
}
struct NODE{
vale VAL;
NODE *L, *R;
void upd(){
this->VAL = CO(this->L->VAL, this->R->VAL);
}
};
int s[500009];
void build(NODE *&x, int l, int r){
x = new NODE();
if(l == r){
int k = s[l] - s[l-1];
x->VAL.S = k;
x->VAL.mir = min(0, k);
x->VAL.mal = max(0, k);
x->VAL.ans = max(k, 0);
}
else{
int mid = (l + r)/2;
build(x->L, l, mid);
build(x->R, mid+1, r);
x->upd();
}
}
vale cnt(NODE *&x, int l, int r, int a, int b){
if(a > r || b < l) { return BL;}
if(l >= a && r <= b) {return x->VAL;}
int mid = (l + r)/2;
return CO(cnt(x->L, l, mid, a ,b),cnt(x->R, mid+1, r, a, b));
}
NODE *rt, *ANS;
main(){
BL .ans = 1e9;
int n;
cin >> n;
string W;
cin >> W;
for(int i = 1; i <= W.size(); i++)
if(W[i-1] == 'C') s[i] = s[i-1]+1;
else s[i] = s[i-1] - 1;
build(rt,1,n);
int q;
cin >> q;
while(q--){
int l, r;
scanf("%d%d",&l,&r);
vale ANS = cnt(rt, 1, n, l, r);
printf("%d\n", ANS. ans - ANS.S);
/*int S = 0;
int M = 0;
for(int i = l - 1; i <= r; i++){
if(s[i] + S < s[l - 1]){
S++;
M = max(0, M-1);
}
if(s[i] > s[r]){
M = max(M, s[i] - s[r]);
}
}
cout << S + M << endl;*/
}
}
Compilation message
election.cpp:51:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
election.cpp: In function 'int main()':
election.cpp:57:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i <= W.size(); i++)
~~^~~~~~~~~~~
election.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&l,&r);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
664 KB |
Output is correct |
4 |
Correct |
4 ms |
744 KB |
Output is correct |
5 |
Correct |
5 ms |
824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
664 KB |
Output is correct |
4 |
Correct |
4 ms |
744 KB |
Output is correct |
5 |
Correct |
5 ms |
824 KB |
Output is correct |
6 |
Correct |
129 ms |
7744 KB |
Output is correct |
7 |
Correct |
137 ms |
8076 KB |
Output is correct |
8 |
Correct |
115 ms |
8980 KB |
Output is correct |
9 |
Correct |
108 ms |
9812 KB |
Output is correct |
10 |
Correct |
124 ms |
10760 KB |
Output is correct |
11 |
Correct |
143 ms |
12148 KB |
Output is correct |
12 |
Correct |
164 ms |
12880 KB |
Output is correct |
13 |
Correct |
151 ms |
13928 KB |
Output is correct |
14 |
Correct |
154 ms |
14888 KB |
Output is correct |
15 |
Correct |
134 ms |
15780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
504 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
664 KB |
Output is correct |
4 |
Correct |
4 ms |
744 KB |
Output is correct |
5 |
Correct |
5 ms |
824 KB |
Output is correct |
6 |
Correct |
129 ms |
7744 KB |
Output is correct |
7 |
Correct |
137 ms |
8076 KB |
Output is correct |
8 |
Correct |
115 ms |
8980 KB |
Output is correct |
9 |
Correct |
108 ms |
9812 KB |
Output is correct |
10 |
Correct |
124 ms |
10760 KB |
Output is correct |
11 |
Correct |
143 ms |
12148 KB |
Output is correct |
12 |
Correct |
164 ms |
12880 KB |
Output is correct |
13 |
Correct |
151 ms |
13928 KB |
Output is correct |
14 |
Correct |
154 ms |
14888 KB |
Output is correct |
15 |
Correct |
134 ms |
15780 KB |
Output is correct |
16 |
Correct |
1553 ms |
67552 KB |
Output is correct |
17 |
Correct |
1340 ms |
75048 KB |
Output is correct |
18 |
Correct |
1326 ms |
82420 KB |
Output is correct |
19 |
Correct |
1169 ms |
89596 KB |
Output is correct |
20 |
Correct |
1747 ms |
96680 KB |
Output is correct |
21 |
Correct |
1441 ms |
106156 KB |
Output is correct |
22 |
Correct |
1416 ms |
113752 KB |
Output is correct |
23 |
Correct |
1580 ms |
121724 KB |
Output is correct |
24 |
Correct |
1403 ms |
129208 KB |
Output is correct |
25 |
Correct |
1557 ms |
136324 KB |
Output is correct |