답안 #425695

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
425695 2021-06-13T10:15:37 Z Mlxa 식물 비교 (IOI20_plants) C++14
100 / 100
1985 ms 62312 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#include "plants.h"
#define cerr if (0) cout

namespace my {

const int L = 18;
const int N = 1 << L;
int n, k, kth[N], can[N];

struct tree_t {
	pair<int, int> mi[2 * N];
	int pu[2 * N];
	int ls(int v) {
		return 2 * v + 1;
	}
	int rs(int v) {
		return 2 * v + 2;
	}
	void build(int v, int l, int r) {
		if (r - l == 1) {
			mi[v] = mp(kth[l], l);
		} else {
			int m = (l + r) / 2;
			build(ls(v), l, m);
			build(rs(v), m, r);
			mi[v] = min(mi[ls(v)], mi[rs(v)]);
		}
	}
	void build() {
		build(0, 0, N);
	}
	void push(int v) {
		mi[ls(v)].x += pu[v];
		mi[rs(v)].x += pu[v];
		pu[ls(v)] += pu[v];
		pu[rs(v)] += pu[v];
		pu[v] = 0;
	}
	void upd(int v, int vl, int vr, int ql, int qr, int d) {
		if (ql <= vl && vr <= qr) {
			mi[v].x += d;
			pu[v] += d;
			return;
		}
		if (qr <= vl || vr <= ql) {
			return;
		}
		push(v);
		int vm = (vl + vr) / 2;
		upd(ls(v), vl, vm, ql, qr, d);
		upd(rs(v), vm, vr, ql, qr, d);
		mi[v] = min(mi[ls(v)], mi[rs(v)]);
	}
	void range_add(int l, int r, int value) {
		upd(0, 0, N, l, r, value);
	}
	void cycle_add(int from, int value) {
		from %= n; from += n; from %= n;
		if (from + k <= n) {
			range_add(from, from + k, value);
		} else {
			range_add(from, n, value), range_add(0, from + k - n, value);
		}
	}
	pair<int, int> query(int v, int vl, int vr, int ql, int qr) {
		if (ql <= vl && vr <= qr) {
			return mi[v];
		}
		if (qr <= vl || vr <= ql) {
			return mp(N, -1);
		}
		int vm = (vl + vr) / 2;
		push(v);
		return min(query(ls(v), vl, vm, ql, qr), query(rs(v), vm, vr, ql, qr));
	}
	pair<int, int> range_min(int l, int r) {
		return query(0, 0, N, l, r);
	}
	pair<int, int> cycle_min(int from) {
		from %= n; from += n; from %= n;
		if (from + k <= n) {
			return range_min(from, from + k);
		} else {
			return min(range_min(from, n), range_min(0, from + k - n));
		}
	}
} tree;

vector<int> topsort;

void extract(int v) {
	tree.range_add(v, v + 1, N);
	while (1) {
		auto e = tree.cycle_min(v - k + 1);
		if (e.x) break;
		extract(e.y);
	}
	tree.cycle_add(v - k + 1, -1);
	topsort.push_back(v);
}

const int BAD = -1;
int lef[L][N], rig[L][N];
}

bool btw(int l, int r, int w) {
	if (l <= r) {
		return l <= w && w <= r;
	} else {
		return l <= w || w <= r;
	}
}

