Submission #300151

# Submission time Handle Problem Language Result Execution time Memory
300151 2020-09-16T21:47:04 Z model_code Comparing Plants (IOI20_plants) C++17
100 / 100
1253 ms 83704 KB
// plants-original-ac
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<set>
#define N_ 251000
#define SZ 262144
#include <vector>
using namespace std;
int n, K, Rank[N_], w[N_], V[N_];
struct Tree {
	int K[SZ + SZ], Mn[SZ + SZ];
	void UDT(int nd) {
		Mn[nd] = min(Mn[nd * 2], Mn[nd * 2 + 1]);
	}
	void init(int nd, int b, int e) {
		K[nd] = 0;
		if (b == e) {
			Mn[nd] = Rank[b] - 1;
			return;
		}
		int m = (b + e) >> 1;
		init(nd * 2, b, m);
		init(nd * 2 + 1, m + 1, e);
		UDT(nd);
	}
	void Add2(int nd, int x) {
		K[nd] += x;
		Mn[nd] += x;
	}
	void Spread(int nd) {
		Add2(nd * 2, K[nd]);
		Add2(nd * 2 + 1, K[nd]);
		K[nd] = 0;
	}
	void Add(int nd, int b, int e, int s, int l, int x) {
		if (s > l)return;
		if (s <= b && e <= l) {
			Add2(nd, x);
			return;
		}
		Spread(nd);
		int m = (b + e) >> 1;
		if (s <= m)Add(nd * 2, b, m, s, l, x);
		if (l > m)Add(nd * 2 + 1, m + 1, e, s, l, x);
		UDT(nd);
	}
	void Put(int nd, int b, int e, int x, int y) {
		if (b == e) {
			Mn[nd] = y;
			return;
		}
		Spread(nd);
		int m = (b + e) >> 1;
		if (x <= m)Put(nd * 2, b, m, x, y);
		else Put(nd * 2 + 1, m + 1, e, x, y);
		UDT(nd);
	}
	int FZ(int nd, int b, int e) {
		if (b == e)return b;
		Spread(nd);
		int m = (b + e) >> 1;
		if (!Mn[nd * 2])return FZ(nd * 2, b, m);
		return FZ(nd * 2 + 1, m + 1, e);
	}
	int FindZero() {
		if (Mn[1])return 0;
		return FZ(1, 1, n);
	}
}IT;
set<int>Set, Good;
void Ins(int a) {
	Set.insert(a);
	auto it = Set.find(a);
	int nxt, prv;
	if (next(it) != Set.end())nxt = *(next(it));
	else nxt = (*Set.begin()) + n;
	if (it != Set.begin())prv = *(prev(it));
	else prv = (*prev(Set.end())) - n;
	if (nxt - a >= K) {
		int t = (nxt - 1) % n + 1;
		Good.insert(t);
	}
	else {
		int t = (nxt - 1) % n + 1;
		if (Good.find(t) != Good.end()) Good.erase(t);
	}
	if (a - prv >= K) {
		Good.insert(a);
	}
}
void Del(int a) {
	auto it = Set.find(a);
	int nxt, prv;
	if (next(it) != Set.end())nxt = *(next(it));
	else nxt = (*Set.begin()) + n;
	if (it != Set.begin())prv = *(prev(it));
	else prv = (*prev(Set.end())) - n;
	Set.erase(a);

	if (Good.find(a) != Good.end())Good.erase(a);

	if (!Set.empty() && nxt - prv >= K) {
		Good.insert((nxt - 1) % n + 1);
	}
}


int BIT[N_];
void Add(int a, int b) {
	while (a < N_) {
		BIT[a] += b;
		a += (a&-a);
	}
}
int Sum(int a) {
	int r = 0;
	while (a) {
		r += BIT[a];
		a -= (a&-a);
	}
	return r;
}

long long Right[N_][20], Left[N_][20], INF = 1e9;

bool RightPath(int a, int b) {
	int d = (b - a + n) % n;
	int i;
	for (i = 17; i >= 0; i--) {
		if (d >= Right[a][i]) {
			d -= Right[a][i];
			a = (a + Right[a][i] - 1) % n + 1;
		}
	}
	if ((b - a + n) % n < K && V[a] <= V[b])return true;
	return false;
}


