Submission #759365

#TimeUsernameProblemLanguageResultExecution timeMemory
759365BulaElection (BOI18_election)C++17
0 / 100
15 ms20020 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long const ll mod=1e9+7,N=5e5+1; vector<int> pref(N),suf(N+1),t(4*N),t1(4*N); int getans(int v,int tl,int tr,int l,int r){ if(l>r) return 0; if(l==tl && r==tr){ return t[v]; } int tm=(tl+tr)/2; return max(getans(v*2,tl,tm,l,min(r,tm)),getans(v*2+1,tm+1,tr,max(l,tm+1),r)); } void build(int v,int tl,int tr){ if(tl==tr){ t[v]=pref[tl]; }else{ int tm=(tl+tr)/2; build(v*2,tl,tm); build(v*2+1,tm+1,tr); t[v]=max(t[v*2],t[v*2+1]); } } int getans1(int v,int tl,int tr,int l,int r){ if(l>r) return 0; if(l==tl && r==tr){ return t1[v]; } int tm=(tl+tr)/2; return max(getans1(v*2,tl,tm,l,min(r,tm)),getans1(v*2+1,tm+1,tr,max(l,tm+1),r)); } void build1(int v,int tl,int tr){ if(tl==tr){ t1[v]=suf[tl]; }else{ int tm=(tl+tr)/2; build1(v*2,tl,tm); build1(v*2+1,tm+1,tr); t1[v]=max(t1[v*2],t1[v*2+1]); } } int main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); int n; cin>>n; string s; cin>>s; vector<int> v(n+1); for(int i=1;i<=n;i++){ if(s[i-1]=='T') v[i]=1; else v[i]=-1; } for(int i=1;i<=n;i++){ pref[i]=pref[i-1]+v[i]; } for(int i=n;i>=1;i--){ suf[i]=suf[i+1]+v[i]; } build(1,1,n); build1(1,1,n); int q; cin>>q; for(int i=1;i<=q;i++){ int l,r; cin>>l>>r; cout<<max(getans(1,1,n,l,r)-pref[l-1],getans1(1,1,n,l,r)-suf[r+1])<<endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...