void init(int _k, vector<int> _r) {
	using namespace my;
	n = (int)_r.size();
	k = _k;
	copy(all(_r), kth);
	tree.build();
	while (1) {
		auto e = tree.range_min(0, n);
		if (e.x) break;
		extract(e.y);
	}
	assert((int)topsort.size() == n);
	reverse(all(topsort));
	for (int i = 0; i < n; ++i) {
		can[topsort[i]] = i;
	}
	set<pair<int, int>> iset;
	for (int i = 0; i < k - 1; ++i) {
		iset.emplace(can[i], i);
	}
	fill_n(lef[0], L * N, BAD), fill_n(rig[0], L * N, BAD);
	for (int i = k - 1;;) {
		auto it = iset.lower_bound(mp(can[i], i));
		if (it != iset.begin()) {
			--it;
			lef[0][i] = it->y;
		}
		iset.erase({can[(i + n - k + 1) % n], (i + n - k + 1) % n});
		iset.emplace(can[i], i);
		(i += 1) %= n;
		if (i == k - 1) break;
	}
	iset.clear();
	for (int i = 0; i < k - 1; ++i) {
		iset.emplace(can[i], i);
	}
	for (int i = n - 1; i >= 0; --i) {
		auto it = iset.lower_bound(mp(can[i], i));
		if (it != iset.begin()) {
			--it;
			rig[0][i] = it->y;
		}
		iset.erase({can[(i + k - 1) % n], (i + k - 1) % n});
		iset.emplace(can[i], i);
	}
	for (int i = 0; i < n; ++i) {
		cerr << i << " " << lef[0][i] << " " << rig[0][i] << endl;
	}
	for (int i = 0; i < n; ++i) {
		cerr << can[i] << " ";
	}
	cerr << endl;
	for (int it = 1; it < L; ++it) {
		for (int i = 0; i < n; ++i) {
			if (lef[it - 1][i] != BAD) {
				int j = lef[it - 1][i];
				if (j == BAD || btw(lef[it - 1][j], j, i)) {
					continue;
				}
				lef[it][i] = lef[it - 1][j];
			}
		}
	}
	for (int it = 1; it < L; ++it) {
		for (int i = 0; i < n; ++i) {
			if (rig[it - 1][i] != BAD) {
				int j = rig[it - 1][i];
				if (j == BAD || btw(j, rig[it - 1][j], i)) {
					continue;
				}
				rig[it][i] = rig[it - 1][j];
			}
		}
	}
}

bool taller(int x, int y) {
	using namespace my;
	cerr << "taller " << x << " " << y << endl;
	int z = x;
	for (int it = L - 1; it >= 0; --it) {
		int v = lef[it][z];
		cerr << it << " " << z << " " << v << endl;
		if (v == BAD || btw(v, z, y)) {
			continue;
		}
		z = v;
	}
	cerr << "z = " << z << " " << y << endl;
	assert(lef[0][z] == BAD || btw(lef[0][z], z, y));
	if (lef[0][z] != BAD && btw(lef[0][z], z, y) && can[z] >= can[y]) {
		return true;
	}
	z = x;
	for (int it = L - 1; it >= 0; --it) {
		int v = rig[it][z];
		if (v == BAD || btw(z, v, y)) {
			continue;
		}
		z = v;
	}
	assert(rig[0][z] == BAD || btw(z, rig[0][z], y));
	cerr << "z = " << z << endl;
	if (rig[0][z] != BAD && btw(z, rig[0][z], y) && can[z] >= can[y]) {
		return true;
	}
	return false;
}

int compare_plants(int x, int y) {
	if (taller(x, y)) {
		return 1;
	} else if (taller(y, x)) {
		return -1;
	} else {
		return 0;
	}
}

