typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
struct Node{
int sum=0,pref=0,suf=0,ans=0;
};
struct Seg{
int n;
vector<Node>tree;
vector<int>arr;
Node comb(Node x,Node y){
Node res;
res.sum=x.sum+y.sum;
res.pref=max(x.pref,x.sum+y.pref);
res.suf=max(x.suf+y.sum,y.suf);
res.ans=max(max(x.ans+y.sum,x.sum+y.ans),x.pref+y.suf);
return res;
}
void build(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left==right){
tree[node].sum=arr[left];
tree[node].pref=max(arr[left],0);
tree[node].suf=max(arr[left],0);
tree[node].ans=max(arr[left],0);
return;
}
build(node*2,left,mid);build(node*2+1,mid+1,right);
tree[node]=comb(tree[node*2],tree[node*2+1]);
}
Seg(vector<int>v){
n=v.size();
arr=v;
tree.resize(n<<2);
build();
}
int l,r;
Node qu(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
Node res;
if(left>r||right<l)return res;
if(left>=l&&right<=r)return tree[node];
res=comb(qu(node*2,left,mid),qu(node*2+1,mid+1,right));
return res;
}
int query(int lef,int rig){
l=lef;r=rig;
return qu().ans;
}
};
void code(){
int n;cin>>n;
vector<int>v;
for(int i=0;i<n;i++){
char c;cin>>c;
v.pb(c=='T'?1:-1);
}
Seg seg(v);
int q;cin>>q;
while(q--){
int l,r;cin>>l>>r;
cout<<seg.query(l-1,r-1)<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
int t=1;
if(!t)cin>>t;
while(t--){code();cout<<endl;}
return 0;
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:80:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~
election.cpp:80:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
34 ms |
6588 KB |
Output is correct |
7 |
Correct |
33 ms |
6596 KB |
Output is correct |
8 |
Correct |
33 ms |
6600 KB |
Output is correct |
9 |
Correct |
30 ms |
6652 KB |
Output is correct |
10 |
Correct |
35 ms |
6508 KB |
Output is correct |
11 |
Correct |
37 ms |
6556 KB |
Output is correct |
12 |
Correct |
42 ms |
6588 KB |
Output is correct |
13 |
Correct |
35 ms |
6752 KB |
Output is correct |
14 |
Correct |
34 ms |
6600 KB |
Output is correct |
15 |
Correct |
36 ms |
6684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
34 ms |
6588 KB |
Output is correct |
7 |
Correct |
33 ms |
6596 KB |
Output is correct |
8 |
Correct |
33 ms |
6600 KB |
Output is correct |
9 |
Correct |
30 ms |
6652 KB |
Output is correct |
10 |
Correct |
35 ms |
6508 KB |
Output is correct |
11 |
Correct |
37 ms |
6556 KB |
Output is correct |
12 |
Correct |
42 ms |
6588 KB |
Output is correct |
13 |
Correct |
35 ms |
6752 KB |
Output is correct |
14 |
Correct |
34 ms |
6600 KB |
Output is correct |
15 |
Correct |
36 ms |
6684 KB |
Output is correct |
16 |
Correct |
303 ms |
45224 KB |
Output is correct |
17 |
Correct |
279 ms |
44924 KB |
Output is correct |
18 |
Correct |
290 ms |
45188 KB |
Output is correct |
19 |
Correct |
247 ms |
44580 KB |
Output is correct |
20 |
Correct |
311 ms |
44452 KB |
Output is correct |
21 |
Correct |
318 ms |
46192 KB |
Output is correct |
22 |
Correct |
313 ms |
46220 KB |
Output is correct |
23 |
Correct |
324 ms |
46240 KB |
Output is correct |
24 |
Correct |
303 ms |
45912 KB |
Output is correct |
25 |
Correct |
311 ms |
45604 KB |
Output is correct |