답안 #817810

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
817810 2023-08-09T16:46:07 Z hugo_pm 식물 비교 (IOI20_plants) C++17
100 / 100
938 ms 56516 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int INF = 1e9;
struct ArgMax {
	inline pii op(pii a, pii b) { return max(a, b); }
	const pii e {-INF, -1};
	int N;
	vector<pii> tree, lazy;
	ArgMax(int __n) {
		N = 1;
		while (N < __n) N *= 2;
		tree.assign(2*N, e);
		lazy.assign(2*N, e);
	}
	void update(int i, int x) {
		i += N;
		tree[i] = {x, i-N};
		while (i > 1) {
			i /= 2;
			tree[i] = op(tree[2*i], tree[2*i+1]);
		}
	}
	pii _query(int L, int R) {
		L += N, R += N+1;
		pii res = e;
		while (L < R) {
			if (L & 1) res = op(res, tree[L++]);
			if (R & 1) res = op(res, tree[--R]);
			L /= 2, R /= 2;
		}
		return res;
	}
	int circMax(int L, int R) {
		return (L > R ? max(_query(L, N-1), _query(0, R)) : _query(L, R)).second;
	}
};

struct MinAdd {
	inline int op(int a, int b) { return min(a, b); }
	const int e = INF;
	int N;
	vector<int> tree, lazy;
	void init(vector<int> v) {
		N = 1;
		while (N < (int)v.size()) N *= 2;
		tree.assign(2*N, e);
		lazy.assign(2*N, 0);
		for (int i = 0; i < (int)v.size(); ++i) {
			tree[N+i] = v[i];
		}
		for (int i = N-1; i >= 1; --i) {
			tree[i] = op(tree[2*i], tree[2*i+1]);
		}
	}
	void push(int node) {
		tree[node] += lazy[node];
		if (node < N) {
			lazy[2*node] += lazy[node];
			lazy[2*node+1] += lazy[node];
		}
		lazy[node] = 0;
	}
	void _add(int lq, int rq, int delta) {
		auto F = [&] (auto f, int node, int lno, int rno) -> void {
			push(node);
			if (lq <= lno && rno <= rq) {
				lazy[node] += delta;
				push(node);
			} else if (lno <= rq && lq <= rno) {
				int mid = (lno+rno)/2;
				f(f, 2*node, lno, mid);
				f(f, 2*node+1, mid+1, rno);
				tree[node] = op(tree[2*node], tree[2*node+1]);
			}
		};
		F(F, 1, 0, N-1);
	}
	optional<int> _popZero(int lq, int rq) {
		optional<int> ret = nullopt;
		auto F = [&] (auto f, int node, int lno, int rno) {
			push(node);
			if (ret || tree[node] > 0 || rno < lq || rq < lno) {
				return;
			} else if (node >= N) {
				tree[node] = e;
				ret = node - N;
			} else {
				int mid = (lno+rno)/2;
				f(f, 2*node, lno, mid);
				f(f, 2*node+1, mid+1, rno);
				tree[node] = op(tree[2*node], tree[2*node+1]);
			}
		};
		F(F, 1, 0, N-1);
		return ret;
	}
	optional<int> circleGet(int L, int R) {
		if (L > R) {
			auto a = _popZero(L, N-1);
			return (a ? a : _popZero(0, R));
		}
		return _popZero(L, R);
	}
	void circleDecr(int L, int R) {
		if (L > R) {
			_add(L, N-1, -1);
			_add(0, R, -1);
		} else {
			_add(L, R, -1);
		}
	}
};
MinAdd util;

int N, K;
int add(int x, int y) {
	x += y;
	if (x >= N) x -= N;
	return x;
}
int sub(int x, int y) {
	x -= y;
	if (x < 0) x += N;
	return x;
}
int anti(int x, int y) { return sub(y, x); }

vector<int> relative, height;
int insertHeight;

bool pop(int L, int R) {
	optional<int> ret = util.circleGet(L, R);
	if (!ret) return false;
	for (; ret; ret = util.circleGet(L, R)) {
		int cur = *ret;
		pop(sub(cur,K-1), sub(cur, 1));
		height[cur] = insertHeight--;
		util.circleDecr(sub(cur,K-1), sub(cur,1));
	}
	return true;
}

