Submission #823519

# Submission time Handle Problem Language Result Execution time Memory
823519 2023-08-12T16:24:33 Z fatemetmhr Comparing Plants (IOI20_plants) C++17
100 / 100
1296 ms 178616 KB
// vaghan ide i nadaram chi benevisam dige :/


#include "plants.h"
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define fi     first
#define se     second
#define mp     make_pair
#define pb     push_back

typedef long long ll;

const int maxn5 = 2e5 + 10;
const int maxnt = 1e6 + 10;
const int lg    = 20;
const int maxn3 = 310;
const int mod   = 1e9 + 10;

int n, ind[maxn5];
pair <int, ll> ed[2][lg][maxn5];
vector <int> av;

namespace seg3{

	pair <int, int> mn[maxnt];

	void build(int l, int r, int v){
		mn[v] = {maxn5, -1};
		if(r - l == 1)
			return;
		int mid = (l + r) >> 1;
		build(l, mid, v * 2);
		build(mid, r, v * 2 + 1);
	}

	void upd(int l, int r, int id, int val, int v){
		if(r - l == 1){
			mn[v] = {val, l};
			return;
		}
		int mid = (l + r) >> 1;
		if(id < mid)
			upd(l, mid, id, val, v * 2);
		else
			upd(mid, r, id, val, v * 2 + 1);
		mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
	}

	pair <int, int> get_min(int l, int r, int lq, int rq, int v){
		if(rq <= l || r <= lq)
			return mp(maxn5, -1);
		if(lq <= l && r <= rq)
			return mn[v];
		int mid = (l + r) >> 1;
		return min(get_min(l, mid, lq, rq, v * 2), get_min(mid, r, lq, rq, v * 2 + 1));
	}
}



struct seg{

	ll lazy[maxnt];
	pair <ll, int> mn[maxnt];

	void upd(int l, int r, int id, int val, int v){
		if(r - l == 1){
			lazy[v] = 0;
			mn[v] = {val, l};
			return;
		}
		int mid = (l + r) >> 1;
		if(id < mid)
			upd(l, mid, id, val, v * 2);
		else
			upd(mid, r, id, val, v * 2 + 1);
		mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
		mn[v].fi += lazy[v];
	}

	void add(int l, int r, int lq, int rq, int val, int v){
		if(rq <= l || r <= lq)
			return;
		if(lq <= l && r <= rq){
			lazy[v] += val;
			mn[v].fi += val;
			return;
		}
		int mid = (l + r) >> 1;
		add(l, mid, lq, rq, val, v * 2);
		add(mid, r, lq, rq, val, v * 2 + 1);
		mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
		mn[v].fi += lazy[v];
	}
} seg1, seg2;

