답안 #301566

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
301566 2020-09-18T04:37:09 Z Yousef_Salama 식물 비교 (IOI20_plants) C++17
100 / 100
1386 ms 84188 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

struct segment_tree{
	int n;
	vector <int> tree, flag;
	
	void build(int i, int L, int R, const vector <int>& v){
		if(L == R){
			tree[i] = v[L];
			return;
		}
		
		int mid = (L + R) / 2;
		
		build(2 * i, L, mid, v);
		build(2 * i + 1, mid + 1, R, v);
		
		tree[i] = min(tree[2 * i], tree[2 * i + 1]);
	}
	segment_tree(const vector <int>& v){
		n = v.size();
		tree.resize(4 * n);
		flag.resize(4 * n, 0);
		
		build(1, 0, n - 1, v);
	}
	
	void push(int i, int L, int R){
		if(L < R){
			flag[2 * i] += flag[i];
			flag[2 * i + 1] += flag[i];
		}
		
		tree[i] += flag[i];
		flag[i] = 0;
	}
	
	void update(int i, int L, int R, int u, int v, int val){
		push(i, L, R);
		
		if(R < u || v < L)return;
		if(u <= L && R <= v){
			flag[i] += val;
			push(i, L, R);
			
			return;
		}
		
		int mid = (L + R) / 2;
		
		update(2 * i, L, mid, u, v, val);
		update(2 * i + 1, mid + 1, R, u, v, val);
		
		tree[i] = min(tree[2 * i], tree[2 * i + 1]);
	}
	void update(int u, int v, int val){
		update(1, 0, n - 1, u, v, val);
	}
	
	int extract_zero(int i, int L, int R){
		push(i, L, R);
		
		if(tree[i] != 0)return -1;
		
		if(L == R){
			tree[i] = 1 << 30;
			return L;
		}
		
		int mid = (L + R) / 2;
		
		push(2 * i, L, mid);
		push(2 * i + 1, mid + 1, R);
		
		int r;
		if(tree[2 * i] == 0){
			r = extract_zero(2 * i, L, mid);
		}else{
			r = extract_zero(2 * i + 1, mid + 1, R);
		}
		
		tree[i] = min(tree[2 * i], tree[2 * i + 1]);
		
		return r;
	}
	int extract_zero(){
		return extract_zero(1, 0, n - 1);
	}
};

const int LOG = 20;

vector < vector <int> > next_greater, next_smaller;
vector <int> v;
int n, k;

void init(int pk, vector <int> r){
	n = r.size();
	k = pk;

	for(int i = 0; i < n; i++)
		r[i] = k - 1 - r[i];
	
	segment_tree tree(r);
	set <int> s;
	set < pair <int, int> > d;
	
	auto dist = [&](int x, int y){
					if(x > y)return x - y;
					return x - y + n;
				};
	auto add = [&](int i){
					if(s.empty()){
						s.insert(i);
						d.insert({n, i});
						return;
					}
					
					auto ptr = s.insert(i).first;
					auto r = (next(ptr) == s.end() ? s.begin() : next(ptr)), l = (ptr == s.begin() ? prev(s.end()) : prev(ptr));
					
					d.erase({dist(*r, *l), *r});
					d.insert({dist(*r, i), *r});
					d.insert({dist(i, *l), i});
				};
	auto del = [&](int i){
					if(s.size() == 1){
						s.clear();
						d.clear();
						return;
					}
					
					auto ptr = s.find(i);
					auto r = (next(ptr) == s.end() ? s.begin() : next(ptr)), l = (ptr == s.begin() ? prev(s.end()) : prev(ptr));
					
					d.insert({dist(*r, *l), *r});
					d.erase({dist(*r, i), *r});
					d.erase({dist(i, *l), i});
						
					s.erase(ptr);
				};
	
	v.resize(2 * n);	
	for(int i = 0; i < n; i++){
		while(true){
			int r = tree.extract_zero();
			if(r == -1)break;
			
			add(r);
		}
		
		int x = d.rbegin()->second;
		del(x);
		
		if(x - (k - 1) >= 0){
			tree.update(x - (k - 1), x - 1, -1);
		}else{
			if(x > 0)tree.update(0, x - 1, -1);
			tree.update(x - (k - 1) + n, n - 1, -1);
		}
		v[x] = i;
	}
	
	for(int i = n; i < 2 * n; i++)
		v[i] = v[i - n];
	
	next_greater.resize(LOG, vector <int>(2 * n));
	next_smaller.resize(LOG, vector <int>(2 * n));
	
	set < pair <int, int> > q;
	for(int i = 2 * n - 1; i >= 0; i--){
		if(i + k < 2 * n){
			q.erase({v[i + k], i + k});
		}
		
		auto x = q.lower_bound(make_pair(v[i], 0));
		
		if(x != q.end())next_greater[0][i] = x->second;
		else next_greater[0][i] = -1;
		
		if(x != q.begin())next_smaller[0][i] = prev(x)->second;
		else next_smaller[0][i] = -1;
		
		for(int j = 1; j < LOG; j++){
			if(next_greater[j - 1][i] == -1)next_greater[j][i] = -1;
			else next_greater[j][i] = next_greater[j - 1][next_greater[j - 1][i]];
			
			if(next_smaller[j - 1][i] == -1)next_smaller[j][i] = -1;
			else next_smaller[j][i] = next_smaller[j - 1][next_smaller[j - 1][i]];
		}
		
		q.insert({v[i], i});
	}
	
	/*for(int i = 0; i < n; i++)
		cout << r[i] << ' ';
	cout << endl;
	for(int i = 0; i < n; i++)
		cout << v[i] << ' ';
	cout << endl;*/
}

