Submission #631862

#TimeUsernameProblemLanguageResultExecution timeMemory
631862czhang2718Election (BOI18_election)C++17
100 / 100
460 ms28028 KiB
#include "bits/stdc++.h" using namespace std; const int N=5e5; int n, q; string s; struct Node{ int pre, suf, sum, ans; Node operator+(Node b){ Node ret; ret.pre=max(pre, sum+b.pre); ret.suf=max(b.suf, b.sum+suf); ret.sum=sum+b.sum; ret.ans=max({ans+b.sum, b.ans+sum, pre+b.suf}); return ret; } } seg[4*N]; Node qry(int l, int r, int x=0, int lx=0, int rx=n){ if(lx>=l && rx<=r) return seg[x]; if(lx>=r || rx<=l) return {0, 0, 0, 0}; int m=(lx+rx)/2; return qry(l, r, 2*x+1, lx, m)+qry(l, r, 2*x+2, m, rx); } void build(int x=0, int lx=0, int rx=n){ if(rx-lx==1){ if(s[lx]=='C') seg[x]={0, 0, -1, 0}; else seg[x]={1, 1, 1, 1}; return; } int m=(lx+rx)/2; build(2*x+1, lx, m); build(2*x+2, m, rx); seg[x]=seg[2*x+1]+seg[2*x+2]; // cout << "seg[" << lx << " " << rx << "] " << seg[x].pre << " " << seg[x].suf << " " << seg[x].sum << " " << seg[x].ans << "\n"; } int main(){ cin.tie(0)->sync_with_stdio(0); cin >> n >> s; build(); cin >> q; while(q--){ int l,r; cin >> l >> r; cout << qry(l-1,r).ans << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...