bool LeftPath(int a, int b) {
	int d = (a - b + n) % n;
	int i;
	for (i = 17; i >= 0; i--) {
		if (d >= Left[a][i]) {
			d -= Left[a][i];
			a = (a - Left[a][i] - 1 + n) % n + 1;
		}
	}
	if ((a - b + n) % n < K && V[a] <= V[b])return true;
	return false;
}

bool Path(int a, int b) {
	return RightPath(a, b) || LeftPath(a, b);
}

int compare_plants(int a, int b) {
	a++;
	b++;
	if (Path(a, b)) return 1;
	else if (Path(b, a)) return -1;
	else return 0;
}

void Do() {
	int i, j;
	for (i = 0; i < N_; i++)BIT[i] = 0;
	for (i = 1; i <= K; i++) {
		Add(V[i], 1);
	}

	for (i = 1; i <= n; i++) {

		int b = V[i] + 1, e = n, mid, r = 0, s = Sum(V[i]);
		while (b <= e) {
			mid = (b + e) >> 1;
			if (Sum(mid) > s) {
				r = mid;
				e = mid - 1;
			}
			else b = mid + 1;
		}
		if (r) {
			r = w[r];
			Right[i][0] = (r - i + n) % n;
		}
		else Right[i][0] = INF;

		Add(V[i], -1);
		Add(V[(i + K - 1) % n + 1], 1);
	}

	for (i = 0; i < N_; i++)BIT[i] = 0;

	for (i = n; i > n - K; i--) {
		Add(V[i], 1);
	}
	for (i = n; i >= 1; i--) {


		int b = V[i] + 1, e = n, mid, r = 0, s = Sum(V[i]);
		while (b <= e) {
			mid = (b + e) >> 1;
			if (Sum(mid) > s) {
				r = mid;
				e = mid - 1;
			}
			else b = mid + 1;
		}
		if (r) {
			r = w[r];
			Left[i][0] = (i - r + n) % n;
		}
		else Left[i][0] = INF;

		Add(V[i], -1);
		Add(V[(i - K + n - 1) % n + 1], 1);
	}
	for (i = 0; i < 18; i++) {
		for (j = 1; j <= n; j++) {
			if (Right[j][i] >= INF) {
				Right[j][i + 1] = INF;
			}
			else {
				int t = (j + Right[j][i] - 1) % n + 1;
				Right[j][i + 1] = min(INF, Right[t][i] + Right[j][i]);
			}
			if (Left[j][i] >= INF) {
				Left[j][i + 1] = INF;
			}
			else {
				int t = (j - Left[j][i] % n - 1 + n) % n + 1;
				Left[j][i + 1] = min(INF, Left[t][i] + Left[j][i]);
			}
		}
	}
}

