Submission #852914

#TimeUsernameProblemLanguageResultExecution timeMemory
852914AbitoElection (BOI18_election)C++17
0 / 100
4 ms31832 KiB
#include <bits/stdc++.h> #define F first #define S second #define pb push_back #define ppb pop_back #define ep insert #define endl '\n' #define elif else if #define pow pwr #define sqrt sqrtt //#define int long long #define y1 YONE typedef unsigned long long ull; using namespace std; const int N=5e5+5; int n,q,a[N],st[25][N],lg[N],p[N],ans[N],l[N],r[N],ct[N]; void build(){ for (int i=1;i<=n;i++) st[0][i]=p[i]; for (int i=1;i<25;i++){ for (int j=1;j+(1<<i)<=n+1;j++){ st[i][j]=min(st[i-1][j],st[i-1][j+(1<<(i-1))]); } }return; } int query(int l,int r){ int i=lg[r-l+1]; return min(st[i][l],st[i][r-(1<<i)+1]); } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); for (int i=2;i<N;i++) lg[i]=lg[i/2]+1; cin>>n; for (int i=1;i<=n;i++){ char c;cin>>c; if (c=='C') a[i]=1; else a[i]=-1; p[i]=a[i]+p[i-1]; ct[i]=ct[i-1]+bool(c=='T'); }build(); cin>>q; for (int i=1;i<=q;i++){ cin>>l[i]>>r[i]; ans[i]=max(0,-(query(l[i],r[i])-p[l[i]-1])); int L=l[i],R=r[i],mid,x=r[i]; while (L<=R){ //cout<<L<<' '<<R<<endl; mid=(L+R)/2; if (ct[mid]-ct[l[i]-1]>=ans[i]){ x=mid; R=mid-1; }else L=mid+1; }l[i]=x+1; //cout<<l[i]<<' '<<r[i]<<endl; } for (int i=n;i;i--) p[i]=p[i+1]+a[i]; build(); for (int i=1;i<=q;i++){ if (l[i]>r[i]) continue; ans[i]+=max(0,p[r[i]+1]-query(l[i],r[i])); } for (int i=1;i<=q;i++) cout<<ans[i]<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...