Submission #1039824

# Submission time Handle Problem Language Result Execution time Memory
1039824 2024-07-31T09:54:10 Z AmirAli_H1 Comparing Plants (IOI20_plants) C++17
100 / 100
825 ms 198228 KB
// In the name of Allah

#include <bits/stdc++.h>
#include "plants.h"
using namespace std;

typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;

#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct node {
	ll lazy; pll min;
};

const int maxn = 2e5 + 7;
const int maxlg = 20;
const ll oo = 1e9 + 7;

int n, k;
int A[maxn], val[maxn];
set<int> st, stx; node t[4 * maxn];
int val1[maxn][maxlg], val2[maxn][maxlg];
pll up1[maxn][maxlg], up2[maxn][maxlg];
set<pll> stw; int arr[maxn];

bool cmp(int i, int j) {
	return (val[i] > val[j]);
}

void build(int v, int tl, int tr) {
	t[v].lazy = 0;
	if (tr - tl == 1) {
		t[v].min = Mp(A[tl], tl);
		return ;
	}
	int mid = (tl + tr) / 2;
	build(2 * v + 1, tl, mid); build(2 * v + 2, mid, tr);
	t[v].min = min(t[2 * v + 1].min, t[2 * v + 2].min); t[v].min.F += t[v].lazy;
}

void add_val(int v, int tl, int tr, int l, int r, ll x) {
	l = max(l, tl); r = min(r, tr);
	if (l >= tr || r <= tl) return ;
	if (l == tl && r == tr) {
		t[v].min.F += x; t[v].lazy += x;
		return ;
	}
	int mid = (tl + tr) / 2;
	add_val(2 * v + 1, tl, mid, l, r, x); add_val(2 * v + 2, mid, tr, l, r, x);
	t[v].min = min(t[2 * v + 1].min, t[2 * v + 2].min); t[v].min.F += t[v].lazy;
}

void checkx(int j) {
	stx.erase(j); int d = 0;
	auto it = st.lower_bound(j);
	if (it != st.begin()) {
		it--; d = (j - *it);
	}
	else d = (j - *st.rbegin() + n);
	if (d >= k) stx.insert(j);
}

void addx(int j) {
	st.insert(j); checkx(j);
	if (len(st) == 1) return ;
	auto it = st.upper_bound(j);
	if (it != st.end()) checkx(*it);
	else checkx(*st.begin());
}

ll get_val1(int v, ll x) {
	ll res = 0;
	for (int j = maxlg - 1; j >= 0; j--) {
		if (val1[v][j] <= x) {
			res += up1[v][j].F;
			v = up1[v][j].S;
		}
	}
	return res;
}

ll get_val2(int v, ll x) {
	ll res = 0;
	for (int j = maxlg - 1; j >= 0; j--) {
		if (val2[v][j] <= x) {
			res += up2[v][j].F;
			v = up2[v][j].S;
		}
	}
	return res;
}

void delx(int j) {
	st.erase(j); stx.erase(j);
	if (len(st) == 0) return ;
	auto it = st.upper_bound(j);
	if (it != st.end()) checkx(*it);
	else checkx(*st.begin());
}

void init(int Kx, vector<int> Ax) {
	n = len(Ax); k = Kx;
	
	for (int i = 0; i < n; i++) A[i] = Ax[i];
	build(0, 0, n);
	
	int sz = 0;
	while (sz < n) {
		while (t[0].min.F == 0) {
			int j = t[0].min.S;
			add_val(0, 0, n, j, j + 1, oo);
			addx(j);
		}
		int j = *stx.begin(); val[j] = sz++;
		int x1 = min(j, k - 1), x2 = (k - 1) - x1;
		add_val(0, 0, n, j - x1, j, -1);
		add_val(0, 0, n, n - x2, n, -1);
		delx(j);
	}
	
	stw.clear();
	for (int j = 0; j < k; j++) stw.insert(Mp(val[j], j));
	for (int i = 0; i < n; i++) {
		int j = (i + k - 1) % n, r = -1;
		
		auto it = stw.upper_bound(Mp(val[i], oo));
		if (it != stw.end()) r = (*it).S;
		else r = i;
		up1[i][0] = Mp((r - i + n) % n, r);
		val1[i][0] = val[r];
		
		it = stw.upper_bound(Mp(val[j], oo));
		if (it != stw.end()) r = (*it).S;
		else r = j;
		up2[j][0] = Mp((j - r + n) % n, r);
		val2[j][0] = val[r];
		
		stw.erase(Mp(val[i], i));
		r = (i + k) % n;
		stw.insert(Mp(val[r], r));
	}
	
	iota(arr, arr + n, 0); sort(arr, arr + n, cmp);
	for (int i = 0; i < n; i++) {
		int v = arr[i];
		for (int j = 1; j < maxlg; j++) {
			int u = up1[v][j - 1].S;
			up1[v][j] = Mp(up1[v][j - 1].F + up1[u][j - 1].F, up1[u][j - 1].S);
			val1[v][j] = val1[u][j - 1];
			
			u = up2[v][j - 1].S;
			up2[v][j] = Mp(up2[v][j - 1].F + up2[u][j - 1].F, up2[u][j - 1].S);
			val2[v][j] = val2[u][j - 1];
		}
	}
	
	return ;
}

