Submission #563027

# Submission time Handle Problem Language Result Execution time Memory
563027 2022-05-16T05:08:42 Z 8e7 Comparing Plants (IOI20_plants) C++17
100 / 100
1382 ms 76088 KB
//Challenge: Accepted
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 1<<30;
struct segtree {
	int seg[4 * maxn], tag[4 * maxn];		
	void push(int cur, int l, int r) {
		if (!tag[cur]) return;
		seg[cur] += tag[cur];
		if (r - l > 1) {
			tag[cur*2] += tag[cur], tag[cur*2+1] += tag[cur];
		}
		tag[cur] = 0;
	}
	void pull(int cur, int l, int r) {
		if (r - l > 1) {
			int m = (l + r) / 2;	
			push(cur*2, l, m), push(cur*2+1, m, r);
		}
		seg[cur] = min(seg[cur*2], seg[cur*2+1]);
	}
	void init(int cur, int l, int r, vector<int> &v) {
		if (r <= l) return;
		if (r - l == 1) {
			seg[cur] = v[l];
			return;
		}
		int m = (l + r) / 2;
		init(cur*2, l, m, v);
		init(cur*2+1, m, r, v);
		pull(cur, l, r);
	}
	void modify(int cur, int l, int r, int ql, int qr, int v) {
		if (r <= l || ql >= r || qr <= l) return;
		push(cur, l, r);
		if (ql <= l && qr >= r) {
			tag[cur] += v;	
			return;
		}
		int m = (l + r) / 2;
		modify(cur*2, l, m, ql, qr, v), modify(cur*2+1, m, r, ql, qr, v);
		pull(cur, l, r);
	}
	void change(int cur, int l, int r, int ind, int v) {
		if (r <= l) return;
		push(cur, l, r);
		if (r - l == 1) {
			seg[cur] = v;
			return;
		}
		int m = (l + r) / 2;
		if (ind < m) change(cur*2, l, m, ind, v);
		else change(cur*2+1, m, r, ind, v);
		pull(cur, l, r);
	}
	int getleft(int cur, int l, int r) {
		if (r <= l) return 0;
		push(cur, l, r);
		if (r - l == 1) return l;
		int m = (l + r) / 2;
		push(cur*2, l, m), push(cur*2+1, m, r);
		if (seg[cur*2] == 0) return getleft(cur*2, l, m);
		else return getleft(cur*2+1, m, r);
	}
	void getzero(int cur, int l, int r, int ql, int qr, vector<int> &v) { // gets all 0's in range [ql, qr)
		if (r <= l || ql >= r || qr <= l) return;	
		push(cur, l, r);
		if (seg[cur] > 0) return;
		if (r - l == 1) {
			v.push_back(l);
			return;
		}
		int m = (l + r) / 2;	
		getzero(cur*2, l, m, ql, qr, v);
		getzero(cur*2+1, m, r, ql, qr, v);
	}
	int query(int cur, int l, int r, int ql, int qr) {
		if (r <= l || ql >= r || qr <= l) return inf;
		if (ql <= l && qr >= r) return seg[cur];
		int m = (l + r) / 2;
		return min(query(cur*2, l, m, ql, qr), query(cur*2+1, m, r, ql, qr));
	}
	void print(int cur, int l, int r) {
		if (r <= l) return;
		push(cur, l, r);
		if (r - l == 1) {
			debug(seg[cur]);
			return;
		}
		int m = (l + r) / 2;
		print(cur*2, l, m), print(cur*2+1, m, r);
	}
}tr, aux, to;

