Submission #432131

# Submission time Handle Problem Language Result Execution time Memory
432131 2021-06-17T21:32:13 Z alishahali1382 Comparing Plants (IOI20_plants) C++14
100 / 100
1866 ms 75056 KB
#include "plants.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
#define debug(x) {cerr<<#x<<"="<<x<<"\n";}
#define debug2(x, y) {cerr<<"{"<<#x<<", "<<#y<<"}={"<<x<<", "<<y<<"}\n";}
#define debugp(p) {cerr<<#p<<"={"<<p.first<<", "<<p.second<<"}\n";}
#define debugv(abcd) {cerr<<#abcd<<": "; for (auto dcba:abcd) cerr<<dcba<<", ";cerr<<"\n";}
#define pb push_back
#define all(x) x.begin(), x.end()
#define SZ(x) ((int)x.size())

const int inf=1000000100; // 1e9
const ll INF=10000000001000000; // 1e16
const int mod=1000000007;
const int MAXN=200010, LOG=20;

int n, m, k, x, y, u, v, t, a, b, posmx;
int A[MAXN], B[MAXN];
int mark1[MAXN], mark2[MAXN];
int L[LOG][MAXN], R[LOG][MAXN];
vector<int> G[MAXN], GR[MAXN];

struct Segment{
	pii seg[MAXN<<2];
	int lazy[MAXN<<2];
	pii Build(int id, int tl, int tr, int *A){
		if (tr-tl==1) return seg[id]={A[tl], tl};
		int mid=(tl+tr)>>1;
		return seg[id]=min(Build(id<<1, tl, mid, A), Build(id<<1 | 1, mid, tr, A));
	}
	inline void add_lazy(int id, int val){
		seg[id].first+=val;
		lazy[id]+=val;
	}
	inline void shift(int id){
		if (lazy[id]){
			add_lazy(id<<1, lazy[id]);
			add_lazy(id<<1 | 1, lazy[id]);
			lazy[id]=0;
		}
	}
	void Add(int id, int tl, int tr, int l, int r, int val){
		if (r<=tl || tr<=l) return ;
		if (l<=tl && tr<=r){
			add_lazy(id, val);
			return ;
		}
		shift(id);
		int mid=(tl+tr)>>1;
		Add(id<<1, tl, mid, l, r, val);
		Add(id<<1 | 1, mid, tr, l, r, val);
		seg[id]=min(seg[id<<1], seg[id<<1 | 1]);
	}
	pii Get(int id, int tl, int tr, int l, int r){
		if (r<=tl || tr<=l) return {inf, 0};
		if (l<=tl && tr<=r) return seg[id];
		shift(id);
		int mid=(tl+tr)>>1;
		return min(Get(id<<1, tl, mid, l, r), Get(id<<1 | 1, mid, tr, l, r));
	}
} seg1, seg2, seg3;

void relax(){
	while (!seg1.seg[1].first){
		int mx=seg1.seg[1].second;
		// cerr<<mx<<" became 0\n";
		seg1.Add(1, 0, n, mx, mx+1, inf);
		seg2.Add(1, 0, n, mx, mx+1, -inf);
		seg2.Add(1, 0, n, mx+1, mx+k, +1);
		seg2.Add(1, 0, n, 0, mx+k-n, +1);
	}
}

inline void add_edge(int u, int v){
	G[u].pb(v);
	GR[v].pb(u);
}

void init(int _k, vi _A){
	n=SZ(_A);
	k=_k;
	for (int i=0; i<n; i++) A[i]=_A[i];
	seg1.Build(1, 0, n, A);
	seg2.Build(1, 0, n, B); // any empty array
	seg3.Build(1, 0, n, B);
	seg2.Add(1, 0, n, 0, n, inf);
	seg3.Add(1, 0, n, 0, n, inf);
	relax();
	for (int i=0; i<n; i++){
		int mx=seg2.seg[1].second;
		B[mx]=n-i;
		
		seg2.Add(1, 0, n, mx, mx+1, inf);
		seg2.Add(1, 0, n, mx+1, mx+k, -1);
		seg2.Add(1, 0, n, 0, mx+k-n, -1);
		
		seg1.Add(1, 0, n, mx-k+1, mx, -1);
		seg1.Add(1, 0, n, n+mx-k+1, n, -1);
		relax();



		seg3.Add(1, 0, n, mx, mx+1, -inf+B[mx]);
		pii l=min(seg3.Get(1, 0, n, mx-k+1, mx), seg3.Get(1, 0, n, mx-k+1+n, n));
		pii r=min(seg3.Get(1, 0, n, mx+1, mx+k), seg3.Get(1, 0, n, 0, mx+k-n));

		if (l.first<inf) L[0][mx]=(n+mx-l.second)%n; //add_edge(mx, l.second);
		if (r.first<inf) R[0][mx]=(r.second-mx+n)%n; //add_edge(mx, r.second);
	}
	// for (int i=0; i<n; i++) cerr<<B[i]-1<<" \n"[i==n-1];
	for (int j=1; j<LOG; j++) for (int i=0; i<n; i++){
		L[j][i]=min(L[j-1][i] + L[j-1][(i-L[j-1][i]+n)%n], n);
		R[j][i]=min(R[j-1][i] + R[j-1][(i+R[j-1][i])%n], n);
	}

	return ;
}

