답안 #1050061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050061 2024-08-09T07:07:26 Z fuad27 식물 비교 (IOI20_plants) C++17
100 / 100
541 ms 100420 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

struct lazy_segtree {
	vector<pair<int,int>> T;
	vector<int> lz;
	int n;
	void build(vector<int> &a, int tl, int tr, int v) {
		if(tl == tr) {
			T[v] = {a[tl], tl};
			return;
		}
		int tm = (tl+tr)/2;
		build(a, tl, tm, 2*v);
		build(a, tm+1, tr, 2*v+1);
		T[v] = min(T[2*v], T[2*v+1]);

	}
	lazy_segtree(vector<int> &a) {
		n=a.size();	
		T.resize(4*n);
		lz.resize(4*n);
		build(a, 0, n-1, 1);
	}
	void push(int v){
		lz[2*v] += lz[v];
		lz[2*v+1] += lz[v];
		T[2*v].first += lz[v];
		T[2*v+1].first += lz[v];
		lz[v] = 0;
	}
	void update(int l, int r, int add, int tl, int tr, int v) {
		if(r < tl or tr < l)return;
		if(l <= tl and tr<=r) {
			lz[v]+=add;
			T[v].first+=add;
			return;
		}
		push(v);
		int tm=(tl+tr)/2;
		update(l, r, add, tl, tm, 2*v);
		update(l, r, add, tm+1, tr, 2*v+1);
		T[v] = min(T[2*v], T[2*v+1]);
	}
	pair<int,int> query(int l, int r, int tl, int tr, int v) {
		if(r < tl or tr < l)return {1e9, 1e9};
		if(l <= tl and tr<=r)return T[v];
		push(v);
		int tm=(tl+tr)/2;
		return min(query(l, r, tl, tm, 2*v), query(l, r, tm+1, tr, 2*v+1));
	}
	void update(int l, int r, int add) {
		update(l, r, add, 0, n-1, 1);
	}
	pair<int,int> query(int l, int r) {
		return query(l, r, 0, n-1, 1);
	}
};
const int N = 4e5+10;
vector<int> H;
vector<int> g[N];
const int lg = 20;
int sml[N][lg], grt[N][lg];
int k;
int n;
void init(int K, std::vector<int> r) {
	k=K;
	n = r.size();
	lazy_segtree ls(r);
	int cnt = n;
	H.resize(n, -1);
	auto dfs = [&](int at, auto &&dfs) -> void {
		if(at >= k-1) {
			pair<int,int> val = ls.query(at-k+1, at-1);
			while(val.first == 0) {
				dfs(val.second, dfs);
				val = ls.query(at-k+1, at-1);
			}
			H[at] = cnt--;
			ls.update(at-k+1, at-1, -1);
			ls.update(at, at, 1e9);
		}
		else {
			pair<int,int> val = min(ls.query(0, at-1), ls.query(n-k+at+1, n-1));
			while(val.first == 0) {
				dfs(val.second, dfs);
				val = min(ls.query(0, at-1), ls.query(n-k+at+1, n-1));
			}
			H[at] = cnt--;
			ls.update(0, at-1, -1), ls.update(n-k+at+1, n-1, -1);
			ls.update(at, at, 1e9);
		}
	};
	while(ls.query(0, n-1).first == 0) {
		dfs(ls.query(0, n-1).second, dfs);
	}
	for(int i = 0;i<n;i++) {
		H.push_back(H[i]);
	}
//	for(int i =0 ;i<2*n;i++) {
//		int nxtmn = -1;
//		for(int j = i+1;j<min(2*n, i+k);j++) {
//			if(H[j] < H[i] and (nxtmn == -1 or H[nxtmn] < H[j])) {
//				nxtmn = j;
//			}
//		}
//		sml[i][0] = nxtmn;
//		nxtmn = -1;
//		for(int j = i+1;j<min(2*n, i+k);j++) {
//			if(H[j] > H[i] and (nxtmn == -1 or H[nxtmn] > H[j])) {
//				nxtmn = j;
//			}
//		}
//		grt[i][0] = nxtmn;
//	}
	set<pair<int,int>> s;
	s.insert({-1e9, 2*n});
	s.insert({1e9, 2*n});
	for(int i = 2*n-1;i>=0;i--) {
		if(i+k < 2*n) {
			s.erase({H[i+k], i+k});
		}
		grt[i][0] = (*s.lower_bound(pair{H[i], i})).second;
		sml[i][0] = (*(--s.lower_bound(pair{H[i], i}))).second;
		s.insert({H[i], i});
	}
	for(int i =0 ;i<2*n;i++) {
		if(grt[i][0] == -1)grt[i][0]=2*n;
		if(sml[i][0] == -1)sml[i][0]=2*n;
	}
	sml[2*n][0] = grt[2*n][0] = 2*n;
	for(int i = 1;i<lg;i++) {
		for(int j = 0;j<=2*n;j++) {
			sml[j][i] = sml[sml[j][i-1]][i-1];
			grt[j][i] = grt[grt[j][i-1]][i-1];
		}
	}
	return;
}
int dist(int a, int b) {
	return min({abs(a-b), abs(a+n-b), abs(b+n-a)});
}
int compare_plants(int x, int y) {
	{
		int a = x, b = y;
		if(b < a)b+=n;
		if(a+k-1 < b) {
			for(int i = lg-1;i>=0;i--) {
				if(grt[a][i] + k - 1 < b) {
					a=grt[a][i];
				}
			}
			a=grt[a][0];
		}
		if(a!=2*n and H[a]<H[b])return -1;
		a=x;
		if(a+k-1 < b) {
			for(int i = lg-1;i>=0;i--) {
				if(sml[a][i] + k - 1 < b) {
					a=sml[a][i];
				}
			}
			a=sml[a][0];
		}
		if(a!=2*n and H[a]>H[b])return 1;
	}
	{
		int a = y, b = x;
		if(b<a)b+=n;
		if(a+k-1 < b) {
			for(int i = lg-1;i>=0;i--) {
				if(grt[a][i] + k - 1 < b) {
					a=grt[a][i];
				}
			}
			a=grt[a][0];
		}
		if(a!=2*n and H[a]<H[b])return 1;
		a=y;
		if(a+k-1 < b) {
			for(int i = lg-1;i>=0;i--) {
				if(sml[a][i] + k - 1 < b) {
					a=sml[a][i];
				}
			}
			a=sml[a][0];
		}
		if(a!=2*n and H[a]>H[b])return -1;
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 44 ms 15444 KB Output is correct
7 Correct 75 ms 22608 KB Output is correct
8 Correct 248 ms 87104 KB Output is correct
9 Correct 242 ms 87388 KB Output is correct
10 Correct 260 ms 87104 KB Output is correct
11 Correct 265 ms 87284 KB Output is correct
12 Correct 264 ms 90144 KB Output is correct
13 Correct 235 ms 90176 KB Output is correct
14 Correct 209 ms 90180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 36 ms 17964 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 35 ms 18076 KB Output is correct
11 Correct 36 ms 17844 KB Output is correct
12 Correct 34 ms 18152 KB Output is correct
13 Correct 35 ms 18088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 36 ms 17964 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12892 KB Output is correct
10 Correct 35 ms 18076 KB Output is correct
11 Correct 36 ms 17844 KB Output is correct
12 Correct 34 ms 18152 KB Output is correct
13 Correct 35 ms 18088 KB Output is correct
14 Correct 68 ms 23032 KB Output is correct
15 Correct 523 ms 92948 KB Output is correct
16 Correct 68 ms 25176 KB Output is correct
17 Correct 521 ms 96836 KB Output is correct
18 Correct 342 ms 95300 KB Output is correct
19 Correct 341 ms 95628 KB Output is correct
20 Correct 476 ms 100420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 50 ms 17800 KB Output is correct
4 Correct 310 ms 91712 KB Output is correct
5 Correct 350 ms 90696 KB Output is correct
6 Correct 432 ms 90692 KB Output is correct
7 Correct 493 ms 91204 KB Output is correct
8 Correct 541 ms 95052 KB Output is correct
9 Correct 310 ms 90628 KB Output is correct
10 Correct 312 ms 90308 KB Output is correct
11 Correct 243 ms 90216 KB Output is correct
12 Correct 253 ms 90432 KB Output is correct
13 Correct 338 ms 93228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 13 ms 13404 KB Output is correct
8 Correct 9 ms 13404 KB Output is correct
9 Correct 12 ms 13404 KB Output is correct
10 Correct 9 ms 13468 KB Output is correct
11 Correct 13 ms 13404 KB Output is correct
12 Correct 12 ms 13392 KB Output is correct
13 Correct 10 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 1 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 3 ms 12892 KB Output is correct
6 Correct 354 ms 87312 KB Output is correct
7 Correct 385 ms 87620 KB Output is correct
8 Correct 458 ms 87620 KB Output is correct
9 Correct 521 ms 94272 KB Output is correct
10 Correct 313 ms 89664 KB Output is correct
11 Correct 381 ms 93620 KB Output is correct
12 Correct 284 ms 94784 KB Output is correct
13 Correct 349 ms 89920 KB Output is correct
14 Correct 386 ms 89664 KB Output is correct
15 Correct 445 ms 90332 KB Output is correct
16 Correct 266 ms 89384 KB Output is correct
17 Correct 357 ms 89548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12632 KB Output is correct
2 Correct 2 ms 12636 KB Output is correct
3 Correct 1 ms 12636 KB Output is correct
4 Correct 1 ms 12636 KB Output is correct
5 Correct 2 ms 12632 KB Output is correct
6 Correct 44 ms 15444 KB Output is correct
7 Correct 75 ms 22608 KB Output is correct
8 Correct 248 ms 87104 KB Output is correct
9 Correct 242 ms 87388 KB Output is correct
10 Correct 260 ms 87104 KB Output is correct
11 Correct 265 ms 87284 KB Output is correct
12 Correct 264 ms 90144 KB Output is correct
13 Correct 235 ms 90176 KB Output is correct
14 Correct 209 ms 90180 KB Output is correct
15 Correct 2 ms 12632 KB Output is correct
16 Correct 1 ms 12636 KB Output is correct
17 Correct 1 ms 12636 KB Output is correct
18 Correct 1 ms 12636 KB Output is correct
19 Correct 2 ms 12636 KB Output is correct
20 Correct 3 ms 12892 KB Output is correct
21 Correct 36 ms 17964 KB Output is correct
22 Correct 2 ms 12636 KB Output is correct
23 Correct 3 ms 12892 KB Output is correct
24 Correct 35 ms 18076 KB Output is correct
25 Correct 36 ms 17844 KB Output is correct
26 Correct 34 ms 18152 KB Output is correct
27 Correct 35 ms 18088 KB Output is correct
28 Correct 68 ms 23032 KB Output is correct
29 Correct 523 ms 92948 KB Output is correct
30 Correct 68 ms 25176 KB Output is correct
31 Correct 521 ms 96836 KB Output is correct
32 Correct 342 ms 95300 KB Output is correct
33 Correct 341 ms 95628 KB Output is correct
34 Correct 476 ms 100420 KB Output is correct
35 Correct 2 ms 12632 KB Output is correct
36 Correct 2 ms 12636 KB Output is correct
37 Correct 50 ms 17800 KB Output is correct
38 Correct 310 ms 91712 KB Output is correct
39 Correct 350 ms 90696 KB Output is correct
40 Correct 432 ms 90692 KB Output is correct
41 Correct 493 ms 91204 KB Output is correct
42 Correct 541 ms 95052 KB Output is correct
43 Correct 310 ms 90628 KB Output is correct
44 Correct 312 ms 90308 KB Output is correct
45 Correct 243 ms 90216 KB Output is correct
46 Correct 253 ms 90432 KB Output is correct
47 Correct 338 ms 93228 KB Output is correct
48 Correct 2 ms 12636 KB Output is correct
49 Correct 2 ms 12636 KB Output is correct
50 Correct 1 ms 12636 KB Output is correct
51 Correct 1 ms 12636 KB Output is correct
52 Correct 2 ms 12636 KB Output is correct
53 Correct 3 ms 12636 KB Output is correct
54 Correct 13 ms 13404 KB Output is correct
55 Correct 9 ms 13404 KB Output is correct
56 Correct 12 ms 13404 KB Output is correct
57 Correct 9 ms 13468 KB Output is correct
58 Correct 13 ms 13404 KB Output is correct
59 Correct 12 ms 13392 KB Output is correct
60 Correct 10 ms 13404 KB Output is correct
61 Correct 67 ms 19544 KB Output is correct
62 Correct 95 ms 24912 KB Output is correct
63 Correct 305 ms 90224 KB Output is correct
64 Correct 345 ms 90396 KB Output is correct
65 Correct 421 ms 90644 KB Output is correct
66 Correct 482 ms 91460 KB Output is correct
67 Correct 519 ms 95040 KB Output is correct
68 Correct 325 ms 90948 KB Output is correct
69 Correct 385 ms 94528 KB Output is correct
70 Correct 322 ms 95292 KB Output is correct
71 Correct 369 ms 90604 KB Output is correct
72 Correct 417 ms 90528 KB Output is correct
73 Correct 479 ms 91204 KB Output is correct
74 Correct 312 ms 90176 KB Output is correct
75 Correct 336 ms 90436 KB Output is correct