int n, k;
int lt[18][maxn], rt[18][maxn], cor[maxn], ord[maxn];
int cl[18][maxn], cr[18][maxn];
void init(int K, vector<int> R) {
	n = R.size();
	k = K;
	tr.init(1, 0, n, R);
	aux.init(1, 0, n, R);

	auto add = [&] (int i, int d) {
		aux.modify(1, 0, n, i+1, min(n, i+k), d);	
		if (i + k > n) aux.modify(1, 0, n, 0, i + k - n, d); 	
	};
	for (int i = 0;i < n;i++) {
		to.change(1, 0, n, i, inf); 
		if (R[i] == 0) add(i, 1);
	}

	auto getmi = [&] (int l, int r) { //[l, r]
		int ret = inf;
		if (l < 0) {
			ret = min(to.query(1, 0, n, l+n, n), to.query(1, 0, n, 0, r+1)); 	
		} else if (r >= n) {
			ret = min(to.query(1, 0, n, l, n), to.query(1, 0, n, 0, r - n + 1));
		} else {
			ret = to.query(1, 0, n, l, r+1);
		}
		if (ret > maxn) return -1;
		return cor[ret];
	};
	lt[0][n] = rt[0][n] = n;
	for (int it = n;it > 0;it--) {
		int ind = aux.getleft(1, 0, n);
		add(ind, -1);
		int lef = getmi(ind-k+1, ind-1), rig = getmi(ind+1, ind+k-1);			
		cor[it] = ind, ord[ind] = it;
		to.change(1, 0, n, ind, it);

		if (lef != -1) lt[0][ind] = lef, cl[0][ind] = (lef > ind) ? 1 : 0;
		else lt[0][ind] = n;
		if (rig != -1) rt[0][ind] = rig, cr[0][ind] = (rig < ind) ? 1 : 0;
		else rt[0][ind] = n;

		vector<int> zero;
		tr.change(1, 0, n, ind, inf);
		aux.change(1, 0, n, ind, inf);

		tr.modify(1, 0, n, max(0, ind-k+1), ind, -1);
		aux.modify(1, 0, n, max(0, ind-k+1), ind, -1);
		tr.getzero(1, 0, n, max(0, ind-k+1), ind, zero);
		if (ind - k + 1 < 0) {
			tr.modify(1, 0, n, ind-k+1 + n, n, -1);
			aux.modify(1, 0, n, ind-k+1 + n, n, -1);
			tr.getzero(1, 0, n, ind-k+1 + n, n, zero);
		}
		for (auto i:zero) add(i, 1);	
	}
	pary(ord, ord + n);
	pary(lt[0], lt[0] + n);
	pary(rt[0], rt[0] + n);

	for (int i = 1;i < 18;i++) {
		for (int j = 0;j <= n;j++) {
			lt[i][j] = lt[i-1][lt[i-1][j]];
			cl[i][j] = cl[i-1][j] + cl[i-1][lt[i-1][j]];
			rt[i][j] = rt[i-1][rt[i-1][j]];
			cr[i][j] = cr[i-1][j] + cr[i-1][rt[i-1][j]];
		}
	}
	return;
}

int cdis(int x, int y) {
	if (x > y) swap(x, y);
	return min(abs(x - y), x + n - y);
}
int compare_plants(int x, int y) {
	if (2 * k > n) return (ord[x] < ord[y] ? -1 : 1);
	int tx = x, ty = y;	
	for (int i = 17;i >= 0;i--) {
		if (rt[i][tx] != n && (cr[i][tx] == 0 && rt[i][tx] <= y)) {
			tx = rt[i][tx];
		}
	}
	if (cdis(tx, ty) < k && ord[tx] <= ord[ty]) return -1; 

	tx = x, ty = y;				
	for (int i = 17;i >= 0;i--) {
		if (lt[i][ty] != n && (cl[i][ty] == 0 && lt[i][ty] >= x)) {
			ty = lt[i][ty];
		}
	}
	if (cdis(tx, ty) < k && ord[tx] >= ord[ty]) return 1; 

	tx = x, ty = y;	
	int cnt = 0;
	for (int i = 17;i >= 0;i--) {
		int nxt = lt[i][tx];
		if (nxt != n && ((cl[i][tx] == 1 && !cnt && nxt >= y) || (!cl[i][tx] && (nxt >= y || tx < y)))) {
			cnt += cl[i][tx];
			tx = nxt;
		}
	}
	if (cdis(tx, ty) < k && ord[tx] <= ord[ty]) return -1; 
 
 	tx = x, ty = y, cnt = 0;
	for (int i = 17;i >= 0;i--) {
		int nxt = rt[i][ty];
		if (nxt != n && ((cr[i][ty] == 1 && !cnt && nxt <= x) || (!cr[i][ty] && (nxt <= x || ty > x)))) {
			cnt += cr[i][ty];
			ty = nxt;
		}
	}
	if (cdis(tx, ty) < k && ord[tx] >= ord[ty]) return 1; 
	return 0;
}
/*
4 3 2
0 1 1 2
0 2
1 2

6 2 5
1 0 1 0 0 0
0 1
3 5
0 3
2 4
2 5
*/