int compare_plants(int x, int y){
	auto go_greater = [&](int x, int y){
							for(int l = LOG - 1; l >= 0; l--){
								if(next_greater[l][x] != -1 && next_greater[l][x] <= y){
									x = next_greater[l][x];
								}
							}
							
							if(v[x] > v[y] || x + k <= y)return false;
							return true;
						};
					
	auto go_smaller = [&](int x, int y){
							for(int l = LOG - 1; l >= 0; l--){
								if(next_smaller[l][x] != -1 && next_smaller[l][x] <= y){
									x = next_smaller[l][x];
								}
							}
							
							if(v[x] < v[y] || x + k <= y)return false;
							return true;
						};
						
	if(v[x] < v[y]){
		if(go_greater(x, y) || go_smaller(y, x + n))return -1;
		return 0;
	}else{
		if(go_smaller(x, y) || go_greater(y, x + n))return 1;
		return 0;
	}
}

/*
int main(){
	init(2, {1, 0, 1, 1, 0});
	cout << compare_plants(0, 2) << endl;
	cout << compare_plants(1, 2) << endl;

	return 0;
}
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 76 ms 3192 KB Output is correct
7 Correct 173 ms 10232 KB Output is correct
8 Correct 843 ms 76408 KB Output is correct
9 Correct 888 ms 76484 KB Output is correct
10 Correct 909 ms 76636 KB Output is correct
11 Correct 960 ms 76592 KB Output is correct
12 Correct 958 ms 76548 KB Output is correct
13 Correct 625 ms 76252 KB Output is correct
14 Correct 988 ms 76536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 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 768 KB Output is correct
7 Correct 97 ms 5124 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 5 ms 768 KB Output is correct
10 Correct 98 ms 5220 KB Output is correct
11 Correct 107 ms 4984 KB Output is correct
12 Correct 118 ms 5368 KB Output is correct
13 Correct 94 ms 5176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 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 768 KB Output is correct
7 Correct 97 ms 5124 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 5 ms 768 KB Output is correct
10 Correct 98 ms 5220 KB Output is correct
11 Correct 107 ms 4984 KB Output is correct
12 Correct 118 ms 5368 KB Output is correct
13 Correct 94 ms 5176 KB Output is correct
14 Correct 167 ms 10556 KB Output is correct
15 Correct 1368 ms 80732 KB Output is correct
16 Correct 164 ms 10456 KB Output is correct
17 Correct 1386 ms 80732 KB Output is correct
18 Correct 917 ms 79560 KB Output is correct
19 Correct 1267 ms 79608 KB Output is correct
20 Correct 1196 ms 84188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 100 ms 3832 KB Output is correct
4 Correct 1030 ms 76408 KB Output is correct
5 Correct 1000 ms 76252 KB Output is correct
6 Correct 1099 ms 76380 KB Output is correct
7 Correct 1172 ms 76380 KB Output is correct
8 Correct 1340 ms 79068 KB Output is correct
9 Correct 824 ms 76380 KB Output is correct
10 Correct 1087 ms 76580 KB Output is correct
11 Correct 631 ms 76252 KB Output is correct
12 Correct 1055 ms 76408 KB Output is correct
13 Correct 882 ms 77604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 23 ms 1152 KB Output is correct
8 Correct 18 ms 1024 KB Output is correct
9 Correct 21 ms 1024 KB Output is correct
10 Correct 18 ms 1152 KB Output is correct
11 Correct 21 ms 1024 KB Output is correct
12 Correct 20 ms 1152 KB Output is correct
13 Correct 18 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 3 ms 768 KB Output is correct
6 Correct 829 ms 76380 KB Output is correct
7 Correct 923 ms 76636 KB Output is correct
8 Correct 1025 ms 76384 KB Output is correct
9 Correct 1208 ms 79068 KB Output is correct
10 Correct 714 ms 76252 KB Output is correct
11 Correct 966 ms 78556 KB Output is correct
12 Correct 837 ms 76408 KB Output is correct
13 Correct 831 ms 76508 KB Output is correct
14 Correct 930 ms 76416 KB Output is correct
15 Correct 1039 ms 76252 KB Output is correct
16 Correct 955 ms 76640 KB Output is correct
17 Correct 796 ms 76380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 76 ms 3192 KB Output is correct
7 Correct 173 ms 10232 KB Output is correct
8 Correct 843 ms 76408 KB Output is correct
9 Correct 888 ms 76484 KB Output is correct
10 Correct 909 ms 76636 KB Output is correct
11 Correct 960 ms 76592 KB Output is correct
12 Correct 958 ms 76548 KB Output is correct
13 Correct 625 ms 76252 KB Output is correct
14 Correct 988 ms 76536 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 1 ms 256 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 768 KB Output is correct
21 Correct 97 ms 5124 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 5 ms 768 KB Output is correct
24 Correct 98 ms 5220 KB Output is correct
25 Correct 107 ms 4984 KB Output is correct
26 Correct 118 ms 5368 KB Output is correct
27 Correct 94 ms 5176 KB Output is correct
28 Correct 167 ms 10556 KB Output is correct
29 Correct 1368 ms 80732 KB Output is correct
30 Correct 164 ms 10456 KB Output is correct
31 Correct 1386 ms 80732 KB Output is correct
32 Correct 917 ms 79560 KB Output is correct
33 Correct 1267 ms 79608 KB Output is correct
34 Correct 1196 ms 84188 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 100 ms 3832 KB Output is correct
38 Correct 1030 ms 76408 KB Output is correct
39 Correct 1000 ms 76252 KB Output is correct
40 Correct 1099 ms 76380 KB Output is correct
41 Correct 1172 ms 76380 KB Output is correct
42 Correct 1340 ms 79068 KB Output is correct
43 Correct 824 ms 76380 KB Output is correct
44 Correct 1087 ms 76580 KB Output is correct
45 Correct 631 ms 76252 KB Output is correct
46 Correct 1055 ms 76408 KB Output is correct
47 Correct 882 ms 77604 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 1 ms 256 KB Output is correct
50 Correct 1 ms 256 KB Output is correct
51 Correct 1 ms 256 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 3 ms 384 KB Output is correct
54 Correct 23 ms 1152 KB Output is correct
55 Correct 18 ms 1024 KB Output is correct
56 Correct 21 ms 1024 KB Output is correct
57 Correct 18 ms 1152 KB Output is correct
58 Correct 21 ms 1024 KB Output is correct
59 Correct 20 ms 1152 KB Output is correct
60 Correct 18 ms 1024 KB Output is correct
61 Correct 100 ms 3832 KB Output is correct
62 Correct 188 ms 10236 KB Output is correct
63 Correct 946 ms 76692 KB Output is correct
64 Correct 979 ms 76380 KB Output is correct
65 Correct 1094 ms 76448 KB Output is correct
66 Correct 1138 ms 76252 KB Output is correct
67 Correct 1375 ms 79068 KB Output is correct
68 Correct 854 ms 76380 KB Output is correct
69 Correct 1085 ms 78668 KB Output is correct
70 Correct 991 ms 76536 KB Output is correct
71 Correct 985 ms 76380 KB Output is correct
72 Correct 1100 ms 76408 KB Output is correct
73 Correct 1167 ms 76380 KB Output is correct
74 Correct 833 ms 76252 KB Output is correct
75 Correct 942 ms 76380 KB Output is correct