답안 #301170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
301170 2020-09-17T17:16:25 Z ecnerwala 식물 비교 (IOI20_plants) C++17
100 / 100
410 ms 18580 KB
#include "plants.h"

#include <bits/stdc++.h>

namespace {

class plants_solver {
	struct index_data {
		int l_to_r;
		int l_to_r_2;
		int r_to_l;
		int r_to_l_2;
	};
	std::vector<index_data> v;
public:
	plants_solver() { }
	plants_solver(int N, int K, std::vector<int> R) : v(N) {
		using std::min;
		using std::max;

		assert(N == int(R.size()));
		assert(1 <= K && K <= N);
		for (int& r : R) assert(0 <= r && r < K);

		struct seg_node {
			int val;
			int ind;
			int lazy;
			int last_taken;
		};
		std::vector<seg_node> seg(2*N);
		auto update_node = [&](int a) {
			assert(a < N);
			if (seg[2*a].val < seg[2*a+1].val) {
				seg[a].val = seg[2*a].val;
				seg[a].ind = seg[2*a].ind;
			} else {
				seg[a].val = seg[2*a+1].val;
				seg[a].ind = seg[2*a+1].ind;
			}
			seg[a].val += seg[a].lazy;
		};

		for (int i = 0; i < N; i++) {
			seg[N+i] = seg_node{R[i], i, 0, -1};
		}
		for (int a = N-1; a; a--) {
			update_node(a);
			seg[a].last_taken = -1;
		}

		auto downdate_all = [&](int n) {
			n >>= __builtin_ctz(n); n >>= 1;
			if (!n) return;
			for (int l = 31 - __builtin_clz(n); l >= 0; l--) {
				int a = n >> l;
				assert(a < N);
				seg[2*a].val += seg[a].lazy;
				seg[2*a].lazy += seg[a].lazy;
				seg[2*a+1].val += seg[a].lazy;
				seg[2*a+1].lazy += seg[a].lazy;
				seg[a].lazy = 0;
			}
		};

		std::vector<int> toposort(N); // We'll use the tail of toposort as a stack.
		std::vector<int> prev_bigger(N);
		std::vector<int> next_bigger(N);
		for (int t = 0, stk = N; t < N; t++) {
			if (stk == N) {
				assert(seg[1].val == 0);
				toposort[--stk] = seg[1].ind;
			}

			int cur = toposort[stk];
			//std::cerr << "trying " << cur << '\n';

			int prev_taken = -1;
			int lo = cur-(K-1), hi = cur;
			if (lo < 0) {
				lo += 2 * N, hi = 2 * (N + hi);
			} else {
				lo += N, hi += N;
			}

			downdate_all(lo);
			downdate_all(hi);

			int zero_ind = -1;
			for (int a = lo, b = hi; a < b; a >>= 1, b >>= 1) {
				if (a & 1) {
					if (seg[a].val == 0) {
						zero_ind = seg[a].ind;
						goto found_zero;
					}
					prev_taken = max(prev_taken, seg[a].last_taken);
					a++;
				}
				if (b & 1) {
					--b;
					if (seg[b].val == 0) {
						zero_ind = seg[b].ind;
						goto found_zero;
					}
					prev_taken = max(prev_taken, seg[b].last_taken);
				}
			}
			goto not_found_zero;
found_zero:
			{
				//std::cerr << "bad: " << zero_ind << '\n';
				toposort[--stk] = zero_ind;
				t--;
				continue;
			}
not_found_zero:;
			int next_taken = -1;
			{
				int lo2 = cur+1, hi2 = cur+K;
				assert(lo2 <= N);
				lo2 += N;
				if (hi2 > N) {
					hi2 = 2 * hi2;
				} else {
					hi2 += N;
				}
				for (int a = lo2, b = hi2; a < b; a >>= 1, b >>= 1) {
					if (a & 1) {
						next_taken = max(next_taken, seg[a].last_taken);
						a++;
					}
					if (b & 1) {
						--b;
						next_taken = max(next_taken, seg[b].last_taken);
					}
				}
			}

			assert(toposort[stk] == cur);
			++stk;
			toposort[t] = cur;
			prev_bigger[cur] = (prev_taken == -1) ? -1 : toposort[prev_taken];
			next_bigger[cur] = (next_taken == -1) ? -1 : toposort[next_taken];

			//std::cerr << "take " << cur << ' ' << prev_bigger[cur] << ' ' << next_bigger[cur] << '\n';

			seg[N+cur].last_taken = t;
			seg[N+cur].val += K; // should never be bad
			for (int a = lo, b = hi; a < b; a >>= 1, b >>= 1) {
				if (a & 1) {
					seg[a].lazy--;
					seg[a].val--;
					a++;
				}
				if (b & 1) {
					--b;
					seg[b].lazy--;
					seg[b].val--;
				}
			}
			for (int a = (N+cur) >> 1; a; a >>= 1) { update_node(a); seg[a].last_taken = t; }
			for (int a = lo >> __builtin_ctz(lo) >> 1; a; a >>= 1) update_node(a);
		}

		{
			std::vector<int> pred(2*N+1, -1);
			{
				int cur = 2*N;
				for (int a : toposort) {
					if (a > N-K) {
						pred[cur] = N+a;
						cur = N+a;
					}
				}
			}
			for (int i = 2*N-K; i >= 0; i--) {
				assert(i + K <= 2 * N);
				int j = next_bigger[i >= N ? i-N : i];
				if (j == -1) {
					j = 2 * N;
				} else {
					if (j < i) j += N;
					assert(i < j && j < i + K);
				}
				pred[i] = pred[j];
				pred[j] = i;
			}
			for (int cur = pred[2*N], idx = 2*N-1; idx >= 0; cur = pred[cur], idx--) {
				if (cur >= N) {
					v[cur - N].l_to_r_2 = idx;
				} else {
					v[cur].l_to_r = idx;
				}
			}
		}
		for (int i = 0; i < N; i++) {
			//std::cerr << v[i].l_to_r << ' ' << v[i].l_to_r_2 << '\n';
			assert(v[i].l_to_r > v[i].l_to_r_2);
		}

		{
			std::vector<int> pred(2*N+1, -1);
			{
				int cur = 2 * N;
				for (int a : toposort) {
					if (a < K) {
						pred[cur] = a;
						cur = a;
					}
				}
			}
			for (int i = K; i < 2*N; i++) {
				int j = prev_bigger[i >= N ? i-N : i];
				if (j == -1) {
					j = 2 * N;
				} else {
					if (j + N < i) {
						j += N;
					}
					assert(i - K < j && j < i);
				}
				pred[i] = pred[j];
				pred[j] = i;
			}
			for (int cur = pred[2*N], idx = 2*N-1; idx >= 0; cur = pred[cur], idx--) {
				if (cur >= N) {
					v[cur - N].r_to_l_2 = idx;
				} else {
					v[cur].r_to_l = idx;
				}
			}
		}
		for (int i = 0; i < N; i++) {
			//std::cerr << v[i].r_to_l << ' ' << v[i].r_to_l_2 << '\n';
			assert(v[i].r_to_l_2 > v[i].r_to_l);
		}
	}

public:
	int compare_plants(int x, int y) const {
		if (x == y) return 0;
		if (x > y) {
			return -compare_plants(y, x);
		}
		assert(x < y);
		const auto& a = v[x];
		const auto& b = v[y];
		if (a.l_to_r < b.l_to_r) {
			return -1;
		}
		if (b.l_to_r < a.l_to_r_2) {
			return 1;
		}
		if (a.r_to_l_2 < b.r_to_l) {
			return -1;
		}
		if (b.r_to_l < a.r_to_l) {
			return 1;
		}

		return 0;
	}
} SOLVER;

}