int compare_plants(int x, int y){
	if (B[x]>B[y]) return -compare_plants(y, x);
	int l=x, dl=(x-y+n)%n;
	int r=x, dr=(y-x+n)%n;
	for (int i=LOG-1; ~i; i--){
		if (L[i][l]<=dl){
			dl-=L[i][l];
			l=(l-L[i][l]+n)%n;
		}
		if (R[i][r]<=dr){
			dr-=R[i][r];
			r=(r+R[i][r]+n)%n;
		}
	}
	if (B[l]<=B[y] && dl<k) return -1;
	if (B[r]<=B[y] && dr<k) return -1;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28620 KB Output is correct
2 Correct 15 ms 28652 KB Output is correct
3 Correct 15 ms 28620 KB Output is correct
4 Correct 15 ms 28620 KB Output is correct
5 Correct 15 ms 28620 KB Output is correct
6 Correct 111 ms 31492 KB Output is correct
7 Correct 210 ms 35604 KB Output is correct
8 Correct 1121 ms 70844 KB Output is correct
9 Correct 1120 ms 70860 KB Output is correct
10 Correct 1094 ms 70576 KB Output is correct
11 Correct 1074 ms 70256 KB Output is correct
12 Correct 1081 ms 70124 KB Output is correct
13 Correct 1124 ms 70096 KB Output is correct
14 Correct 1162 ms 70268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28620 KB Output is correct
2 Correct 15 ms 28728 KB Output is correct
3 Correct 16 ms 28620 KB Output is correct
4 Correct 15 ms 28700 KB Output is correct
5 Correct 16 ms 28684 KB Output is correct
6 Correct 21 ms 28948 KB Output is correct
7 Correct 115 ms 32536 KB Output is correct
8 Correct 19 ms 28748 KB Output is correct
9 Correct 20 ms 28876 KB Output is correct
10 Correct 113 ms 32544 KB Output is correct
11 Correct 147 ms 32492 KB Output is correct
12 Correct 147 ms 32672 KB Output is correct
13 Correct 112 ms 32532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28620 KB Output is correct
2 Correct 15 ms 28728 KB Output is correct
3 Correct 16 ms 28620 KB Output is correct
4 Correct 15 ms 28700 KB Output is correct
5 Correct 16 ms 28684 KB Output is correct
6 Correct 21 ms 28948 KB Output is correct
7 Correct 115 ms 32536 KB Output is correct
8 Correct 19 ms 28748 KB Output is correct
9 Correct 20 ms 28876 KB Output is correct
10 Correct 113 ms 32544 KB Output is correct
11 Correct 147 ms 32492 KB Output is correct
12 Correct 147 ms 32672 KB Output is correct
13 Correct 112 ms 32532 KB Output is correct
14 Correct 203 ms 35788 KB Output is correct
15 Correct 1866 ms 71408 KB Output is correct
16 Correct 194 ms 35688 KB Output is correct
17 Correct 1823 ms 71416 KB Output is correct
18 Correct 1330 ms 71132 KB Output is correct
19 Correct 1355 ms 70964 KB Output is correct
20 Correct 1552 ms 71404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 28696 KB Output is correct
2 Correct 16 ms 28612 KB Output is correct
3 Correct 130 ms 31860 KB Output is correct
4 Correct 1087 ms 71344 KB Output is correct
5 Correct 1195 ms 71320 KB Output is correct
6 Correct 1543 ms 73420 KB Output is correct
7 Correct 1747 ms 73364 KB Output is correct
8 Correct 1833 ms 73540 KB Output is correct
9 Correct 1265 ms 74332 KB Output is correct
10 Correct 1124 ms 74340 KB Output is correct
11 Correct 1122 ms 73240 KB Output is correct
12 Correct 1223 ms 73728 KB Output is correct
13 Correct 1350 ms 73976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28620 KB Output is correct
2 Correct 15 ms 28636 KB Output is correct
3 Correct 14 ms 28612 KB Output is correct
4 Correct 14 ms 28732 KB Output is correct
5 Correct 15 ms 28708 KB Output is correct
6 Correct 18 ms 28748 KB Output is correct
7 Correct 38 ms 29380 KB Output is correct
8 Correct 33 ms 29388 KB Output is correct
9 Correct 36 ms 29364 KB Output is correct
10 Correct 39 ms 29348 KB Output is correct
11 Correct 44 ms 29340 KB Output is correct
12 Correct 45 ms 29372 KB Output is correct
13 Correct 37 ms 29308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28620 KB Output is correct
2 Correct 14 ms 28624 KB Output is correct
3 Correct 16 ms 28620 KB Output is correct
4 Correct 15 ms 28620 KB Output is correct
5 Correct 19 ms 28876 KB Output is correct
6 Correct 959 ms 71496 KB Output is correct
7 Correct 1281 ms 73824 KB Output is correct
8 Correct 1714 ms 74052 KB Output is correct
9 Correct 1743 ms 74212 KB Output is correct
10 Correct 832 ms 73540 KB Output is correct
11 Correct 1284 ms 74176 KB Output is correct
12 Correct 846 ms 73328 KB Output is correct
13 Correct 954 ms 73584 KB Output is correct
14 Correct 1455 ms 73820 KB Output is correct
15 Correct 1613 ms 74012 KB Output is correct
16 Correct 871 ms 73412 KB Output is correct
17 Correct 929 ms 73768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28620 KB Output is correct
2 Correct 15 ms 28652 KB Output is correct
3 Correct 15 ms 28620 KB Output is correct
4 Correct 15 ms 28620 KB Output is correct
5 Correct 15 ms 28620 KB Output is correct
6 Correct 111 ms 31492 KB Output is correct
7 Correct 210 ms 35604 KB Output is correct
8 Correct 1121 ms 70844 KB Output is correct
9 Correct 1120 ms 70860 KB Output is correct
10 Correct 1094 ms 70576 KB Output is correct
11 Correct 1074 ms 70256 KB Output is correct
12 Correct 1081 ms 70124 KB Output is correct
13 Correct 1124 ms 70096 KB Output is correct
14 Correct 1162 ms 70268 KB Output is correct
15 Correct 15 ms 28620 KB Output is correct
16 Correct 15 ms 28728 KB Output is correct
17 Correct 16 ms 28620 KB Output is correct
18 Correct 15 ms 28700 KB Output is correct
19 Correct 16 ms 28684 KB Output is correct
20 Correct 21 ms 28948 KB Output is correct
21 Correct 115 ms 32536 KB Output is correct
22 Correct 19 ms 28748 KB Output is correct
23 Correct 20 ms 28876 KB Output is correct
24 Correct 113 ms 32544 KB Output is correct
25 Correct 147 ms 32492 KB Output is correct
26 Correct 147 ms 32672 KB Output is correct
27 Correct 112 ms 32532 KB Output is correct
28 Correct 203 ms 35788 KB Output is correct
29 Correct 1866 ms 71408 KB Output is correct
30 Correct 194 ms 35688 KB Output is correct
31 Correct 1823 ms 71416 KB Output is correct
32 Correct 1330 ms 71132 KB Output is correct
33 Correct 1355 ms 70964 KB Output is correct
34 Correct 1552 ms 71404 KB Output is correct
35 Correct 17 ms 28696 KB Output is correct
36 Correct 16 ms 28612 KB Output is correct
37 Correct 130 ms 31860 KB Output is correct
38 Correct 1087 ms 71344 KB Output is correct
39 Correct 1195 ms 71320 KB Output is correct
40 Correct 1543 ms 73420 KB Output is correct
41 Correct 1747 ms 73364 KB Output is correct
42 Correct 1833 ms 73540 KB Output is correct
43 Correct 1265 ms 74332 KB Output is correct
44 Correct 1124 ms 74340 KB Output is correct
45 Correct 1122 ms 73240 KB Output is correct
46 Correct 1223 ms 73728 KB Output is correct
47 Correct 1350 ms 73976 KB Output is correct
48 Correct 15 ms 28620 KB Output is correct
49 Correct 15 ms 28636 KB Output is correct
50 Correct 14 ms 28612 KB Output is correct
51 Correct 14 ms 28732 KB Output is correct
52 Correct 15 ms 28708 KB Output is correct
53 Correct 18 ms 28748 KB Output is correct
54 Correct 38 ms 29380 KB Output is correct
55 Correct 33 ms 29388 KB Output is correct
56 Correct 36 ms 29364 KB Output is correct
57 Correct 39 ms 29348 KB Output is correct
58 Correct 44 ms 29340 KB Output is correct
59 Correct 45 ms 29372 KB Output is correct
60 Correct 37 ms 29308 KB Output is correct
61 Correct 130 ms 33496 KB Output is correct
62 Correct 232 ms 37828 KB Output is correct
63 Correct 1235 ms 74268 KB Output is correct
64 Correct 1103 ms 74504 KB Output is correct
65 Correct 1513 ms 74688 KB Output is correct
66 Correct 1776 ms 74884 KB Output is correct
67 Correct 1816 ms 75056 KB Output is correct
68 Correct 1259 ms 74456 KB Output is correct
69 Correct 1456 ms 75040 KB Output is correct
70 Correct 1104 ms 74180 KB Output is correct
71 Correct 1288 ms 74484 KB Output is correct
72 Correct 1575 ms 74684 KB Output is correct
73 Correct 1754 ms 74852 KB Output is correct
74 Correct 1166 ms 74456 KB Output is correct
75 Correct 1151 ms 74480 KB Output is correct