답안 #770977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
770977 2023-07-02T08:40:12 Z SanguineChameleon 식물 비교 (IOI20_plants) C++17
100 / 100
644 ms 84832 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 20;
const int maxk = 20;
const int inf = 1e9 + 20;
int a[maxn];
int tree[maxn * 4];
int lazy[maxn * 4];
long long step_left[maxn][maxk];
long long step_right[maxn][maxk];
int height[maxn];
set<int> all_zeroes, good_zeroes;
int n, k;

void build(int id, int lt, int rt) {
	if (lt == rt) {
		tree[id] = a[lt];
		return;
	}
	int mt = (lt + rt) / 2;
	build(id * 2, lt, mt);
	build(id * 2 + 1, mt + 1, rt);
	tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
}

void update(int id, int lt, int rt, int ql, int qr, int add) {
	if (lt == ql && rt == qr) {
		tree[id] += add;
		lazy[id] += add;
		return;
	}
	tree[id * 2] += lazy[id];
	lazy[id * 2] += lazy[id];
	tree[id * 2 + 1] += lazy[id];
	lazy[id * 2 + 1] += lazy[id];
	lazy[id] = 0;
	int mt = (lt + rt) / 2;
	if (qr <= mt) {
		update(id * 2, lt, mt, ql, qr, add);
	}
	else if (ql >= mt + 1) {
		update(id * 2 + 1, mt + 1, rt, ql, qr, add);
	}
	else {
		update(id * 2, lt, mt, ql, mt, add);
		update(id * 2 + 1, mt + 1, rt, mt + 1, qr, add);
	}
	tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
}

bool contains(int x, int y) {
	if (x < y) {
		return y - x + 1 <= k;
	}
	else {
		return n - (x - y - 1) <= k;
	}
}

void add_zero(int x) {
	auto it = all_zeroes.insert(x).first;
	int prv = (it == all_zeroes.begin() ? *all_zeroes.rbegin() : *prev(it));
	if (!contains(prv, x)) {
		good_zeroes.insert(x);
	} 
	int nxt = (next(it) == all_zeroes.end() ? *all_zeroes.begin() : *next(it));
	if (contains(x, nxt)) {
		good_zeroes.erase(nxt);
	}
	else {
		good_zeroes.insert(nxt);
	}
}

void rem_zero(int x) {
	auto it = all_zeroes.find(x);
	int prv = (it == all_zeroes.begin() ? *all_zeroes.rbegin() : *prev(it));
	int nxt = (next(it) == all_zeroes.end() ? *all_zeroes.begin() : *next(it));
	if (contains(prv, nxt)) {
		good_zeroes.erase(nxt);
	}
	else {
		good_zeroes.insert(nxt);
	}
	int rt = x;
	int lt = (rt + n - (k - 1)) % n;
	if (lt <= rt) {
		update(1, 0, n - 1, lt, rt, -1);
	}
	else {
		update(1, 0, n - 1, lt, n - 1, -1);
		update(1, 0, n - 1, 0, rt, -1);
	}
	all_zeroes.erase(it);
	good_zeroes.erase(x);
}

void find_zeroes(int id, int lt, int rt) {
	if (tree[id] > 0) {
		return;
	}
	if (lt == rt) {
		add_zero(lt);
		tree[id] = inf;
		lazy[id] = 0;
		return;
	}
	tree[id * 2] += lazy[id];
	lazy[id * 2] += lazy[id];
	tree[id * 2 + 1] += lazy[id];
	lazy[id * 2 + 1] += lazy[id];
	lazy[id] = 0;
	int mt = (lt + rt) / 2;
	find_zeroes(id * 2, lt, mt);
	find_zeroes(id * 2 + 1, mt + 1, rt);
	tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
}

int dist_right(int x, int y) {
	return (y + n - x) % n;
}

int dist_left(int x, int y) {
	return (x + n - y) % n;
}