void init(int k, vector<int> r) {
	n = r.size();
	for(int i = 0; i < n; i++){
		seg1.upd(0, n, i, r[i], 1);
		seg2.upd(0, n, i, r[i], 1);
	}
	bool con = true;
	while(con){
		con = false;
		while(seg1.mn[1].fi == 0){
			con = true;
			int v = seg1.mn[1].se;
			//cout << "ok seg1 " << v << endl;
			seg1.upd(0, n, v, mod / 2, 1);
			if(v < n - 1)
				seg2.add(0, n, v + 1, min(n, v + k), 1, 1);
			if(v + k > n)
				seg2.add(0, n, 0, v + k - n, 1, 1);
		}
		if(seg2.mn[1].fi == 0){
			con = true;
			int v = seg2.mn[1].se;
			//cout << "ok seg2 " << v << ' ' << k << ' ' << (v - (k - 1)) << endl;
			av.pb(v);
			seg2.upd(0, n, v, mod / 2, 1);
			if(v < n - 1)
				seg2.add(0, n, v + 1, min(n, v + k), -1, 1);
			if(v + k > n)
				seg2.add(0, n, 0, v + k - n, -1, 1);
			if(v){
				seg1.add(0, n, max(0, v - (k - 1)), v, -1, 1);
				seg2.add(0, n, max(0, v - (k - 1)), v, -1, 1);
			}
			if(v - (k - 1) < 0){
				seg1.add(0, n, v - (k - 1) + n, n, -1, 1);
				seg2.add(0, n, v - (k - 1) + n, n, -1, 1);
			}
		}

	}
	//cout << av.size() << endl;
	reverse(all(av));
	for(int i = 0; i < n; i++){
		ind[av[i]] = i;
		//cout << av[i] << ' ';
	}
	//cout << endl;
	memset(ed, -1, sizeof ed);
	seg3::build(0, n, 1);
	for(int i = n - 1; i >= 0; i--){
		int v = av[i];
		pair <int, int> ans1 = {maxn5, -1}, ans2 = {maxn5, -1};
		if(v < n - 1)
			ans1 = seg3::get_min(0, n, v, min(n, v + k), 1);
		if(v + k > n)
			ans2 = seg3::get_min(0, n, 0, v + k - n, 1);
		ans1 = min(ans1, ans2);
		ed[0][0][v] = {ans1.se, (ans1.se - v + n) % n};
		ans1 = ans2 = {maxn5, -1};
		if(v)
			ans1 = seg3::get_min(0, n, max(0, v - k + 1), v, 1);
		if(v - k + 1 < 0)
			ans2 = seg3::get_min(0, n, v - k + 1 + n, n, 1);
		ans1 = min(ans1, ans2);
		ed[1][0][v] = {ans1.se, (v - ans1.se + n) % n};
		seg3::upd(0, n, v, i, 1);
	}
	for(int i = 1; i < lg; i++) for(int j = 0; j < n; j++){
		if(ed[0][i - 1][j].fi != -1)
			ed[0][i][j] = {ed[0][i - 1][ed[0][i - 1][j].fi].fi, ed[0][i - 1][ed[0][i - 1][j].fi].se + ed[0][i - 1][j].se};
		if(ed[1][i - 1][j].fi != -1)
			ed[1][i][j] = {ed[1][i - 1][ed[1][i - 1][j].fi].fi, ed[1][i - 1][ed[1][i - 1][j].fi].se + ed[1][i - 1][j].se};
	}
	return;
}

ll find_len(int x, int lim, int ty){
	ll len = 0;
	for(int i = lg - 1; i >= 0; i--)
		if(ed[ty][i][x].fi != -1 && ind[ed[ty][i][x].fi] <= lim){
			len += ed[ty][i][x].se;
			x = ed[ty][i][x].fi;
		}
	return len;
}

