답안 #304386

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
304386 2020-09-21T08:40:33 Z myungwoo 식물 비교 (IOI20_plants) C++17
100 / 100
1363 ms 43500 KB
#include <bits/stdc++.h>
#include "plants.h"
using namespace std;

#define MAXN 200005
#define debug(...) fprintf(stderr, __VA_ARGS__); fflush(stderr);
typedef pair<int, int> pii;

int N, K, R[MAXN];
int H[MAXN], nxt_r[MAXN][18], nxt_l[MAXN][18];

template <typename T>
struct SegTree{
	int n_bits, TS, ST;
	int range_l(int x){ return ((x<<(__builtin_clz(x)-31+n_bits)) ^ (1<<n_bits)) + 1; }
	int range_r(int x){ return ((x<<(__builtin_clz(x)-31+n_bits) ^ ((1<<(__builtin_clz(x)-31+n_bits))-1)) ^ (1<<n_bits)) + 1; }
	struct Value{
		T value, lazysum;
	};
	vector<Value> tree;
	SegTree(int arr[]){
		n_bits = 32-__builtin_clz(N-1);
		TS = 1 << n_bits+1; ST = TS/2-1;
		tree.resize(TS, {0, 0});
		for (int i=ST+1;i<TS;i++) tree[i].value = i-ST <= N ? arr[i-ST] : 1e9;
		for (int i=ST;i;i--) tree[i].value = min(tree[i*2].value, tree[i*2+1].value);
	}
	void propagate(int n){
		if (n <= ST){
			tree[n*2].value += tree[n].lazysum, tree[n*2+1].value += tree[n].lazysum;
			tree[n*2].lazysum += tree[n].lazysum, tree[n*2+1].lazysum += tree[n].lazysum;
		}
		tree[n].lazysum = 0;
	}
	void update(int n, int l, int r, const int &v){
		int s = range_l(n), e = range_r(n);
		if (l > r || e < l || r < s) return;
		if (l <= s && e <= r){
			tree[n].value += v, tree[n].lazysum += v;
			return;
		}
		propagate(n);
		update(n*2, l, r, v);
		update(n*2+1, l, r, v);
		tree[n].value = min(tree[n*2].value, tree[n*2+1].value);
	}
	int findzero(int n){
		if (tree[n].value != 0) return 0;
		propagate(n);
		if (n > ST) return n-ST;
		if (!tree[n*2].value) return findzero(n*2);
		else if (!tree[n*2+1].value) return findzero(n*2+1);
		else assert(0);
	}
};

struct ZeroContainer{
	set<int> pos, non_covered;
	set<int>::iterator _left(set<int>::iterator it){
		auto ret = it;
		if (ret == pos.begin()) ret = pos.end();
		return --ret;
	}
	set<int>::iterator _right(set<int>::iterator it){
		auto ret = it; ret++;
		if (ret == pos.end()) return pos.begin();
		return ret;
	}
	void insert(int p){
		auto it = pos.insert(p).first;
		auto l = _left(it), r = _right(it);
		if (l == it || (*it-*l+N)%N >= K) non_covered.insert(p);
		if (r != it && (*r-*it+N)%N < K) non_covered.erase(*r);
	}
	void remove(int p){
		auto it = pos.find(p);
		auto l = _left(it), r = _right(it);
		if (l != it && r != it && (l == r || (*r-*l+N)%N >= K)) non_covered.insert(*r);
		pos.erase(it);
	}
	int pop_smallest(){
		int ret = *non_covered.begin(); non_covered.erase(ret);
		remove(ret);
		return ret;
	}
};

int go_left(int n, int d)
{ return (n - d%N - 1 + N)%N + 1; }

int go_right(int n, int d)
{ return (n + d%N - 1 + N)%N + 1; }