const int LG = 18;
static_assert((1<<LG) >= 200'000);
vector<vi> lft, rgt;

void init(int _k, std::vector<int> _r) {
	K = _k;
	relative = _r;
	N = _r.size();
	//--
	util.init(relative);
	height.assign(N, -1);
	insertHeight = N-1;
	while(pop(0, N-1));
	dbg(height);
	//-
	lft.assign(LG, vi(N, -1)), rgt.assign(LG, vi(N, -1));
	ArgMax st(N);
	vector<int> order(N);
	iota(all(order), 0);
	sort(all(order), [&] (int i, int j) { return height[i] < height[j]; });
	for (int i : order) {
		lft[0][i] = st.circMax(sub(i,K-1), sub(i,1));
		rgt[0][i] = st.circMax(add(i,1), add(i,K-1));
		st.update(i, height[i]);
	}
	function<int(int, int)> dist = sub;
	auto jmp = &lft;
	rep(_, 0, 2) {
		rep(lvl, 0, LG-1) rep(node, 0, N) {
			int mid = (*jmp)[lvl][node];
			if (mid != -1) {
				int nxt = (*jmp)[lvl][mid];
				if (dist(node,mid)+dist(mid,nxt) < N) {
					(*jmp)[lvl+1][node] = nxt;
				}
			}
		}
		jmp = &rgt, dist = anti;
	}
	dbg(lft[0]);
	dbg(rgt[0]);
}

int cercle(int x, int y) { return min(sub(x,y),sub(y,x)); }
bool superieur(int depart, int dest, vector<vi> &jmp, int (*dist)(int, int)) {
	int cur = depart;
	int iniDist = dist(depart, dest);
	dbg(depart, dest, iniDist);
	for (int lvl = LG-1; lvl >= 0; --lvl) {
		int prop = jmp[lvl][cur];
		if (prop != -1) dbg(lvl, cur, prop);
		if (prop != -1 && dist(depart,cur) + dist(cur,prop) < iniDist) {
			cur = prop;
		}
	}
	return cercle(cur,dest) < K && height[cur] > height[dest];
}
bool sup(int x, int y) {
	return superieur(x, y, lft, sub) || superieur(x, y, rgt, anti);
}
int compare_plants(int x, int y) {
	bool xy = sup(x,y), yx = sup(y,x);
	return xy - yx;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 62 ms 3284 KB Output is correct
7 Correct 118 ms 7992 KB Output is correct
8 Correct 430 ms 50748 KB Output is correct
9 Correct 482 ms 50692 KB Output is correct
10 Correct 535 ms 50756 KB Output is correct
11 Correct 542 ms 50772 KB Output is correct
12 Correct 543 ms 50696 KB Output is correct
13 Correct 704 ms 50696 KB Output is correct
14 Correct 529 ms 50772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 85 ms 4624 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 564 KB Output is correct
10 Correct 82 ms 4488 KB Output is correct
11 Correct 97 ms 4432 KB Output is correct
12 Correct 97 ms 4628 KB Output is correct
13 Correct 78 ms 4576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 85 ms 4624 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 3 ms 564 KB Output is correct
10 Correct 82 ms 4488 KB Output is correct
11 Correct 97 ms 4432 KB Output is correct
12 Correct 97 ms 4628 KB Output is correct
13 Correct 78 ms 4576 KB Output is correct
14 Correct 122 ms 7904 KB Output is correct
15 Correct 647 ms 48344 KB Output is correct
16 Correct 123 ms 7704 KB Output is correct
17 Correct 657 ms 48380 KB Output is correct
18 Correct 767 ms 48364 KB Output is correct
19 Correct 650 ms 48304 KB Output is correct
20 Correct 597 ms 48392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 110 ms 4028 KB Output is correct
4 Correct 789 ms 53328 KB Output is correct
5 Correct 882 ms 48792 KB Output is correct
6 Correct 927 ms 48352 KB Output is correct
7 Correct 795 ms 48380 KB Output is correct
8 Correct 725 ms 48436 KB Output is correct
9 Correct 761 ms 48744 KB Output is correct
10 Correct 746 ms 48244 KB Output is correct
11 Correct 674 ms 48092 KB Output is correct
12 Correct 563 ms 48308 KB Output is correct
13 Correct 728 ms 48296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 19 ms 1232 KB Output is correct
8 Correct 16 ms 1220 KB Output is correct
9 Correct 20 ms 1236 KB Output is correct
10 Correct 16 ms 1236 KB Output is correct
11 Correct 18 ms 1236 KB Output is correct
12 Correct 18 ms 1152 KB Output is correct
13 Correct 19 ms 1160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 2 ms 472 KB Output is correct
6 Correct 543 ms 50084 KB Output is correct
7 Correct 497 ms 50300 KB Output is correct
8 Correct 560 ms 50392 KB Output is correct
9 Correct 508 ms 50660 KB Output is correct
10 Correct 488 ms 50320 KB Output is correct
11 Correct 593 ms 50620 KB Output is correct
12 Correct 512 ms 56096 KB Output is correct
13 Correct 540 ms 50640 KB Output is correct
14 Correct 566 ms 50328 KB Output is correct
15 Correct 559 ms 50468 KB Output is correct
16 Correct 474 ms 49944 KB Output is correct
17 Correct 479 ms 50120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 62 ms 3284 KB Output is correct
7 Correct 118 ms 7992 KB Output is correct
8 Correct 430 ms 50748 KB Output is correct
9 Correct 482 ms 50692 KB Output is correct
10 Correct 535 ms 50756 KB Output is correct
11 Correct 542 ms 50772 KB Output is correct
12 Correct 543 ms 50696 KB Output is correct
13 Correct 704 ms 50696 KB Output is correct
14 Correct 529 ms 50772 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 304 KB Output is correct
20 Correct 3 ms 596 KB Output is correct
21 Correct 85 ms 4624 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 3 ms 564 KB Output is correct
24 Correct 82 ms 4488 KB Output is correct
25 Correct 97 ms 4432 KB Output is correct
26 Correct 97 ms 4628 KB Output is correct
27 Correct 78 ms 4576 KB Output is correct
28 Correct 122 ms 7904 KB Output is correct
29 Correct 647 ms 48344 KB Output is correct
30 Correct 123 ms 7704 KB Output is correct
31 Correct 657 ms 48380 KB Output is correct
32 Correct 767 ms 48364 KB Output is correct
33 Correct 650 ms 48304 KB Output is correct
34 Correct 597 ms 48392 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 300 KB Output is correct
37 Correct 110 ms 4028 KB Output is correct
38 Correct 789 ms 53328 KB Output is correct
39 Correct 882 ms 48792 KB Output is correct
40 Correct 927 ms 48352 KB Output is correct
41 Correct 795 ms 48380 KB Output is correct
42 Correct 725 ms 48436 KB Output is correct
43 Correct 761 ms 48744 KB Output is correct
44 Correct 746 ms 48244 KB Output is correct
45 Correct 674 ms 48092 KB Output is correct
46 Correct 563 ms 48308 KB Output is correct
47 Correct 728 ms 48296 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 19 ms 1232 KB Output is correct
55 Correct 16 ms 1220 KB Output is correct
56 Correct 20 ms 1236 KB Output is correct
57 Correct 16 ms 1236 KB Output is correct
58 Correct 18 ms 1236 KB Output is correct
59 Correct 18 ms 1152 KB Output is correct
60 Correct 19 ms 1160 KB Output is correct
61 Correct 86 ms 5212 KB Output is correct
62 Correct 130 ms 9548 KB Output is correct
63 Correct 540 ms 50776 KB Output is correct
64 Correct 599 ms 50896 KB Output is correct
65 Correct 842 ms 51184 KB Output is correct
66 Correct 728 ms 51352 KB Output is correct
67 Correct 696 ms 51592 KB Output is correct
68 Correct 619 ms 51480 KB Output is correct
69 Correct 812 ms 51424 KB Output is correct
70 Correct 721 ms 56516 KB Output is correct
71 Correct 938 ms 51396 KB Output is correct
72 Correct 823 ms 51200 KB Output is correct
73 Correct 738 ms 51352 KB Output is correct
74 Correct 511 ms 50748 KB Output is correct
75 Correct 620 ms 51064 KB Output is correct