int compare_plants(int x, int y) {
	int sw = -1;
	if(ind[x] > ind[y]){
		swap(x, y);
		sw = 1;
	}
	ll len = find_len(x, ind[y], 0);
	if((y - x + n) % n <= len)
		return sw;
	len = find_len(x, ind[y], 1);
	if((x - y + n) % n <= len)
		return sw;
	return 0;

}
# Verdict Execution time Memory Grader output
1 Correct 51 ms 156804 KB Output is correct
2 Correct 50 ms 156804 KB Output is correct
3 Correct 49 ms 156836 KB Output is correct
4 Correct 52 ms 156884 KB Output is correct
5 Correct 49 ms 156796 KB Output is correct
6 Correct 97 ms 159696 KB Output is correct
7 Correct 170 ms 161316 KB Output is correct
8 Correct 566 ms 172784 KB Output is correct
9 Correct 573 ms 172756 KB Output is correct
10 Correct 598 ms 172672 KB Output is correct
11 Correct 614 ms 172748 KB Output is correct
12 Correct 650 ms 172772 KB Output is correct
13 Correct 796 ms 172776 KB Output is correct
14 Correct 646 ms 172776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 156876 KB Output is correct
2 Correct 52 ms 156748 KB Output is correct
3 Correct 48 ms 156812 KB Output is correct
4 Correct 50 ms 156756 KB Output is correct
5 Correct 50 ms 156836 KB Output is correct
6 Correct 54 ms 157000 KB Output is correct
7 Correct 154 ms 160228 KB Output is correct
8 Correct 52 ms 156840 KB Output is correct
9 Correct 54 ms 156900 KB Output is correct
10 Correct 140 ms 160244 KB Output is correct
11 Correct 134 ms 160116 KB Output is correct
12 Correct 132 ms 160260 KB Output is correct
13 Correct 152 ms 160236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 156876 KB Output is correct
2 Correct 52 ms 156748 KB Output is correct
3 Correct 48 ms 156812 KB Output is correct
4 Correct 50 ms 156756 KB Output is correct
5 Correct 50 ms 156836 KB Output is correct
6 Correct 54 ms 157000 KB Output is correct
7 Correct 154 ms 160228 KB Output is correct
8 Correct 52 ms 156840 KB Output is correct
9 Correct 54 ms 156900 KB Output is correct
10 Correct 140 ms 160244 KB Output is correct
11 Correct 134 ms 160116 KB Output is correct
12 Correct 132 ms 160260 KB Output is correct
13 Correct 152 ms 160236 KB Output is correct
14 Correct 207 ms 161540 KB Output is correct
15 Correct 1278 ms 174776 KB Output is correct
16 Correct 234 ms 161588 KB Output is correct
17 Correct 1296 ms 174804 KB Output is correct
18 Correct 847 ms 174728 KB Output is correct
19 Correct 872 ms 174816 KB Output is correct
20 Correct 1215 ms 174904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 156796 KB Output is correct
2 Correct 62 ms 156800 KB Output is correct
3 Correct 130 ms 159832 KB Output is correct
4 Correct 750 ms 174308 KB Output is correct
5 Correct 898 ms 174796 KB Output is correct
6 Correct 1078 ms 174844 KB Output is correct
7 Correct 1105 ms 174784 KB Output is correct
8 Correct 1238 ms 174964 KB Output is correct
9 Correct 846 ms 174736 KB Output is correct
10 Correct 791 ms 174696 KB Output is correct
11 Correct 777 ms 172764 KB Output is correct
12 Correct 794 ms 174792 KB Output is correct
13 Correct 978 ms 174820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 156748 KB Output is correct
2 Correct 54 ms 156796 KB Output is correct
3 Correct 56 ms 156860 KB Output is correct
4 Correct 53 ms 156812 KB Output is correct
5 Correct 53 ms 156748 KB Output is correct
6 Correct 54 ms 156880 KB Output is correct
7 Correct 68 ms 157528 KB Output is correct
8 Correct 68 ms 157484 KB Output is correct
9 Correct 69 ms 157832 KB Output is correct
10 Correct 67 ms 157796 KB Output is correct
11 Correct 68 ms 157808 KB Output is correct
12 Correct 67 ms 157780 KB Output is correct
13 Correct 67 ms 157856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 156852 KB Output is correct
2 Correct 52 ms 156856 KB Output is correct
3 Correct 54 ms 156876 KB Output is correct
4 Correct 54 ms 156748 KB Output is correct
5 Correct 56 ms 156884 KB Output is correct
6 Correct 618 ms 174688 KB Output is correct
7 Correct 778 ms 174768 KB Output is correct
8 Correct 991 ms 174804 KB Output is correct
9 Correct 1057 ms 174864 KB Output is correct
10 Correct 564 ms 174692 KB Output is correct
11 Correct 741 ms 174784 KB Output is correct
12 Correct 529 ms 174212 KB Output is correct
13 Correct 624 ms 174752 KB Output is correct
14 Correct 979 ms 174732 KB Output is correct
15 Correct 993 ms 174844 KB Output is correct
16 Correct 617 ms 174568 KB Output is correct
17 Correct 577 ms 174700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 156804 KB Output is correct
2 Correct 50 ms 156804 KB Output is correct
3 Correct 49 ms 156836 KB Output is correct
4 Correct 52 ms 156884 KB Output is correct
5 Correct 49 ms 156796 KB Output is correct
6 Correct 97 ms 159696 KB Output is correct
7 Correct 170 ms 161316 KB Output is correct
8 Correct 566 ms 172784 KB Output is correct
9 Correct 573 ms 172756 KB Output is correct
10 Correct 598 ms 172672 KB Output is correct
11 Correct 614 ms 172748 KB Output is correct
12 Correct 650 ms 172772 KB Output is correct
13 Correct 796 ms 172776 KB Output is correct
14 Correct 646 ms 172776 KB Output is correct
15 Correct 48 ms 156876 KB Output is correct
16 Correct 52 ms 156748 KB Output is correct
17 Correct 48 ms 156812 KB Output is correct
18 Correct 50 ms 156756 KB Output is correct
19 Correct 50 ms 156836 KB Output is correct
20 Correct 54 ms 157000 KB Output is correct
21 Correct 154 ms 160228 KB Output is correct
22 Correct 52 ms 156840 KB Output is correct
23 Correct 54 ms 156900 KB Output is correct
24 Correct 140 ms 160244 KB Output is correct
25 Correct 134 ms 160116 KB Output is correct
26 Correct 132 ms 160260 KB Output is correct
27 Correct 152 ms 160236 KB Output is correct
28 Correct 207 ms 161540 KB Output is correct
29 Correct 1278 ms 174776 KB Output is correct
30 Correct 234 ms 161588 KB Output is correct
31 Correct 1296 ms 174804 KB Output is correct
32 Correct 847 ms 174728 KB Output is correct
33 Correct 872 ms 174816 KB Output is correct
34 Correct 1215 ms 174904 KB Output is correct
35 Correct 63 ms 156796 KB Output is correct
36 Correct 62 ms 156800 KB Output is correct
37 Correct 130 ms 159832 KB Output is correct
38 Correct 750 ms 174308 KB Output is correct
39 Correct 898 ms 174796 KB Output is correct
40 Correct 1078 ms 174844 KB Output is correct
41 Correct 1105 ms 174784 KB Output is correct
42 Correct 1238 ms 174964 KB Output is correct
43 Correct 846 ms 174736 KB Output is correct
44 Correct 791 ms 174696 KB Output is correct
45 Correct 777 ms 172764 KB Output is correct
46 Correct 794 ms 174792 KB Output is correct
47 Correct 978 ms 174820 KB Output is correct
48 Correct 52 ms 156748 KB Output is correct
49 Correct 54 ms 156796 KB Output is correct
50 Correct 56 ms 156860 KB Output is correct
51 Correct 53 ms 156812 KB Output is correct
52 Correct 53 ms 156748 KB Output is correct
53 Correct 54 ms 156880 KB Output is correct
54 Correct 68 ms 157528 KB Output is correct
55 Correct 68 ms 157484 KB Output is correct
56 Correct 69 ms 157832 KB Output is correct
57 Correct 67 ms 157796 KB Output is correct
58 Correct 68 ms 157808 KB Output is correct
59 Correct 67 ms 157780 KB Output is correct
60 Correct 67 ms 157856 KB Output is correct
61 Correct 114 ms 161484 KB Output is correct
62 Correct 233 ms 163616 KB Output is correct
63 Correct 643 ms 177228 KB Output is correct
64 Correct 817 ms 177880 KB Output is correct
65 Correct 1006 ms 178092 KB Output is correct
66 Correct 1037 ms 178280 KB Output is correct
67 Correct 1294 ms 178616 KB Output is correct
68 Correct 809 ms 177716 KB Output is correct
69 Correct 832 ms 178508 KB Output is correct
70 Correct 799 ms 177120 KB Output is correct
71 Correct 877 ms 177876 KB Output is correct
72 Correct 1016 ms 178092 KB Output is correct
73 Correct 1064 ms 178444 KB Output is correct
74 Correct 645 ms 177544 KB Output is correct
75 Correct 755 ms 177928 KB Output is correct