void init(int k, vector<int> r) {
	n = r.size();
	K = k;
	int i;
	for(i=0; i<n; i++) {
		Rank[i+1] = r[i]+1;
	}
	IT.init(1, 1, n);
	for (i = 1; i <= n; i++) {

		int t;
		while ((t = IT.FindZero())) {
			Ins(t);
			IT.Put(1, 1, n, t, INF);
		}
		assert(!Good.empty());
		int a = *Good.begin();
		w[i] = a;
		V[a] = i;
		IT.Add(1, 1, n, max(a - K + 1, 1), a - 1, -1);
		IT.Add(1, 1, n, a - K + 1 + n, min(a - 1 + n, n), -1);
		Del(a);
	}
	for (i = 1; i <= K; i++) Add(V[i], 1);
	for (i = 1; i <= n; i++) {
		assert(Sum(V[i]) == Rank[i]);
		Add(V[i], -1);
		Add(V[(i + K - 1) % n + 1], 1);
	}
	Do();
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1536 KB Output is correct
3 Correct 1 ms 1280 KB Output is correct
4 Correct 1 ms 1280 KB Output is correct
5 Correct 1 ms 1408 KB Output is correct
6 Correct 97 ms 4216 KB Output is correct
7 Correct 220 ms 11536 KB Output is correct
8 Correct 678 ms 79608 KB Output is correct
9 Correct 726 ms 78840 KB Output is correct
10 Correct 776 ms 78968 KB Output is correct
11 Correct 814 ms 79608 KB Output is correct
12 Correct 832 ms 79324 KB Output is correct
13 Correct 840 ms 83652 KB Output is correct
14 Correct 874 ms 74312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1312 KB Output is correct
3 Correct 1 ms 1408 KB Output is correct
4 Correct 2 ms 1280 KB Output is correct
5 Correct 1 ms 1408 KB Output is correct
6 Correct 5 ms 1792 KB Output is correct
7 Correct 106 ms 6008 KB Output is correct
8 Correct 5 ms 1408 KB Output is correct
9 Correct 5 ms 1792 KB Output is correct
10 Correct 104 ms 5956 KB Output is correct
11 Correct 114 ms 6008 KB Output is correct
12 Correct 159 ms 6136 KB Output is correct
13 Correct 104 ms 6008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1312 KB Output is correct
3 Correct 1 ms 1408 KB Output is correct
4 Correct 2 ms 1280 KB Output is correct
5 Correct 1 ms 1408 KB Output is correct
6 Correct 5 ms 1792 KB Output is correct
7 Correct 106 ms 6008 KB Output is correct
8 Correct 5 ms 1408 KB Output is correct
9 Correct 5 ms 1792 KB Output is correct
10 Correct 104 ms 5956 KB Output is correct
11 Correct 114 ms 6008 KB Output is correct
12 Correct 159 ms 6136 KB Output is correct
13 Correct 104 ms 6008 KB Output is correct
14 Correct 162 ms 11384 KB Output is correct
15 Correct 868 ms 74492 KB Output is correct
16 Correct 164 ms 11384 KB Output is correct
17 Correct 875 ms 74488 KB Output is correct
18 Correct 889 ms 78968 KB Output is correct
19 Correct 912 ms 74488 KB Output is correct
20 Correct 841 ms 74360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1280 KB Output is correct
3 Correct 121 ms 4856 KB Output is correct
4 Correct 937 ms 77560 KB Output is correct
5 Correct 959 ms 75000 KB Output is correct
6 Correct 1080 ms 74448 KB Output is correct
7 Correct 952 ms 74424 KB Output is correct
8 Correct 906 ms 74256 KB Output is correct
9 Correct 983 ms 76616 KB Output is correct
10 Correct 1181 ms 75108 KB Output is correct
11 Correct 905 ms 83704 KB Output is correct
12 Correct 941 ms 74352 KB Output is correct
13 Correct 1059 ms 80888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1280 KB Output is correct
3 Correct 2 ms 1280 KB Output is correct
4 Correct 1 ms 1280 KB Output is correct
5 Correct 1 ms 1280 KB Output is correct
6 Correct 5 ms 1440 KB Output is correct
7 Correct 30 ms 2080 KB Output is correct
8 Correct 29 ms 2040 KB Output is correct
9 Correct 57 ms 2144 KB Output is correct
10 Correct 22 ms 2048 KB Output is correct
11 Correct 27 ms 2080 KB Output is correct
12 Correct 27 ms 2048 KB Output is correct
13 Correct 21 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 1 ms 1280 KB Output is correct
3 Correct 1 ms 1280 KB Output is correct
4 Correct 1 ms 1312 KB Output is correct
5 Correct 4 ms 1664 KB Output is correct
6 Correct 816 ms 74536 KB Output is correct
7 Correct 816 ms 74436 KB Output is correct
8 Correct 810 ms 74360 KB Output is correct
9 Correct 928 ms 74408 KB Output is correct
10 Correct 776 ms 76644 KB Output is correct
11 Correct 852 ms 74808 KB Output is correct
12 Correct 927 ms 77372 KB Output is correct
13 Correct 941 ms 75000 KB Output is correct
14 Correct 838 ms 74360 KB Output is correct
15 Correct 927 ms 74304 KB Output is correct
16 Correct 889 ms 75896 KB Output is correct
17 Correct 789 ms 74592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1536 KB Output is correct
3 Correct 1 ms 1280 KB Output is correct
4 Correct 1 ms 1280 KB Output is correct
5 Correct 1 ms 1408 KB Output is correct
6 Correct 97 ms 4216 KB Output is correct
7 Correct 220 ms 11536 KB Output is correct
8 Correct 678 ms 79608 KB Output is correct
9 Correct 726 ms 78840 KB Output is correct
10 Correct 776 ms 78968 KB Output is correct
11 Correct 814 ms 79608 KB Output is correct
12 Correct 832 ms 79324 KB Output is correct
13 Correct 840 ms 83652 KB Output is correct
14 Correct 874 ms 74312 KB Output is correct
15 Correct 1 ms 1280 KB Output is correct
16 Correct 1 ms 1312 KB Output is correct
17 Correct 1 ms 1408 KB Output is correct
18 Correct 2 ms 1280 KB Output is correct
19 Correct 1 ms 1408 KB Output is correct
20 Correct 5 ms 1792 KB Output is correct
21 Correct 106 ms 6008 KB Output is correct
22 Correct 5 ms 1408 KB Output is correct
23 Correct 5 ms 1792 KB Output is correct
24 Correct 104 ms 5956 KB Output is correct
25 Correct 114 ms 6008 KB Output is correct
26 Correct 159 ms 6136 KB Output is correct
27 Correct 104 ms 6008 KB Output is correct
28 Correct 162 ms 11384 KB Output is correct
29 Correct 868 ms 74492 KB Output is correct
30 Correct 164 ms 11384 KB Output is correct
31 Correct 875 ms 74488 KB Output is correct
32 Correct 889 ms 78968 KB Output is correct
33 Correct 912 ms 74488 KB Output is correct
34 Correct 841 ms 74360 KB Output is correct
35 Correct 1 ms 1280 KB Output is correct
36 Correct 1 ms 1280 KB Output is correct
37 Correct 121 ms 4856 KB Output is correct
38 Correct 937 ms 77560 KB Output is correct
39 Correct 959 ms 75000 KB Output is correct
40 Correct 1080 ms 74448 KB Output is correct
41 Correct 952 ms 74424 KB Output is correct
42 Correct 906 ms 74256 KB Output is correct
43 Correct 983 ms 76616 KB Output is correct
44 Correct 1181 ms 75108 KB Output is correct
45 Correct 905 ms 83704 KB Output is correct
46 Correct 941 ms 74352 KB Output is correct
47 Correct 1059 ms 80888 KB Output is correct
48 Correct 1 ms 1280 KB Output is correct
49 Correct 1 ms 1280 KB Output is correct
50 Correct 2 ms 1280 KB Output is correct
51 Correct 1 ms 1280 KB Output is correct
52 Correct 1 ms 1280 KB Output is correct
53 Correct 5 ms 1440 KB Output is correct
54 Correct 30 ms 2080 KB Output is correct
55 Correct 29 ms 2040 KB Output is correct
56 Correct 57 ms 2144 KB Output is correct
57 Correct 22 ms 2048 KB Output is correct
58 Correct 27 ms 2080 KB Output is correct
59 Correct 27 ms 2048 KB Output is correct
60 Correct 21 ms 2048 KB Output is correct
61 Correct 125 ms 4856 KB Output is correct
62 Correct 248 ms 11300 KB Output is correct
63 Correct 850 ms 75768 KB Output is correct
64 Correct 965 ms 74520 KB Output is correct
65 Correct 1222 ms 74360 KB Output is correct
66 Correct 1084 ms 74284 KB Output is correct
67 Correct 998 ms 74268 KB Output is correct
68 Correct 1030 ms 76604 KB Output is correct
69 Correct 1253 ms 74824 KB Output is correct
70 Correct 1191 ms 77436 KB Output is correct
71 Correct 1123 ms 74976 KB Output is correct
72 Correct 1072 ms 74432 KB Output is correct
73 Correct 1189 ms 74324 KB Output is correct
74 Correct 962 ms 75300 KB Output is correct
75 Correct 987 ms 74488 KB Output is correct