void init(int K, vector<int> r)
{
	::K = K;
	N = r.size();
	for (int i=1;i<=N;i++) R[i] = r[i-1];

	{
		SegTree<int> segtree(R);
		ZeroContainer zc;
		for (int ord=N;ord;ord--){
			for (;;){
				int p = segtree.findzero(1);
				if (!p) break;
				zc.insert(p);
				segtree.update(1, p, p, 1e9);
			}
			int x = zc.pop_smallest();
			H[x] = ord;
			segtree.update(1, max(1, x-K+1), x-1, -1);
			if (x-K+1 < 1) segtree.update(1, N-(1-(x-K+1))+1, N, -1);
		}
	}

	{
		set <pii> s;
		for (int i=1;i<=K;i++) s.insert({H[i], i});
		for (int i=1;i<=N;i++){
			auto it = s.lower_bound({H[i], 0});
			if (it != s.begin()){
				it--;
				nxt_r[i][0] = (it->second - i + N) % N;
			}
			int t = (i+K-1)%N+1;
			s.insert({H[t], t});
			s.erase({H[i], i});
		}
	}
	{
		set <pii> s;
		for (int i=1;i<K;i++) s.insert({H[i], i});
		for (int i=1;i<=N;i++){
			int t = (i+K-2)%N+1;
			auto it = s.lower_bound({H[t], 0});
			if (it != s.begin()){
				it--;
				nxt_l[t][0] = (t - it->second + N) % N;
			}
			s.insert({H[t], t});
			s.erase({H[i], i});
		}
	}
	for (int i=0;i+1<18;i++) for (int j=1;j<=N;j++){
		nxt_l[j][i+1] = min(N+10, nxt_l[go_left(j, nxt_l[j][i])][i] + nxt_l[j][i]);
		nxt_r[j][i+1] = min(N+10, nxt_r[go_right(j, nxt_r[j][i])][i] + nxt_r[j][i]);
	}
}

bool has_left_path(int a, int b)
{
	int d = (a-b+N)%N;
	for (int i=18;i--;) if (d >= nxt_l[a][i]){
		d -= nxt_l[a][i];
		a = go_left(a, nxt_l[a][i]);
	}
	return (a-b+N)%N < K && H[a] >= H[b];
}

bool has_right_path(int a, int b)
{
	int d = (b-a+N)%N;
	for (int i=18;i--;) if (d >= nxt_r[a][i]){
		d -= nxt_r[a][i];
		a = go_right(a, nxt_r[a][i]);
	}
	return (b-a+N)%N < K && H[a] >= H[b];
}

int compare_plants(int a, int b)
{
	a++; b++;
	if (has_left_path(a, b) || has_right_path(a, b)) return 1;
	if (has_left_path(b, a) || has_right_path(b, a)) return -1;
	return 0;
}

Compilation message

plants.cpp: In instantiation of 'SegTree<T>::SegTree(int*) [with T = int]':
plants.cpp:101:25:   required from here
plants.cpp:23:19: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   23 |   TS = 1 << n_bits+1; ST = TS/2-1;
      |             ~~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 219 ms 3144 KB Output is correct