#ifdef LC
#include "grader.cpp"
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 41292 KB Output is correct
2 Correct 29 ms 41292 KB Output is correct
3 Correct 26 ms 41288 KB Output is correct
4 Correct 28 ms 41292 KB Output is correct
5 Correct 25 ms 41308 KB Output is correct
6 Correct 122 ms 44100 KB Output is correct
7 Correct 242 ms 44772 KB Output is correct
8 Correct 615 ms 49368 KB Output is correct
9 Correct 707 ms 52152 KB Output is correct
10 Correct 733 ms 52152 KB Output is correct
11 Correct 744 ms 52272 KB Output is correct
12 Correct 767 ms 52236 KB Output is correct
13 Correct 699 ms 52152 KB Output is correct
14 Correct 985 ms 52152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 41324 KB Output is correct
2 Correct 25 ms 41324 KB Output is correct
3 Correct 29 ms 41360 KB Output is correct
4 Correct 30 ms 41292 KB Output is correct
5 Correct 32 ms 41396 KB Output is correct
6 Correct 34 ms 41436 KB Output is correct
7 Correct 176 ms 44484 KB Output is correct
8 Correct 28 ms 41472 KB Output is correct
9 Correct 31 ms 41420 KB Output is correct
10 Correct 218 ms 44432 KB Output is correct
11 Correct 175 ms 44368 KB Output is correct
12 Correct 210 ms 44496 KB Output is correct
13 Correct 161 ms 44484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 41324 KB Output is correct
2 Correct 25 ms 41324 KB Output is correct
3 Correct 29 ms 41360 KB Output is correct
4 Correct 30 ms 41292 KB Output is correct
5 Correct 32 ms 41396 KB Output is correct
6 Correct 34 ms 41436 KB Output is correct
7 Correct 176 ms 44484 KB Output is correct
8 Correct 28 ms 41472 KB Output is correct
9 Correct 31 ms 41420 KB Output is correct
10 Correct 218 ms 44432 KB Output is correct
11 Correct 175 ms 44368 KB Output is correct
12 Correct 210 ms 44496 KB Output is correct
13 Correct 161 ms 44484 KB Output is correct
14 Correct 265 ms 45136 KB Output is correct
15 Correct 1985 ms 55004 KB Output is correct
16 Correct 327 ms 47404 KB Output is correct
17 Correct 1661 ms 58744 KB Output is correct
18 Correct 1120 ms 57200 KB Output is correct
19 Correct 1238 ms 57712 KB Output is correct
20 Correct 1652 ms 62312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 41260 KB Output is correct
2 Correct 30 ms 41332 KB Output is correct
3 Correct 192 ms 44196 KB Output is correct
4 Correct 1081 ms 51536 KB Output is correct
5 Correct 1140 ms 52544 KB Output is correct
6 Correct 1214 ms 52632 KB Output is correct
7 Correct 1205 ms 53112 KB Output is correct
8 Correct 1288 ms 57096 KB Output is correct
9 Correct 971 ms 52512 KB Output is correct
10 Correct 970 ms 52316 KB Output is correct
11 Correct 797 ms 52172 KB Output is correct
12 Correct 899 ms 52316 KB Output is correct
13 Correct 1024 ms 55156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 41292 KB Output is correct
2 Correct 25 ms 41300 KB Output is correct
3 Correct 28 ms 41380 KB Output is correct
4 Correct 25 ms 41292 KB Output is correct
5 Correct 25 ms 41296 KB Output is correct
6 Correct 27 ms 41416 KB Output is correct
7 Correct 52 ms 41924 KB Output is correct
8 Correct 52 ms 41920 KB Output is correct
9 Correct 54 ms 41896 KB Output is correct
10 Correct 45 ms 41988 KB Output is correct
11 Correct 52 ms 41924 KB Output is correct
12 Correct 50 ms 42024 KB Output is correct
13 Correct 45 ms 41956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 41304 KB Output is correct
2 Correct 25 ms 41328 KB Output is correct
3 Correct 25 ms 41252 KB Output is correct
4 Correct 32 ms 41276 KB Output is correct
5 Correct 28 ms 41420 KB Output is correct
6 Correct 887 ms 49328 KB Output is correct
7 Correct 932 ms 51880 KB Output is correct
8 Correct 874 ms 52176 KB Output is correct
9 Correct 1284 ms 56256 KB Output is correct
10 Correct 816 ms 51628 KB Output is correct
11 Correct 961 ms 55668 KB Output is correct
12 Correct 758 ms 54124 KB Output is correct
13 Correct 910 ms 51676 KB Output is correct
14 Correct 843 ms 51808 KB Output is correct
15 Correct 995 ms 52188 KB Output is correct
16 Correct 645 ms 51404 KB Output is correct
17 Correct 758 ms 51592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 41292 KB Output is correct
2 Correct 29 ms 41292 KB Output is correct
3 Correct 26 ms 41288 KB Output is correct
4 Correct 28 ms 41292 KB Output is correct
5 Correct 25 ms 41308 KB Output is correct
6 Correct 122 ms 44100 KB Output is correct
7 Correct 242 ms 44772 KB Output is correct
8 Correct 615 ms 49368 KB Output is correct
9 Correct 707 ms 52152 KB Output is correct
10 Correct 733 ms 52152 KB Output is correct
11 Correct 744 ms 52272 KB Output is correct
12 Correct 767 ms 52236 KB Output is correct
13 Correct 699 ms 52152 KB Output is correct
14 Correct 985 ms 52152 KB Output is correct
15 Correct 28 ms 41324 KB Output is correct
16 Correct 25 ms 41324 KB Output is correct
17 Correct 29 ms 41360 KB Output is correct
18 Correct 30 ms 41292 KB Output is correct
19 Correct 32 ms 41396 KB Output is correct
20 Correct 34 ms 41436 KB Output is correct
21 Correct 176 ms 44484 KB Output is correct
22 Correct 28 ms 41472 KB Output is correct
23 Correct 31 ms 41420 KB Output is correct
24 Correct 218 ms 44432 KB Output is correct
25 Correct 175 ms 44368 KB Output is correct
26 Correct 210 ms 44496 KB Output is correct
27 Correct 161 ms 44484 KB Output is correct
28 Correct 265 ms 45136 KB Output is correct
29 Correct 1985 ms 55004 KB Output is correct
30 Correct 327 ms 47404 KB Output is correct
31 Correct 1661 ms 58744 KB Output is correct
32 Correct 1120 ms 57200 KB Output is correct
33 Correct 1238 ms 57712 KB Output is correct
34 Correct 1652 ms 62312 KB Output is correct
35 Correct 28 ms 41260 KB Output is correct
36 Correct 30 ms 41332 KB Output is correct
37 Correct 192 ms 44196 KB Output is correct
38 Correct 1081 ms 51536 KB Output is correct
39 Correct 1140 ms 52544 KB Output is correct
40 Correct 1214 ms 52632 KB Output is correct
41 Correct 1205 ms 53112 KB Output is correct
42 Correct 1288 ms 57096 KB Output is correct
43 Correct 971 ms 52512 KB Output is correct
44 Correct 970 ms 52316 KB Output is correct
45 Correct 797 ms 52172 KB Output is correct
46 Correct 899 ms 52316 KB Output is correct
47 Correct 1024 ms 55156 KB Output is correct
48 Correct 24 ms 41292 KB Output is correct
49 Correct 25 ms 41300 KB Output is correct
50 Correct 28 ms 41380 KB Output is correct
51 Correct 25 ms 41292 KB Output is correct
52 Correct 25 ms 41296 KB Output is correct
53 Correct 27 ms 41416 KB Output is correct
54 Correct 52 ms 41924 KB Output is correct
55 Correct 52 ms 41920 KB Output is correct
56 Correct 54 ms 41896 KB Output is correct
57 Correct 45 ms 41988 KB Output is correct
58 Correct 52 ms 41924 KB Output is correct
59 Correct 50 ms 42024 KB Output is correct
60 Correct 45 ms 41956 KB Output is correct
61 Correct 199 ms 45916 KB Output is correct
62 Correct 284 ms 47012 KB Output is correct
63 Correct 883 ms 52168 KB Output is correct
64 Correct 1092 ms 52388 KB Output is correct
65 Correct 1304 ms 52628 KB Output is correct
66 Correct 1148 ms 53260 KB Output is correct
67 Correct 1319 ms 57096 KB Output is correct
68 Correct 915 ms 52528 KB Output is correct
69 Correct 1170 ms 56616 KB Output is correct
70 Correct 1012 ms 54816 KB Output is correct
71 Correct 1377 ms 52572 KB Output is correct
72 Correct 1229 ms 52660 KB Output is correct
73 Correct 1190 ms 53304 KB Output is correct
74 Correct 965 ms 52416 KB Output is correct
75 Correct 1002 ms 52520 KB Output is correct