void init(int _k, vector<int> _a) {
	n = _a.size();
	k = _k;
	for (int i = 0; i < n; i++) {
		a[i] = _a[i];
	}
	build(1, 0, n - 1);
	for (int i = n - 1; i >= 0; i--) {
		find_zeroes(1, 0, n - 1);
		height[*good_zeroes.begin()] = i;
		rem_zero(*good_zeroes.begin());
	}
	set<pair<int, int>> heights;
	for (int i = 0; i < k - 1; i++) {
		heights.emplace(height[i], i);
	}
	for (int i = 0; i < n; i++) {
		int prv = (i + n - 1) % n;
		int nxt = (i + k - 1) % n;
		auto it = heights.upper_bound(make_pair(height[prv], -1));
		step_right[prv][0] = (it == heights.end() ? 0 : dist_right(prv, it->second));
		it = heights.upper_bound(make_pair(height[nxt], -1));
		step_left[nxt][0] = (it == heights.end() ? 0 : dist_left(nxt, it->second));
		heights.erase(make_pair(height[i], i));
		heights.emplace(height[nxt], nxt);
	}
	for (int k = 1; k < maxk; k++) {
		for (int i = 0; i < n; i++) {
			step_right[i][k] = step_right[i][k - 1] + step_right[(step_right[i][k - 1] + i) % n][k - 1];
			step_left[i][k] = step_left[i][k - 1] + step_left[(n - step_left[i][k - 1] % n + i) % n][k - 1];
		}
	}
	return;
}

int check_left(int x, int y) {
	int dist = dist_left(x, y);
	for (int k = maxk - 1; k >= 0; k--) {
		if (step_left[x][k] < dist) {
			dist -= step_left[x][k];
			x = (x + n - step_left[x][k] % n) % n;
		}
	}
	return dist < k && height[x] < height[y];
}

int check_right(int x, int y) {
	int dist = dist_right(x, y);
	for (int k = maxk - 1; k >= 0; k--) {
		if (step_right[x][k] < dist) {
			dist -= step_right[x][k];
			x = (x + step_right[x][k]) % n;
		}
	}
	return dist < k && height[x] < height[y];
}

