제출 #1016078

#제출 시각아이디문제언어결과실행 시간메모리
1016078dostsElection (BOI18_election)C++17
0 / 100
2 ms604 KiB
//Dost SEFEROĞLU #include <bits/stdc++.h> using namespace std; #define int long long #define pii pair<int,int> #define ff first #define ss second #define sp << " " << #define vi vector<int> #define vvi vector<vi> const int N = 4e5+1,inf = 2e18,B = 23,MOD = 998244353,LIM = 1e9; struct Node { int sm,wp,ws,ans; }; Node merge(Node a,Node b) { Node ret; ret.sm = a.sm+b.sm; ret.wp = max(a.wp,a.sm+b.wp); ret.ws = max(b.ws,b.sm+a.ws); ret.ans = max({a.ans+b.ws,b.ans+a.wp,a.wp+b.ws}); return ret; }; struct ST { vector<Node> t; ST(int nn) { t.assign(4*nn+1,{0,0,0,0}); } void update(int node,int l,int r,int p,int v) { if (l == r) { t[node] = {v,max(0ll,v),max(0ll,v),max(0ll,v)}; return; } int m = (l+r) >> 1; if (p <= m) update(2*node,l,m,p,v); else update(2*node+1,m+1,r,p,v); t[node] = merge(t[2*node],t[2*node+1]); } Node query(int node,int l,int r,int L,int R) { if (l > R || r < L) return {0,0,0,0}; if (l >= L && r <= R) return t[node]; int m = (l+r) >> 1; return merge(query(2*node,l,m,L,R),query(2*node+1,m+1,r,L,R)); } }; void solve() { int n; cin >> n; string s; cin >> s; ST st(n); for (int i=1;i<=n;i++) { if (s[i-1] == 'C') st.update(1,1,n,i,-1); else st.update(1,1,n,i,1); } int q; cin >> q; while (q--) { int l,r; cin >> l >> r; cout << st.query(1,1,n,l,r).ans << '\n'; } } signed main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #ifdef Dodi freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int t = 1; //cin >> t; while (t --> 0) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...