Submission #471238

# Submission time Handle Problem Language Result Execution time Memory
471238 2021-09-07T19:57:13 Z disastah Election (BOI18_election) C++17
100 / 100
971 ms 39436 KB
#include <bits/stdc++.h>
#define ar array
using namespace std;
typedef long double ld;
typedef long long ll;
const int inf=2e9+9;
const ll linf=4e18+18;
const int N=5e5;

struct segtr {
	struct node {
		int ans, ansup, ansdown;
	};
	string s;
	vector<int> a, b;
	vector<node> t;
	int n;
	segtr(string &s, int n): s(s), n(n) {
		a.resize(n+1); a[0]=0;
		b.resize(n+1); b[n]=0;
		for (int i=0; i<n; ++i) a[i+1]=a[i]+(s[i]=='C'?1:-1);
		for (int i=n-1; ~i; --i) b[i]=b[i+1]+(s[i]=='C'?1:-1);
		t.resize(n*4);
	}
	void comb(int v, int l, int r) {
		int m=(l+r)/2;
		t[v].ans=t[2*v+1].ansup+t[2*v+2].ansdown;
		int x1=max(0,t[2*v+1].ans-t[2*v+1].ansup-t[2*v+2].ansdown-(b[m]-b[r]));
		int x2=max(0,t[2*v+2].ans-t[2*v+2].ansdown-t[2*v+1].ansup-(a[m]-a[l]));
		t[v].ans+=max(x1,x2);
		t[v].ansup=max(t[2*v+1].ansup,t[2*v+2].ansup-(a[m]-a[l]));
		t[v].ansdown=max(t[2*v+1].ansdown-(b[m]-b[r]),t[2*v+2].ansdown);
	}
	void build(int v, int l, int r) {
		if (l+1==r) {
			if (s[l]=='C') t[v].ans=t[v].ansup=t[v].ansdown=0;
			else t[v].ans=t[v].ansup=t[v].ansdown=1;
		}
		else {
			int m=(l+r)/2;
			build(2*v+1,l,m);
			build(2*v+2,m,r);
			comb(v,l,r);
		}
	} void build() {build(0,0,n);}
	node qry(int v, int tl, int tr, int l, int r) {
		if (l>=r) return {};
		if (tl==l&&tr==r) return t[v];
		int tm=(tl+tr)/2;
		node L=qry(2*v+1,tl,tm,l,min(r,tm)), R=qry(2*v+2,tm,tr,max(l,tm),r);
		if (l>=min(r,tm)) return R;
		if (max(l,tm)>=r) return L;
		node S={};
		S.ans=L.ansup+R.ansdown;
		int x1=max(0,L.ans-L.ansup-R.ansdown-(b[tm]-b[r]));
		int x2=max(0,R.ans-R.ansdown-L.ansup-(a[tm]-a[l]));
		S.ans+=max(x1,x2);
		S.ansup=max(L.ansup,R.ansup-(a[tm]-a[l]));
		S.ansdown=max(L.ansdown-(b[tm]-b[r]),R.ansdown);
		return S;
	} int qry(int l, int r) {
		node ans=qry(0,0,n,l,r);
		return ans.ans;
	}
	void print() {
		for (int i=0; i<n*4; ++i) {
			cout << t[i].ans << " " << t[i].ansup << " " << t[i].ansdown << "\n";
		}
	}
};

int n, q;
string s;

void solve() {
	cin >> n >> s;
	segtr tr(s,n); tr.build();
	cin >> q;
	while(q--) {
		int l, r; cin >> l >> r; --l;
		cout << tr.qry(l,r) << "\n";
		//tr.print();
		//cout << "\n--------------\n";
	}
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
#ifdef disastah
	cout << "Output\n\n";
#endif
/*#ifndef disastah
	freopen("cardgame.in","r",stdin);
	freopen("cardgame.out","w",stdout);
#endif*/
	int tt=1;
//	cin >> tt;
	while (tt--) {
		solve();
		cout << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 452 KB Output is correct
6 Correct 103 ms 5488 KB Output is correct
7 Correct 84 ms 5388 KB Output is correct
8 Correct 82 ms 5392 KB Output is correct
9 Correct 72 ms 5384 KB Output is correct
10 Correct 93 ms 5436 KB Output is correct
11 Correct 91 ms 5584 KB Output is correct
12 Correct 87 ms 5668 KB Output is correct
13 Correct 89 ms 5572 KB Output is correct
14 Correct 89 ms 5584 KB Output is correct
15 Correct 82 ms 5548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 460 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 452 KB Output is correct
6 Correct 103 ms 5488 KB Output is correct
7 Correct 84 ms 5388 KB Output is correct
8 Correct 82 ms 5392 KB Output is correct
9 Correct 72 ms 5384 KB Output is correct
10 Correct 93 ms 5436 KB Output is correct
11 Correct 91 ms 5584 KB Output is correct
12 Correct 87 ms 5668 KB Output is correct
13 Correct 89 ms 5572 KB Output is correct
14 Correct 89 ms 5584 KB Output is correct
15 Correct 82 ms 5548 KB Output is correct
16 Correct 922 ms 38472 KB Output is correct
17 Correct 727 ms 38020 KB Output is correct
18 Correct 872 ms 38556 KB Output is correct
19 Correct 671 ms 37772 KB Output is correct
20 Correct 930 ms 37572 KB Output is correct
21 Correct 869 ms 39252 KB Output is correct
22 Correct 901 ms 39256 KB Output is correct
23 Correct 971 ms 39436 KB Output is correct
24 Correct 873 ms 39068 KB Output is correct
25 Correct 867 ms 38544 KB Output is correct