int compare_plants(int x, int y) {
	if (check_left(x, y) || check_right(x, y)) {
		return -1;
	}
	else if (check_left(y, x) || check_right(y, x)) {
		return 1;
	}
	else {
		return 0;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 129 ms 4056 KB Output is correct
7 Correct 175 ms 12712 KB Output is correct
8 Correct 421 ms 80520 KB Output is correct
9 Correct 403 ms 79952 KB Output is correct
10 Correct 409 ms 80008 KB Output is correct
11 Correct 448 ms 80476 KB Output is correct
12 Correct 462 ms 80364 KB Output is correct
13 Correct 512 ms 84832 KB Output is correct
14 Correct 473 ms 75340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 59 ms 5040 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 58 ms 5112 KB Output is correct
11 Correct 154 ms 4940 KB Output is correct
12 Correct 96 ms 5092 KB Output is correct
13 Correct 58 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 59 ms 5040 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 58 ms 5112 KB Output is correct
11 Correct 154 ms 4940 KB Output is correct
12 Correct 96 ms 5092 KB Output is correct
13 Correct 58 ms 5100 KB Output is correct
14 Correct 89 ms 10704 KB Output is correct
15 Correct 577 ms 78316 KB Output is correct
16 Correct 88 ms 10676 KB Output is correct
17 Correct 583 ms 78360 KB Output is correct
18 Correct 607 ms 77132 KB Output is correct
19 Correct 518 ms 77180 KB Output is correct
20 Correct 598 ms 81920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 127 ms 3796 KB Output is correct
4 Correct 557 ms 75660 KB Output is correct
5 Correct 494 ms 73200 KB Output is correct
6 Correct 516 ms 72524 KB Output is correct
7 Correct 579 ms 72984 KB Output is correct
8 Correct 579 ms 76756 KB Output is correct
9 Correct 559 ms 74748 KB Output is correct
10 Correct 514 ms 73264 KB Output is correct
11 Correct 501 ms 81880 KB Output is correct
12 Correct 487 ms 72532 KB Output is correct
13 Correct 593 ms 79192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 4 ms 468 KB Output is correct
7 Correct 34 ms 1300 KB Output is correct
8 Correct 13 ms 1416 KB Output is correct
9 Correct 27 ms 1356 KB Output is correct
10 Correct 14 ms 1312 KB Output is correct
11 Correct 34 ms 1292 KB Output is correct
12 Correct 26 ms 1296 KB Output is correct
13 Correct 12 ms 1340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 484 ms 74820 KB Output is correct
7 Correct 397 ms 74952 KB Output is correct
8 Correct 456 ms 75596 KB Output is correct
9 Correct 527 ms 79464 KB Output is correct
10 Correct 498 ms 77032 KB Output is correct
11 Correct 505 ms 79060 KB Output is correct
12 Correct 437 ms 77704 KB Output is correct
13 Correct 473 ms 75408 KB Output is correct
14 Correct 459 ms 74988 KB Output is correct
15 Correct 435 ms 75492 KB Output is correct
16 Correct 418 ms 76176 KB Output is correct
17 Correct 468 ms 74860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 129 ms 4056 KB Output is correct
7 Correct 175 ms 12712 KB Output is correct
8 Correct 421 ms 80520 KB Output is correct
9 Correct 403 ms 79952 KB Output is correct
10 Correct 409 ms 80008 KB Output is correct
11 Correct 448 ms 80476 KB Output is correct
12 Correct 462 ms 80364 KB Output is correct
13 Correct 512 ms 84832 KB Output is correct
14 Correct 473 ms 75340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 59 ms 5040 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 724 KB Output is correct
24 Correct 58 ms 5112 KB Output is correct
25 Correct 154 ms 4940 KB Output is correct
26 Correct 96 ms 5092 KB Output is correct
27 Correct 58 ms 5100 KB Output is correct
28 Correct 89 ms 10704 KB Output is correct
29 Correct 577 ms 78316 KB Output is correct
30 Correct 88 ms 10676 KB Output is correct
31 Correct 583 ms 78360 KB Output is correct
32 Correct 607 ms 77132 KB Output is correct
33 Correct 518 ms 77180 KB Output is correct
34 Correct 598 ms 81920 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 127 ms 3796 KB Output is correct
38 Correct 557 ms 75660 KB Output is correct
39 Correct 494 ms 73200 KB Output is correct
40 Correct 516 ms 72524 KB Output is correct
41 Correct 579 ms 72984 KB Output is correct
42 Correct 579 ms 76756 KB Output is correct
43 Correct 559 ms 74748 KB Output is correct
44 Correct 514 ms 73264 KB Output is correct
45 Correct 501 ms 81880 KB Output is correct
46 Correct 487 ms 72532 KB Output is correct
47 Correct 593 ms 79192 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 0 ms 300 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 4 ms 468 KB Output is correct
54 Correct 34 ms 1300 KB Output is correct
55 Correct 13 ms 1416 KB Output is correct
56 Correct 27 ms 1356 KB Output is correct
57 Correct 14 ms 1312 KB Output is correct
58 Correct 34 ms 1292 KB Output is correct
59 Correct 26 ms 1296 KB Output is correct
60 Correct 12 ms 1340 KB Output is correct
61 Correct 164 ms 5452 KB Output is correct
62 Correct 197 ms 12492 KB Output is correct
63 Correct 522 ms 76772 KB Output is correct
64 Correct 539 ms 75768 KB Output is correct
65 Correct 500 ms 75756 KB Output is correct
66 Correct 542 ms 76480 KB Output is correct
67 Correct 570 ms 80340 KB Output is correct
68 Correct 606 ms 77752 KB Output is correct
69 Correct 644 ms 79820 KB Output is correct
70 Correct 614 ms 78476 KB Output is correct
71 Correct 597 ms 76512 KB Output is correct
72 Correct 531 ms 75876 KB Output is correct
73 Correct 520 ms 76368 KB Output is correct
74 Correct 513 ms 76660 KB Output is correct
75 Correct 474 ms 75844 KB Output is correct