답안 #159631

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
159631 2019-10-23T16:05:58 Z iefnah06 Election (BOI18_election) C++11
100 / 100
1298 ms 56940 KB
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 5.1e5;
int N, Q;
string S;
int pref[MAXN];

struct seg {
	seg* ch[2];
	int incr;
	int maxVal;
};
seg nodes[MAXN*2];
int V;

void propagate(seg* n) {
	for (int i = 0; i < 2; i++) {
		n->ch[i]->maxVal += n->incr;
		n->ch[i]->incr += n->incr;
	}
	n->incr = 0;
}

void update(seg* n) {
	n->maxVal = max(n->ch[0]->maxVal, n->ch[1]->maxVal);
}

seg* build(int x = 0, int y = N+1) {
	seg* n = &nodes[V++];
	if (y - x == 1) {
		n->maxVal = pref[x];
	} else {
		int z = (x + y) / 2;
		n->ch[0] = build(x, z);
		n->ch[1] = build(z, y);
		update(n);
	}
	return n;
}

void update(int l, int r, int v, seg* n, int x = 0, int y = N+1) {
	if (l <= x && y <= r) {
		n->maxVal += v;
		n->incr += v;
	} else {
		propagate(n);
		int z = (x + y) / 2;
		if (l < z) {
			update(l, r, v, n->ch[0], x, z);
		}
		if (z < r) {
			update(l, r, v, n->ch[1], z, y);
		}
		update(n);
	}
}

int query(int l, int r, seg* n, int x = 0, int y = N+1) {
	if (l <= x && y <= r) {
		return n->maxVal;
	} else {
		propagate(n);
		int ans = -MAXN;
		int z = (x + y) / 2;
		if (l < z) {
			ans = max(ans, query(l, r, n->ch[0], x, z));
		}
		if (z < r) {
			ans = max(ans, query(l, r, n->ch[1], z, y));
		}
		return ans;
	}
}

int bit[MAXN];
void update(int i, int v) {
	for (i++; i <= N+5; i += (i & -i)) {
		bit[i] += v;
	}
}
int query(int i) {
	int ans = 0;
	for (; i; i -= (i & -i)) {
		ans += bit[i];
	}
	return ans;
}
int query(int l, int r) { // half open
	return query(r) - query(l);
}

const int MAXQ = 5.1e5;
int ans[MAXQ];
vector<pair<int, int>> queries[MAXN];

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> N >> S;
	pref[0] = 0;
	for (int i = 0; i < N; i++) {
		pref[i+1] = pref[i] + (S[i] == 'C' ? +1 : -1);
	}
	seg* root = build();

	cin >> Q;
	for (int q = 0; q < Q; q++) {
		int l, r; cin >> l >> r; l--;
		queries[l].emplace_back(r, q);
	}

	vector<int> st;
	for (int x = N; x >= 0; x--) {
		while (!st.empty() && pref[st.back()] >= pref[x]) {
			int i = st.back();
			update(i, -1);
			update(i, N+1, -1, root);
			st.pop_back();
		}
		for (auto q : queries[x]) {
			int r = q.first;
			int ind = q.second;
			ans[ind] = query(x+1, r+1) + query(x, r+1, root) - query(r, r+1, root);
		}
		{
			update(x, 1);
			update(x, N+1, 1, root);
			st.push_back(x);
		}
	}
	for (int q = 0; q < Q; q++) {
		cout << ans[q] << '\n';
	}

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12536 KB Output is correct
2 Correct 17 ms 12536 KB Output is correct
3 Correct 16 ms 12536 KB Output is correct
4 Correct 15 ms 12536 KB Output is correct
5 Correct 16 ms 12536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12536 KB Output is correct
2 Correct 17 ms 12536 KB Output is correct
3 Correct 16 ms 12536 KB Output is correct
4 Correct 15 ms 12536 KB Output is correct
5 Correct 16 ms 12536 KB Output is correct
6 Correct 176 ms 19228 KB Output is correct
7 Correct 123 ms 18680 KB Output is correct
8 Correct 134 ms 18732 KB Output is correct
9 Correct 131 ms 19084 KB Output is correct
10 Correct 136 ms 19064 KB Output is correct
11 Correct 132 ms 19304 KB Output is correct
12 Correct 139 ms 19312 KB Output is correct
13 Correct 148 ms 19472 KB Output is correct
14 Correct 135 ms 19292 KB Output is correct
15 Correct 135 ms 19320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 12536 KB Output is correct
2 Correct 17 ms 12536 KB Output is correct
3 Correct 16 ms 12536 KB Output is correct
4 Correct 15 ms 12536 KB Output is correct
5 Correct 16 ms 12536 KB Output is correct
6 Correct 176 ms 19228 KB Output is correct
7 Correct 123 ms 18680 KB Output is correct
8 Correct 134 ms 18732 KB Output is correct
9 Correct 131 ms 19084 KB Output is correct
10 Correct 136 ms 19064 KB Output is correct
11 Correct 132 ms 19304 KB Output is correct
12 Correct 139 ms 19312 KB Output is correct
13 Correct 148 ms 19472 KB Output is correct
14 Correct 135 ms 19292 KB Output is correct
15 Correct 135 ms 19320 KB Output is correct
16 Correct 1199 ms 55084 KB Output is correct
17 Correct 957 ms 51420 KB Output is correct
18 Correct 1103 ms 52352 KB Output is correct
19 Correct 1047 ms 54044 KB Output is correct
20 Correct 1178 ms 54124 KB Output is correct
21 Correct 1153 ms 56580 KB Output is correct
22 Correct 1208 ms 56176 KB Output is correct
23 Correct 1194 ms 56940 KB Output is correct
24 Correct 1231 ms 56300 KB Output is correct
25 Correct 1298 ms 55544 KB Output is correct