답안 #918943

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
918943 2024-01-30T21:43:18 Z myst6 Election (BOI18_election) C++14
100 / 100
755 ms 43844 KB
#include <bits/stdc++.h>
 
using namespace std;
 
struct Tree {
	vector<int> info, tags;
	Tree(int size) {
		info.resize(size*4, 0);
		tags.resize(size*4);
	}
	void pushdown(int x) {
		info[x] += tags[x];
		if (x*2 < tags.size()) tags[x*2] += tags[x];
		if (x*2+1 < tags.size()) tags[x*2+1] += tags[x];
		tags[x] = 0;
	}
	void update(int l, int r, int x, int xl, int xr, int tag) {
		if (l > r) return;
		if (l == xl && r == xr) {
			tags[x] += tag;
		} else {
			int xm = (xl+xr)/2;
			update(l, min(r,xm), x*2, xl, xm, tag);
			update(max(l,xm+1), r, x*2+1, xm+1, xr, tag);
			pushdown(x*2); pushdown(x*2+1);
			info[x] = min(info[x*2], info[x*2+1]);
		}
	}
	int query(int l, int r, int x, int xl, int xr) {
		if (l > r) return 1<<30;
		pushdown(x);
		if (l == xl && r == xr) {
			return info[x];
		} else {
			int xm = (xl+xr)/2;
			int left = query(l, min(r,xm), x*2, xl, xm);
			int right = query(max(l,xm+1), r, x*2+1, xm+1, xr);
			return min(left, right);
		}
	}
};
 
struct BIT {
	vector<int> data;
	BIT(int size) {
		data.resize(size+1);
	}
	void update(int x, int delta) {
		while (x < data.size()) {
			data[x] += delta;
			x += x & (-x);
		}
	}
	int query(int x) {
		int ans = 0;
		while (x > 0) {
			ans += data[x];
			x -= x & (-x);
		}
		return ans;
	}
};
 
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int N;
	cin >> N;
	string s;
	cin >> s;
	int Q;
	cin >> Q;
	vector<pair<int,int>> queries(Q);
	for (int i=0; i<Q; i++) {
		int L, R;
		cin >> L >> R;
		queries[i] = {L-1, R-1};
	}
	vector<int> order(Q);
	iota(order.begin(), order.end(), 0);
	sort(order.begin(), order.end(), [&](int i, int j) -> bool {
		return queries[i].first > queries[j].first;
	});
	int L = N-1;
	set<int> Xs;
	BIT left(N);
	Tree right(N);
	vector<int> ans(Q);
	for (int i=0; i<Q; i++) {
		pair<int,int> q = queries[order[i]];
		while (L >= q.first) {
			if (s[L] == 'C') {
				if (!Xs.empty()) {
					int idx = *Xs.begin();
					left.update(idx+1, -1);
					right.update(0, idx, 1, 0, N-1, -1);
					Xs.erase(idx);
				}
				right.update(0, L, 1, 0, N-1, +1);
			} else {
				Xs.insert(L);
				left.update(L+1, +1);
			}
			L -= 1;
		}
		int R = q.second;
		int base = left.query(R+1);
		int extra = right.query(L+1, R, 1, 0, N-1);
		int next = R == N-1 ? 0 : right.query(R+1, R+1, 1, 0, N-1);
		ans[order[i]] = base - min(0, extra - next);
	}
	for (int i=0; i<Q; i++) {
		cout << ans[i] << "\n";
	}
	return 0;
}

Compilation message

election.cpp: In member function 'void Tree::pushdown(int)':
election.cpp:13:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |   if (x*2 < tags.size()) tags[x*2] += tags[x];
      |       ~~~~^~~~~~~~~~~~~
election.cpp:14:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |   if (x*2+1 < tags.size()) tags[x*2+1] += tags[x];
      |       ~~~~~~^~~~~~~~~~~~~
election.cpp: In member function 'void BIT::update(int, int)':
election.cpp:49:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   while (x < data.size()) {
      |          ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 360 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 360 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 73 ms 5408 KB Output is correct
7 Correct 74 ms 5456 KB Output is correct
8 Correct 69 ms 5200 KB Output is correct
9 Correct 65 ms 5164 KB Output is correct
10 Correct 71 ms 5152 KB Output is correct
11 Correct 76 ms 5832 KB Output is correct
12 Correct 73 ms 5624 KB Output is correct
13 Correct 70 ms 6404 KB Output is correct
14 Correct 71 ms 5616 KB Output is correct
15 Correct 72 ms 5412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 360 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 73 ms 5408 KB Output is correct
7 Correct 74 ms 5456 KB Output is correct
8 Correct 69 ms 5200 KB Output is correct
9 Correct 65 ms 5164 KB Output is correct
10 Correct 71 ms 5152 KB Output is correct
11 Correct 76 ms 5832 KB Output is correct
12 Correct 73 ms 5624 KB Output is correct
13 Correct 70 ms 6404 KB Output is correct
14 Correct 71 ms 5616 KB Output is correct
15 Correct 72 ms 5412 KB Output is correct
16 Correct 676 ms 36024 KB Output is correct
17 Correct 595 ms 35544 KB Output is correct
18 Correct 673 ms 35860 KB Output is correct
19 Correct 607 ms 35300 KB Output is correct
20 Correct 682 ms 35256 KB Output is correct
21 Correct 755 ms 41744 KB Output is correct
22 Correct 719 ms 38088 KB Output is correct
23 Correct 701 ms 43844 KB Output is correct
24 Correct 733 ms 39780 KB Output is correct
25 Correct 649 ms 36836 KB Output is correct