답안 #300887

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300887 2020-09-17T14:48:09 Z ainta 식물 비교 (IOI20_plants) C++17
100 / 100
1061 ms 83832 KB
#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();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1280 KB Output is correct
5 Correct 1 ms 1280 KB Output is correct
6 Correct 98 ms 4216 KB Output is correct
7 Correct 207 ms 11404 KB Output is correct
8 Correct 684 ms 79480 KB Output is correct
9 Correct 733 ms 78840 KB Output is correct
10 Correct 784 ms 78968 KB Output is correct
11 Correct 826 ms 79736 KB Output is correct
12 Correct 840 ms 79352 KB Output is correct
13 Correct 816 ms 83832 KB Output is correct
14 Correct 902 ms 74552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1280 KB Output is correct
5 Correct 2 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 4 ms 1408 KB Output is correct
9 Correct 5 ms 1792 KB Output is correct
10 Correct 104 ms 6008 KB Output is correct
11 Correct 125 ms 6008 KB Output is correct
12 Correct 155 ms 6136 KB Output is correct
13 Correct 107 ms 6008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1280 KB Output is correct
5 Correct 2 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 4 ms 1408 KB Output is correct
9 Correct 5 ms 1792 KB Output is correct
10 Correct 104 ms 6008 KB Output is correct
11 Correct 125 ms 6008 KB Output is correct
12 Correct 155 ms 6136 KB Output is correct
13 Correct 107 ms 6008 KB Output is correct
14 Correct 175 ms 11424 KB Output is correct
15 Correct 963 ms 74488 KB Output is correct
16 Correct 180 ms 11384 KB Output is correct
17 Correct 971 ms 74300 KB Output is correct
18 Correct 911 ms 78964 KB Output is correct
19 Correct 954 ms 74360 KB Output is correct
20 Correct 857 ms 74360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 1 ms 1280 KB Output is correct
3 Correct 123 ms 4856 KB Output is correct
4 Correct 938 ms 77688 KB Output is correct
5 Correct 918 ms 75256 KB Output is correct
6 Correct 1000 ms 74424 KB Output is correct
7 Correct 946 ms 74360 KB Output is correct
8 Correct 904 ms 74488 KB Output is correct
9 Correct 830 ms 76664 KB Output is correct
10 Correct 938 ms 75256 KB Output is correct
11 Correct 812 ms 83704 KB Output is correct
12 Correct 926 ms 74360 KB Output is correct
13 Correct 886 ms 80888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1280 KB Output is correct
5 Correct 1 ms 1280 KB Output is correct
6 Correct 4 ms 1408 KB Output is correct
7 Correct 26 ms 2048 KB Output is correct
8 Correct 21 ms 2048 KB Output is correct
9 Correct 27 ms 2048 KB Output is correct
10 Correct 22 ms 2048 KB Output is correct
11 Correct 27 ms 2040 KB Output is correct
12 Correct 26 ms 2040 KB Output is correct
13 Correct 21 ms 2040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 1 ms 1280 KB Output is correct
3 Correct 1 ms 1312 KB Output is correct
4 Correct 1 ms 1280 KB Output is correct
5 Correct 3 ms 1664 KB Output is correct
6 Correct 786 ms 74652 KB Output is correct
7 Correct 817 ms 74488 KB Output is correct
8 Correct 778 ms 74556 KB Output is correct
9 Correct 951 ms 74448 KB Output is correct
10 Correct 803 ms 76720 KB Output is correct
11 Correct 895 ms 74720 KB Output is correct
12 Correct 818 ms 77432 KB Output is correct
13 Correct 837 ms 75000 KB Output is correct
14 Correct 785 ms 74348 KB Output is correct
15 Correct 867 ms 74360 KB Output is correct
16 Correct 830 ms 75896 KB Output is correct
17 Correct 757 ms 74488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1280 KB Output is correct
5 Correct 1 ms 1280 KB Output is correct
6 Correct 98 ms 4216 KB Output is correct
7 Correct 207 ms 11404 KB Output is correct
8 Correct 684 ms 79480 KB Output is correct
9 Correct 733 ms 78840 KB Output is correct
10 Correct 784 ms 78968 KB Output is correct
11 Correct 826 ms 79736 KB Output is correct
12 Correct 840 ms 79352 KB Output is correct
13 Correct 816 ms 83832 KB Output is correct
14 Correct 902 ms 74552 KB Output is correct
15 Correct 1 ms 1280 KB Output is correct
16 Correct 1 ms 1280 KB Output is correct
17 Correct 1 ms 1280 KB Output is correct
18 Correct 1 ms 1280 KB Output is correct
19 Correct 2 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 4 ms 1408 KB Output is correct
23 Correct 5 ms 1792 KB Output is correct
24 Correct 104 ms 6008 KB Output is correct
25 Correct 125 ms 6008 KB Output is correct
26 Correct 155 ms 6136 KB Output is correct
27 Correct 107 ms 6008 KB Output is correct
28 Correct 175 ms 11424 KB Output is correct
29 Correct 963 ms 74488 KB Output is correct
30 Correct 180 ms 11384 KB Output is correct
31 Correct 971 ms 74300 KB Output is correct
32 Correct 911 ms 78964 KB Output is correct
33 Correct 954 ms 74360 KB Output is correct
34 Correct 857 ms 74360 KB Output is correct
35 Correct 2 ms 1280 KB Output is correct
36 Correct 1 ms 1280 KB Output is correct
37 Correct 123 ms 4856 KB Output is correct
38 Correct 938 ms 77688 KB Output is correct
39 Correct 918 ms 75256 KB Output is correct
40 Correct 1000 ms 74424 KB Output is correct
41 Correct 946 ms 74360 KB Output is correct
42 Correct 904 ms 74488 KB Output is correct
43 Correct 830 ms 76664 KB Output is correct
44 Correct 938 ms 75256 KB Output is correct
45 Correct 812 ms 83704 KB Output is correct
46 Correct 926 ms 74360 KB Output is correct
47 Correct 886 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 1 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 4 ms 1408 KB Output is correct
54 Correct 26 ms 2048 KB Output is correct
55 Correct 21 ms 2048 KB Output is correct
56 Correct 27 ms 2048 KB Output is correct
57 Correct 22 ms 2048 KB Output is correct
58 Correct 27 ms 2040 KB Output is correct
59 Correct 26 ms 2040 KB Output is correct
60 Correct 21 ms 2040 KB Output is correct
61 Correct 131 ms 4836 KB Output is correct
62 Correct 233 ms 11400 KB Output is correct
63 Correct 798 ms 75768 KB Output is correct
64 Correct 902 ms 74616 KB Output is correct
65 Correct 953 ms 74360 KB Output is correct
66 Correct 931 ms 74616 KB Output is correct
67 Correct 928 ms 74360 KB Output is correct
68 Correct 913 ms 76664 KB Output is correct
69 Correct 1048 ms 74876 KB Output is correct
70 Correct 964 ms 77564 KB Output is correct
71 Correct 1061 ms 75000 KB Output is correct
72 Correct 1004 ms 74360 KB Output is correct
73 Correct 964 ms 74360 KB Output is correct
74 Correct 809 ms 75380 KB Output is correct
75 Correct 845 ms 74488 KB Output is correct