#include <bits/stdc++.h>
#define MAXN 500000
using namespace std;
int N,Q,a[MAXN];
struct node{
int pref,suff,sum,ans;
};
node st[4*MAXN];
node merge(node nl,node nr){
node n;
n.pref=max(nl.pref,nl.sum+nr.pref);
n.suff=max(nr.suff,nr.sum+nl.suff);
n.sum=nl.sum+nr.sum;
n.ans=max({nl.pref+nr.suff,nl.sum+nr.ans,nr.sum+nl.ans});
return n;
}
//segment tree
void build(int v=1,int start=0,int end=N-1){
if(start==end){
st[v]={max(0,a[start]),max(0,a[start]),a[start],max(0,a[start])};
return;
}
int mid=(start+end)/2;
build(2*v,start,mid);
build(2*v+1,mid+1,end);
st[v]=merge(st[2*v],st[2*v+1]);
}
node query(int from,int to,int v=1,int start=0,int end=N-1){
if(start==from&&end==to){
return st[v];
}
int mid=(start+end)/2;
if(to<=mid){
return query(from,to,2*v,start,mid);
}else if(from>mid){
return query(from,to,2*v+1,mid+1,end);
}else{
return merge(query(from,mid,2*v,start,mid),query(mid+1,to,2*v+1,mid+1,end));
}
}
int main(){
scanf("%d",&N);
for(int i=0;i<N;i++){
char c;
scanf(" %c",&c);
a[i]=(c=='C')?-1:1;
}
build();
scanf("%d",&Q);
for(int i=0;i<Q;i++){
int l,r;
scanf("%d %d",&l,&r);
l--; r--;
printf("%d\n",query(l,r).ans);
}
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf("%d",&N);
| ~~~~~^~~~~~~~~
election.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf(" %c",&c);
| ~~~~~^~~~~~~~~~
election.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d",&Q);
| ~~~~~^~~~~~~~~
election.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d %d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
47 ms |
4848 KB |
Output is correct |
7 |
Correct |
46 ms |
5744 KB |
Output is correct |
8 |
Correct |
45 ms |
5792 KB |
Output is correct |
9 |
Correct |
41 ms |
5804 KB |
Output is correct |
10 |
Correct |
46 ms |
5744 KB |
Output is correct |
11 |
Correct |
47 ms |
5904 KB |
Output is correct |
12 |
Correct |
47 ms |
5884 KB |
Output is correct |
13 |
Correct |
48 ms |
5956 KB |
Output is correct |
14 |
Correct |
54 ms |
5876 KB |
Output is correct |
15 |
Correct |
64 ms |
5772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
47 ms |
4848 KB |
Output is correct |
7 |
Correct |
46 ms |
5744 KB |
Output is correct |
8 |
Correct |
45 ms |
5792 KB |
Output is correct |
9 |
Correct |
41 ms |
5804 KB |
Output is correct |
10 |
Correct |
46 ms |
5744 KB |
Output is correct |
11 |
Correct |
47 ms |
5904 KB |
Output is correct |
12 |
Correct |
47 ms |
5884 KB |
Output is correct |
13 |
Correct |
48 ms |
5956 KB |
Output is correct |
14 |
Correct |
54 ms |
5876 KB |
Output is correct |
15 |
Correct |
64 ms |
5772 KB |
Output is correct |
16 |
Correct |
432 ms |
27336 KB |
Output is correct |
17 |
Correct |
379 ms |
27460 KB |
Output is correct |
18 |
Correct |
423 ms |
27388 KB |
Output is correct |
19 |
Correct |
341 ms |
26964 KB |
Output is correct |
20 |
Correct |
445 ms |
26524 KB |
Output is correct |
21 |
Correct |
435 ms |
28260 KB |
Output is correct |
22 |
Correct |
449 ms |
28184 KB |
Output is correct |
23 |
Correct |
458 ms |
28340 KB |
Output is correct |
24 |
Correct |
429 ms |
28004 KB |
Output is correct |
25 |
Correct |
438 ms |
27464 KB |
Output is correct |