int compare_plants(int x, int y) {
	if (val[x] < val[y]) {
		int d1 = (y - x + n) % n;
		int d2 = (x - y + n) % n;
		if (get_val1(x, val[y]) < d1 && get_val2(x, val[y]) < d2) return 0;
	}
	else {
		int d1 = (x - y + n) % n;
		int d2 = (y - x + n) % n;
		if (get_val1(y, val[x]) < d1 && get_val2(y, val[x]) < d2) return 0;
	}
	
	if (val[x] < val[y]) return 1;
	else return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19700 KB Output is correct
2 Correct 9 ms 19548 KB Output is correct
3 Correct 7 ms 19548 KB Output is correct
4 Correct 7 ms 20828 KB Output is correct
5 Correct 6 ms 19548 KB Output is correct
6 Correct 82 ms 22612 KB Output is correct
7 Correct 113 ms 40128 KB Output is correct
8 Correct 421 ms 187192 KB Output is correct
9 Correct 426 ms 186452 KB Output is correct
10 Correct 434 ms 186452 KB Output is correct
11 Correct 452 ms 186964 KB Output is correct
12 Correct 498 ms 186752 KB Output is correct
13 Correct 519 ms 191316 KB Output is correct
14 Correct 515 ms 181840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 19548 KB Output is correct
2 Correct 6 ms 19572 KB Output is correct
3 Correct 6 ms 21004 KB Output is correct
4 Correct 6 ms 19544 KB Output is correct
5 Correct 7 ms 21084 KB Output is correct
6 Correct 9 ms 20692 KB Output is correct
7 Correct 75 ms 26708 KB Output is correct
8 Correct 8 ms 21084 KB Output is correct
9 Correct 10 ms 20572 KB Output is correct
10 Correct 75 ms 26704 KB Output is correct
11 Correct 70 ms 26448 KB Output is correct
12 Correct 75 ms 27988 KB Output is correct
13 Correct 84 ms 26808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 19548 KB Output is correct
2 Correct 6 ms 19572 KB Output is correct
3 Correct 6 ms 21004 KB Output is correct
4 Correct 6 ms 19544 KB Output is correct
5 Correct 7 ms 21084 KB Output is correct
6 Correct 9 ms 20692 KB Output is correct
7 Correct 75 ms 26708 KB Output is correct
8 Correct 8 ms 21084 KB Output is correct
9 Correct 10 ms 20572 KB Output is correct
10 Correct 75 ms 26704 KB Output is correct
11 Correct 70 ms 26448 KB Output is correct
12 Correct 75 ms 27988 KB Output is correct
13 Correct 84 ms 26808 KB Output is correct
14 Correct 121 ms 38996 KB Output is correct
15 Correct 791 ms 189540 KB Output is correct
16 Correct 123 ms 41300 KB Output is correct
17 Correct 781 ms 193476 KB Output is correct
18 Correct 600 ms 191568 KB Output is correct
19 Correct 560 ms 192084 KB Output is correct
20 Correct 825 ms 198228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19548 KB Output is correct
2 Correct 6 ms 19584 KB Output is correct
3 Correct 68 ms 25392 KB Output is correct
4 Correct 520 ms 185172 KB Output is correct
5 Correct 508 ms 182668 KB Output is correct
6 Correct 553 ms 181976 KB Output is correct
7 Correct 611 ms 182608 KB Output is correct
8 Correct 704 ms 187476 KB Output is correct
9 Correct 452 ms 187272 KB Output is correct
10 Correct 468 ms 185764 KB Output is correct
11 Correct 486 ms 194132 KB Output is correct
12 Correct 519 ms 185172 KB Output is correct
13 Correct 587 ms 191828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 19544 KB Output is correct
2 Correct 6 ms 19548 KB Output is correct
3 Correct 6 ms 20872 KB Output is correct
4 Correct 6 ms 19548 KB Output is correct
5 Correct 6 ms 20824 KB Output is correct
6 Correct 8 ms 19800 KB Output is correct
7 Correct 17 ms 20436 KB Output is correct
8 Correct 16 ms 20568 KB Output is correct
9 Correct 19 ms 20564 KB Output is correct
10 Correct 18 ms 20556 KB Output is correct
11 Correct 17 ms 20556 KB Output is correct
12 Correct 17 ms 20600 KB Output is correct
13 Correct 17 ms 21852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 19544 KB Output is correct
2 Correct 6 ms 19548 KB Output is correct
3 Correct 6 ms 19548 KB Output is correct
4 Correct 6 ms 19548 KB Output is correct
5 Correct 8 ms 20572 KB Output is correct
6 Correct 493 ms 182132 KB Output is correct
7 Correct 519 ms 182100 KB Output is correct
8 Correct 579 ms 182544 KB Output is correct
9 Correct 657 ms 187648 KB Output is correct
10 Correct 442 ms 186328 KB Output is correct
11 Correct 547 ms 189780 KB Output is correct
12 Correct 437 ms 187212 KB Output is correct
13 Correct 479 ms 184912 KB Output is correct
14 Correct 531 ms 184404 KB Output is correct
15 Correct 589 ms 185172 KB Output is correct
16 Correct 438 ms 185528 KB Output is correct
17 Correct 458 ms 184400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 19700 KB Output is correct
2 Correct 9 ms 19548 KB Output is correct
3 Correct 7 ms 19548 KB Output is correct
4 Correct 7 ms 20828 KB Output is correct
5 Correct 6 ms 19548 KB Output is correct
6 Correct 82 ms 22612 KB Output is correct
7 Correct 113 ms 40128 KB Output is correct
8 Correct 421 ms 187192 KB Output is correct
9 Correct 426 ms 186452 KB Output is correct
10 Correct 434 ms 186452 KB Output is correct
11 Correct 452 ms 186964 KB Output is correct
12 Correct 498 ms 186752 KB Output is correct
13 Correct 519 ms 191316 KB Output is correct
14 Correct 515 ms 181840 KB Output is correct
15 Correct 7 ms 19548 KB Output is correct
16 Correct 6 ms 19572 KB Output is correct
17 Correct 6 ms 21004 KB Output is correct
18 Correct 6 ms 19544 KB Output is correct
19 Correct 7 ms 21084 KB Output is correct
20 Correct 9 ms 20692 KB Output is correct
21 Correct 75 ms 26708 KB Output is correct
22 Correct 8 ms 21084 KB Output is correct
23 Correct 10 ms 20572 KB Output is correct
24 Correct 75 ms 26704 KB Output is correct
25 Correct 70 ms 26448 KB Output is correct
26 Correct 75 ms 27988 KB Output is correct
27 Correct 84 ms 26808 KB Output is correct
28 Correct 121 ms 38996 KB Output is correct
29 Correct 791 ms 189540 KB Output is correct
30 Correct 123 ms 41300 KB Output is correct
31 Correct 781 ms 193476 KB Output is correct
32 Correct 600 ms 191568 KB Output is correct
33 Correct 560 ms 192084 KB Output is correct
34 Correct 825 ms 198228 KB Output is correct
35 Correct 6 ms 19548 KB Output is correct
36 Correct 6 ms 19584 KB Output is correct
37 Correct 68 ms 25392 KB Output is correct
38 Correct 520 ms 185172 KB Output is correct
39 Correct 508 ms 182668 KB Output is correct
40 Correct 553 ms 181976 KB Output is correct
41 Correct 611 ms 182608 KB Output is correct
42 Correct 704 ms 187476 KB Output is correct
43 Correct 452 ms 187272 KB Output is correct
44 Correct 468 ms 185764 KB Output is correct
45 Correct 486 ms 194132 KB Output is correct
46 Correct 519 ms 185172 KB Output is correct
47 Correct 587 ms 191828 KB Output is correct
48 Correct 7 ms 19544 KB Output is correct
49 Correct 6 ms 19548 KB Output is correct
50 Correct 6 ms 20872 KB Output is correct
51 Correct 6 ms 19548 KB Output is correct
52 Correct 6 ms 20824 KB Output is correct
53 Correct 8 ms 19800 KB Output is correct
54 Correct 17 ms 20436 KB Output is correct
55 Correct 16 ms 20568 KB Output is correct
56 Correct 19 ms 20564 KB Output is correct
57 Correct 18 ms 20556 KB Output is correct
58 Correct 17 ms 20556 KB Output is correct
59 Correct 17 ms 20600 KB Output is correct
60 Correct 17 ms 21852 KB Output is correct
61 Correct 64 ms 26964 KB Output is correct
62 Correct 106 ms 40784 KB Output is correct
63 Correct 424 ms 186192 KB Output is correct
64 Correct 463 ms 185256 KB Output is correct
65 Correct 534 ms 185428 KB Output is correct
66 Correct 602 ms 186152 KB Output is correct
67 Correct 721 ms 191200 KB Output is correct
68 Correct 470 ms 187300 KB Output is correct
69 Correct 617 ms 190544 KB Output is correct
70 Correct 520 ms 187984 KB Output is correct
71 Correct 510 ms 185680 KB Output is correct
72 Correct 545 ms 185224 KB Output is correct
73 Correct 593 ms 186088 KB Output is correct
74 Correct 419 ms 185940 KB Output is correct
75 Correct 453 ms 185284 KB Output is correct