void init(int K, std::vector<int> R) {
	int N = int(R.size());
	SOLVER = plants_solver(N, K, R);
}

int compare_plants(int x, int y) {
	return SOLVER.compare_plants(x, y);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 62 ms 3192 KB Output is correct
7 Correct 87 ms 4216 KB Output is correct
8 Correct 217 ms 18428 KB Output is correct
9 Correct 201 ms 18396 KB Output is correct
10 Correct 198 ms 18376 KB Output is correct
11 Correct 194 ms 18396 KB Output is correct
12 Correct 198 ms 18396 KB Output is correct
13 Correct 201 ms 18396 KB Output is correct
14 Correct 181 ms 18396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 77 ms 3420 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 80 ms 3416 KB Output is correct
11 Correct 74 ms 3288 KB Output is correct
12 Correct 74 ms 3544 KB Output is correct
13 Correct 76 ms 3416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 77 ms 3420 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 80 ms 3416 KB Output is correct
11 Correct 74 ms 3288 KB Output is correct
12 Correct 74 ms 3544 KB Output is correct
13 Correct 76 ms 3416 KB Output is correct
14 Correct 98 ms 4216 KB Output is correct
15 Correct 399 ms 18524 KB Output is correct
16 Correct 100 ms 4216 KB Output is correct
17 Correct 402 ms 18396 KB Output is correct
18 Correct 255 ms 18552 KB Output is correct
19 Correct 232 ms 18396 KB Output is correct
20 Correct 344 ms 18396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 74 ms 3320 KB Output is correct
4 Correct 223 ms 18580 KB Output is correct
5 Correct 252 ms 18396 KB Output is correct
6 Correct 330 ms 18424 KB Output is correct
7 Correct 385 ms 18396 KB Output is correct
8 Correct 410 ms 18424 KB Output is correct
9 Correct 226 ms 18396 KB Output is correct
10 Correct 237 ms 18424 KB Output is correct
11 Correct 198 ms 18376 KB Output is correct
12 Correct 190 ms 18396 KB Output is correct
13 Correct 255 ms 18396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 16 ms 1024 KB Output is correct
8 Correct 17 ms 1024 KB Output is correct
9 Correct 18 ms 1024 KB Output is correct
10 Correct 16 ms 1024 KB Output is correct
11 Correct 16 ms 1024 KB Output is correct
12 Correct 16 ms 1024 KB Output is correct
13 Correct 15 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 251 ms 18492 KB Output is correct
7 Correct 309 ms 18396 KB Output is correct
8 Correct 378 ms 18396 KB Output is correct
9 Correct 401 ms 18396 KB Output is correct
10 Correct 218 ms 18396 KB Output is correct
11 Correct 306 ms 18400 KB Output is correct
12 Correct 202 ms 18400 KB Output is correct
13 Correct 233 ms 18396 KB Output is correct
14 Correct 324 ms 18424 KB Output is correct
15 Correct 376 ms 18424 KB Output is correct
16 Correct 210 ms 18424 KB Output is correct
17 Correct 236 ms 18396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 62 ms 3192 KB Output is correct
7 Correct 87 ms 4216 KB Output is correct
8 Correct 217 ms 18428 KB Output is correct
9 Correct 201 ms 18396 KB Output is correct
10 Correct 198 ms 18376 KB Output is correct
11 Correct 194 ms 18396 KB Output is correct
12 Correct 198 ms 18396 KB Output is correct
13 Correct 201 ms 18396 KB Output is correct
14 Correct 181 ms 18396 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 77 ms 3420 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 80 ms 3416 KB Output is correct
25 Correct 74 ms 3288 KB Output is correct
26 Correct 74 ms 3544 KB Output is correct
27 Correct 76 ms 3416 KB Output is correct
28 Correct 98 ms 4216 KB Output is correct
29 Correct 399 ms 18524 KB Output is correct
30 Correct 100 ms 4216 KB Output is correct
31 Correct 402 ms 18396 KB Output is correct
32 Correct 255 ms 18552 KB Output is correct
33 Correct 232 ms 18396 KB Output is correct
34 Correct 344 ms 18396 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 74 ms 3320 KB Output is correct
38 Correct 223 ms 18580 KB Output is correct
39 Correct 252 ms 18396 KB Output is correct
40 Correct 330 ms 18424 KB Output is correct
41 Correct 385 ms 18396 KB Output is correct
42 Correct 410 ms 18424 KB Output is correct
43 Correct 226 ms 18396 KB Output is correct
44 Correct 237 ms 18424 KB Output is correct
45 Correct 198 ms 18376 KB Output is correct
46 Correct 190 ms 18396 KB Output is correct
47 Correct 255 ms 18396 KB Output is correct
48 Correct 0 ms 256 KB Output is correct
49 Correct 0 ms 256 KB Output is correct
50 Correct 0 ms 256 KB Output is correct
51 Correct 1 ms 256 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 2 ms 384 KB Output is correct
54 Correct 16 ms 1024 KB Output is correct
55 Correct 17 ms 1024 KB Output is correct
56 Correct 18 ms 1024 KB Output is correct
57 Correct 16 ms 1024 KB Output is correct
58 Correct 16 ms 1024 KB Output is correct
59 Correct 16 ms 1024 KB Output is correct
60 Correct 15 ms 1024 KB Output is correct
61 Correct 69 ms 3320 KB Output is correct
62 Correct 87 ms 4216 KB Output is correct
63 Correct 221 ms 18424 KB Output is correct
64 Correct 248 ms 18424 KB Output is correct
65 Correct 321 ms 18396 KB Output is correct
66 Correct 378 ms 18424 KB Output is correct
67 Correct 402 ms 18396 KB Output is correct
68 Correct 232 ms 18396 KB Output is correct
69 Correct 309 ms 18396 KB Output is correct
70 Correct 220 ms 18424 KB Output is correct
71 Correct 247 ms 18396 KB Output is correct
72 Correct 334 ms 18524 KB Output is correct
73 Correct 379 ms 18360 KB Output is correct
74 Correct 229 ms 18396 KB Output is correct
75 Correct 245 ms 18496 KB Output is correct