7 Correct 267 ms 6900 KB Output is correct
8 Correct 670 ms 39220 KB Output is correct
9 Correct 673 ms 38508 KB Output is correct
10 Correct 687 ms 38636 KB Output is correct
11 Correct 705 ms 39148 KB Output is correct
12 Correct 712 ms 38892 KB Output is correct
13 Correct 725 ms 43372 KB Output is correct
14 Correct 713 ms 34028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 105 ms 4212 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 111 ms 4192 KB Output is correct
11 Correct 154 ms 3952 KB Output is correct
12 Correct 206 ms 4176 KB Output is correct
13 Correct 106 ms 4212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 5 ms 640 KB Output is correct
7 Correct 105 ms 4212 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 111 ms 4192 KB Output is correct
11 Correct 154 ms 3952 KB Output is correct
12 Correct 206 ms 4176 KB Output is correct
13 Correct 106 ms 4212 KB Output is correct
14 Correct 171 ms 6848 KB Output is correct
15 Correct 1324 ms 39788 KB Output is correct
16 Correct 172 ms 6772 KB Output is correct
17 Correct 1363 ms 39920 KB Output is correct
18 Correct 916 ms 38720 KB Output is correct
19 Correct 998 ms 38672 KB Output is correct
20 Correct 1281 ms 43464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 162 ms 3576 KB Output is correct
4 Correct 821 ms 37100 KB Output is correct
5 Correct 868 ms 34668 KB Output is correct
6 Correct 929 ms 34028 KB Output is correct
7 Correct 954 ms 34540 KB Output is correct
8 Correct 1141 ms 38280 KB Output is correct
9 Correct 845 ms 36332 KB Output is correct
10 Correct 848 ms 34796 KB Output is correct
11 Correct 766 ms 43500 KB Output is correct
12 Correct 785 ms 34028 KB Output is correct
13 Correct 911 ms 40684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 7 ms 384 KB Output is correct
7 Correct 54 ms 1048 KB Output is correct
8 Correct 22 ms 1020 KB Output is correct
9 Correct 39 ms 1016 KB Output is correct
10 Correct 22 ms 1024 KB Output is correct
11 Correct 52 ms 1144 KB Output is correct
12 Correct 42 ms 1024 KB Output is correct
13 Correct 20 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 816 ms 34156 KB Output is correct
7 Correct 873 ms 34028 KB Output is correct
8 Correct 816 ms 34668 KB Output is correct
9 Correct 1135 ms 38380 KB Output is correct
10 Correct 816 ms 36464 KB Output is correct
11 Correct 868 ms 37868 KB Output is correct
12 Correct 727 ms 37396 KB Output is correct
13 Correct 827 ms 34796 KB Output is correct
14 Correct 777 ms 34104 KB Output is correct
15 Correct 887 ms 34544 KB Output is correct
16 Correct 704 ms 35692 KB Output is correct
17 Correct 790 ms 34156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 219 ms 3144 KB Output is correct
7 Correct 267 ms 6900 KB Output is correct
8 Correct 670 ms 39220 KB Output is correct
9 Correct 673 ms 38508 KB Output is correct
10 Correct 687 ms 38636 KB Output is correct
11 Correct 705 ms 39148 KB Output is correct
12 Correct 712 ms 38892 KB Output is correct
13 Correct 725 ms 43372 KB Output is correct
14 Correct 713 ms 34028 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 5 ms 640 KB Output is correct
21 Correct 105 ms 4212 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 111 ms 4192 KB Output is correct
25 Correct 154 ms 3952 KB Output is correct
26 Correct 206 ms 4176 KB Output is correct
27 Correct 106 ms 4212 KB Output is correct
28 Correct 171 ms 6848 KB Output is correct
29 Correct 1324 ms 39788 KB Output is correct
30 Correct 172 ms 6772 KB Output is correct
31 Correct 1363 ms 39920 KB Output is correct
32 Correct 916 ms 38720 KB Output is correct
33 Correct 998 ms 38672 KB Output is correct
34 Correct 1281 ms 43464 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 0 ms 384 KB Output is correct
37 Correct 162 ms 3576 KB Output is correct
38 Correct 821 ms 37100 KB Output is correct
39 Correct 868 ms 34668 KB Output is correct
40 Correct 929 ms 34028 KB Output is correct
41 Correct 954 ms 34540 KB Output is correct
42 Correct 1141 ms 38280 KB Output is correct
43 Correct 845 ms 36332 KB Output is correct
44 Correct 848 ms 34796 KB Output is correct
45 Correct 766 ms 43500 KB Output is correct
46 Correct 785 ms 34028 KB Output is correct
47 Correct 911 ms 40684 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 0 ms 384 KB Output is correct
50 Correct 0 ms 384 KB Output is correct
51 Correct 0 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 7 ms 384 KB Output is correct
54 Correct 54 ms 1048 KB Output is correct
55 Correct 22 ms 1020 KB Output is correct
56 Correct 39 ms 1016 KB Output is correct
57 Correct 22 ms 1024 KB Output is correct
58 Correct 52 ms 1144 KB Output is correct
59 Correct 42 ms 1024 KB Output is correct
60 Correct 20 ms 1024 KB Output is correct
61 Correct 266 ms 3452 KB Output is correct
62 Correct 324 ms 6516 KB Output is correct
63 Correct 831 ms 35440 KB Output is correct
64 Correct 863 ms 34156 KB Output is correct
65 Correct 934 ms 34188 KB Output is correct
66 Correct 965 ms 34668 KB Output is correct
67 Correct 1165 ms 38256 KB Output is correct
68 Correct 872 ms 36716 KB Output is correct
69 Correct 971 ms 37868 KB Output is correct
70 Correct 860 ms 37192 KB Output is correct
71 Correct 973 ms 34796 KB Output is correct
72 Correct 949 ms 34232 KB Output is correct
73 Correct 970 ms 34668 KB Output is correct
74 Correct 821 ms 35180 KB Output is correct
75 Correct 808 ms 34228 KB Output is correct