Compilation message

plants.cpp: In member function 'void segtree::print(int, int, int)':
plants.cpp:13:20: warning: statement has no effect [-Wunused-value]
   13 | #define debug(...) 0
      |                    ^
plants.cpp:105:4: note: in expansion of macro 'debug'
  105 |    debug(seg[cur]);
      |    ^~~~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:14:19: warning: statement has no effect [-Wunused-value]
   14 | #define pary(...) 0
      |                   ^
plants.cpp:170:2: note: in expansion of macro 'pary'
  170 |  pary(ord, ord + n);
      |  ^~~~
plants.cpp:14:19: warning: statement has no effect [-Wunused-value]
   14 | #define pary(...) 0
      |                   ^
plants.cpp:171:2: note: in expansion of macro 'pary'
  171 |  pary(lt[0], lt[0] + n);
      |  ^~~~
plants.cpp:14:19: warning: statement has no effect [-Wunused-value]
   14 | #define pary(...) 0
      |                   ^
plants.cpp:172:2: note: in expansion of macro 'pary'
  172 |  pary(rt[0], rt[0] + n);
      |  ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 70 ms 4016 KB Output is correct
7 Correct 134 ms 11120 KB Output is correct
8 Correct 642 ms 71964 KB Output is correct
9 Correct 656 ms 72056 KB Output is correct
10 Correct 684 ms 71716 KB Output is correct
11 Correct 741 ms 71440 KB Output is correct
12 Correct 787 ms 71372 KB Output is correct
13 Correct 842 ms 71212 KB Output is correct
14 Correct 771 ms 71268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 5 ms 1108 KB Output is correct
7 Correct 65 ms 5368 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Correct 4 ms 1108 KB Output is correct
10 Correct 61 ms 5324 KB Output is correct
11 Correct 62 ms 5204 KB Output is correct
12 Correct 60 ms 5480 KB Output is correct
13 Correct 69 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 5 ms 1108 KB Output is correct
7 Correct 65 ms 5368 KB Output is correct
8 Correct 2 ms 852 KB Output is correct
9 Correct 4 ms 1108 KB Output is correct
10 Correct 61 ms 5324 KB Output is correct
11 Correct 62 ms 5204 KB Output is correct
12 Correct 60 ms 5480 KB Output is correct
13 Correct 69 ms 5368 KB Output is correct
14 Correct 123 ms 10816 KB Output is correct
15 Correct 1190 ms 72332 KB Output is correct
16 Correct 134 ms 10720 KB Output is correct
17 Correct 1221 ms 72384 KB Output is correct
18 Correct 745 ms 71936 KB Output is correct
19 Correct 809 ms 72104 KB Output is correct
20 Correct 1063 ms 72432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 95 ms 4156 KB Output is correct
4 Correct 926 ms 75100 KB Output is correct
5 Correct 1039 ms 75592 KB Output is correct
6 Correct 1220 ms 75696 KB Output is correct
7 Correct 1327 ms 75888 KB Output is correct
8 Correct 1382 ms 76088 KB Output is correct
9 Correct 944 ms 75320 KB Output is correct
10 Correct 967 ms 75372 KB Output is correct
11 Correct 846 ms 73440 KB Output is correct
12 Correct 813 ms 74728 KB Output is correct
13 Correct 1130 ms 74952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 4 ms 836 KB Output is correct
7 Correct 19 ms 1784 KB Output is correct
8 Correct 16 ms 1724 KB Output is correct
9 Correct 18 ms 1740 KB Output is correct
10 Correct 15 ms 1808 KB Output is correct
11 Correct 28 ms 1704 KB Output is correct
12 Correct 25 ms 1800 KB Output is correct
13 Correct 13 ms 1800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 4 ms 980 KB Output is correct
6 Correct 895 ms 74544 KB Output is correct
7 Correct 1030 ms 74824 KB Output is correct
8 Correct 1195 ms 75096 KB Output is correct
9 Correct 1251 ms 75380 KB Output is correct
10 Correct 826 ms 74512 KB Output is correct
11 Correct 992 ms 75216 KB Output is correct
12 Correct 797 ms 74120 KB Output is correct
13 Correct 982 ms 74564 KB Output is correct
14 Correct 1069 ms 74812 KB Output is correct
15 Correct 1166 ms 75060 KB Output is correct
16 Correct 739 ms 74328 KB Output is correct
17 Correct 815 ms 74636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 70 ms 4016 KB Output is correct
7 Correct 134 ms 11120 KB Output is correct
8 Correct 642 ms 71964 KB Output is correct
9 Correct 656 ms 72056 KB Output is correct
10 Correct 684 ms 71716 KB Output is correct
11 Correct 741 ms 71440 KB Output is correct
12 Correct 787 ms 71372 KB Output is correct
13 Correct 842 ms 71212 KB Output is correct
14 Correct 771 ms 71268 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 5 ms 1108 KB Output is correct
21 Correct 65 ms 5368 KB Output is correct
22 Correct 2 ms 852 KB Output is correct
23 Correct 4 ms 1108 KB Output is correct
24 Correct 61 ms 5324 KB Output is correct
25 Correct 62 ms 5204 KB Output is correct
26 Correct 60 ms 5480 KB Output is correct
27 Correct 69 ms 5368 KB Output is correct
28 Correct 123 ms 10816 KB Output is correct
29 Correct 1190 ms 72332 KB Output is correct
30 Correct 134 ms 10720 KB Output is correct
31 Correct 1221 ms 72384 KB Output is correct
32 Correct 745 ms 71936 KB Output is correct
33 Correct 809 ms 72104 KB Output is correct
34 Correct 1063 ms 72432 KB Output is correct
35 Correct 0 ms 724 KB Output is correct
36 Correct 1 ms 724 KB Output is correct
37 Correct 95 ms 4156 KB Output is correct
38 Correct 926 ms 75100 KB Output is correct
39 Correct 1039 ms 75592 KB Output is correct
40 Correct 1220 ms 75696 KB Output is correct
41 Correct 1327 ms 75888 KB Output is correct
42 Correct 1382 ms 76088 KB Output is correct
43 Correct 944 ms 75320 KB Output is correct
44 Correct 967 ms 75372 KB Output is correct
45 Correct 846 ms 73440 KB Output is correct
46 Correct 813 ms 74728 KB Output is correct
47 Correct 1130 ms 74952 KB Output is correct
48 Correct 1 ms 724 KB Output is correct
49 Correct 1 ms 724 KB Output is correct
50 Correct 1 ms 724 KB Output is correct
51 Correct 1 ms 724 KB Output is correct
52 Correct 1 ms 724 KB Output is correct
53 Correct 4 ms 836 KB Output is correct
54 Correct 19 ms 1784 KB Output is correct
55 Correct 16 ms 1724 KB Output is correct
56 Correct 18 ms 1740 KB Output is correct
57 Correct 15 ms 1808 KB Output is correct
58 Correct 28 ms 1704 KB Output is correct
59 Correct 25 ms 1800 KB Output is correct
60 Correct 13 ms 1800 KB Output is correct
61 Correct 105 ms 5852 KB Output is correct
62 Correct 163 ms 12868 KB Output is correct
63 Correct 809 ms 75084 KB Output is correct
64 Correct 953 ms 75448 KB Output is correct
65 Correct 1225 ms 75632 KB Output is correct
66 Correct 1328 ms 75928 KB Output is correct
67 Correct 1324 ms 76024 KB Output is correct
68 Correct 937 ms 75316 KB Output is correct
69 Correct 1069 ms 76052 KB Output is correct
70 Correct 946 ms 75100 KB Output is correct
71 Correct 1144 ms 75400 KB Output is correct
72 Correct 1269 ms 75912 KB Output is correct
73 Correct 1214 ms 76080 KB Output is correct
74 Correct 770 ms 75288 KB Output is correct
75 Correct 911 ms 75452 KB Output is correct