Submission #738336

# Submission time Handle Problem Language Result Execution time Memory
738336 2023-05-08T14:23:27 Z bobthebuilder Comparing Plants (IOI20_plants) C++17
100 / 100
1206 ms 116072 KB
#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define lowb(x) (x&(-x))
#define ALL(_x) _x.begin(),_x.end()
#define pii pair<int,int>
#define f first
#define s second
#define SORT_UNIQUE(x) sort(ALL(x)),x.erase(unique(ALL(x)),x.end())
#define ll long long
#define MNTO(x,y) x=min(x,y)
#define MXTO(x,y) x=max(x,y)
const int maxn=2e5+5;
const int INF=0x3f3f3f3f;
namespace {
int seg[4*maxn],pos[4*maxn];
int arr[maxn];
int lazy[4*maxn];
int ans[2*maxn];
int cur;
int k;
int n;
}
void pushdown(int idx,int l,int r){
	if(!lazy[idx]){
		return;
	}
	seg[idx]+=lazy[idx];
	if(l==r){
		lazy[idx]=0;
		return;
	}
	lazy[idx*2]+=lazy[idx];
	lazy[idx*2+1]+=lazy[idx];
	lazy[idx]=0;
}
void build(int idx,int l,int r){
	
	if(l==r){
		seg[idx]=arr[l];
		pos[idx]=l;
		return;
	}
	int mid=(l+r)/2;
	build(idx*2,l,mid),build(idx*2+1,mid+1,r);
	seg[idx]=min(seg[idx*2],seg[idx*2+1]);
	if(seg[idx]==seg[idx*2]) pos[idx]=pos[idx*2];
	else pos[idx]=pos[idx*2+1];
}
void upd(int idx,int l,int r,int ql,int qr,int x){
	if(ql>qr) return;
	pushdown(idx,l,r);
	if(r<ql or l>qr) return;
	if(ql<=l and r<=qr){
		lazy[idx]+=x;
		pushdown(idx,l,r);
		return;
	}
	int mid=(l+r)/2;
	upd(idx*2,l,mid,ql,qr,x);
	upd(idx*2+1,mid+1,r,ql,qr,x);
	seg[idx]=min(seg[idx*2],seg[idx*2+1]);
	if(seg[idx]==seg[idx*2]) pos[idx]=pos[idx*2];
	else pos[idx]=pos[idx*2+1];
}
pii query(int idx,int l,int r,int ql,int qr){
	if(ql>qr) return {INF,-1};
	pushdown(idx,l,r);
	if(r<ql or l>qr) return {INF,-1};
	if(ql<=l and r<=qr){
		return {seg[idx],pos[idx]};
	}
	int mid=(l+r)/2;
	return min(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr));
}

void get(int z){
	//cout<<z<<'\n';
	pii mn;
	while(true){
		if(z-k<0){
			mn=min(query(1,0,n-1,0,z-1),query(1,0,n-1,n+(z-k),n-1));
		}
		else{
			mn=query(1,0,n-1,z-k,z-1);
		}
		if(mn.f==0){
			get(mn.s);
		}
		else{
			break;
		}
	}
	ans[z]=cur--;
	if(z-k<0){
		upd(1,0,n-1,0,z-1,-1);
		upd(1,0,n-1,n+(z-k),n-1,-1);
	}
	else{
		upd(1,0,n-1,z-k,z-1,-1);
	}
	upd(1,0,n-1,z,z,INF);
}
vector<vector<int>> nxti,nxtd;
void init(int K, std::vector<int> r) {
	k=K;
	--k;
	n=sz(r);
	REP(i,n){
		arr[i]=r[i];
	}
	cur=n;
	build(1,0,n-1);
	while(cur>0){
		int z=query(1,0,n-1,0,n-1).s;
		assert(query(1,0,n-1,0,n-1).f==0);
		get(z);
	}
	REP(i,n) ans[i+n]=ans[i];
	//REP(i,n) cout<<ans[i]<<' ';
	//cout<<'\n';
	set<pii> s;
	nxti.assign(2*n,vector<int>(20,-1));
	nxtd.assign(2*n,vector<int>(20,-1));
	for(int i=2*n-1;i>=0;i--){
		auto it=s.lower_bound({ans[i],i});
		nxti[i][0]=(it==s.end())?-1:(it->s);
		nxtd[i][0]=(it==s.begin())?-1:(prev(it)->s);
		//cout<<i<<' '<<nxti[i][0]<<' '<<nxtd[i][0]<<'\n';
		s.insert({ans[i],i});
		if(i+k<2*n){
			s.erase({ans[i+k],i+k});
		}
	}
	REP1(j,19){
		REP(i,2*n){
			if(nxti[i][j-1]!=-1){
				nxti[i][j]=nxti[nxti[i][j-1]][j-1];
			}
			else nxti[i][j]=-1;
			if(nxtd[i][j-1]!=-1){
				nxtd[i][j]=nxtd[nxtd[i][j-1]][j-1];
			}
			else nxtd[i][j]=-1;
		}
	}
}
bool ok(int x,int y,int t){
	auto &nxt=(t?nxtd:nxti);
	for(int j=19;j>=0;j--){
		if(nxt[x][j]!=-1 and y-nxt[x][j]>k){
			x=nxt[x][j];
		}
	}
	x=nxt[x][0];
	return (x!=-1 and y-x<=k and (t?ans[x]>=ans[y]:ans[x]<=ans[y]));
}
int compare_plants(int x, int y) {
	bool sam=(y-x<=k);
	if(ans[x]<ans[y]){
		sam|=ok(x,y,0);
		sam|=ok(y,x+n,1);
	}
	else{
		sam|=ok(x,y,1);
		sam|=ok(y,x+n,0);
	}
	if(!sam){
		return 0;
	}
	if(ans[x]>ans[y]) return 1;
	return -1;
} 
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 65 ms 3036 KB Output is correct
7 Correct 135 ms 13564 KB Output is correct
8 Correct 541 ms 106264 KB Output is correct
9 Correct 563 ms 106220 KB Output is correct
10 Correct 623 ms 106164 KB Output is correct
11 Correct 643 ms 106244 KB Output is correct
12 Correct 688 ms 106060 KB Output is correct
13 Correct 712 ms 106164 KB Output is correct
14 Correct 744 ms 106172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 82 ms 5888 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 106 ms 6004 KB Output is correct
11 Correct 79 ms 5792 KB Output is correct
12 Correct 84 ms 5968 KB Output is correct
13 Correct 81 ms 5936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 82 ms 5888 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 106 ms 6004 KB Output is correct
11 Correct 79 ms 5792 KB Output is correct
12 Correct 84 ms 5968 KB Output is correct
13 Correct 81 ms 5936 KB Output is correct
14 Correct 153 ms 14120 KB Output is correct
15 Correct 1127 ms 112472 KB Output is correct
16 Correct 150 ms 14100 KB Output is correct
17 Correct 1025 ms 112492 KB Output is correct
18 Correct 791 ms 111368 KB Output is correct
19 Correct 787 ms 111420 KB Output is correct
20 Correct 1014 ms 116072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 92 ms 4064 KB Output is correct
4 Correct 846 ms 108136 KB Output is correct
5 Correct 897 ms 106804 KB Output is correct
6 Correct 1080 ms 106732 KB Output is correct
7 Correct 1144 ms 107140 KB Output is correct
8 Correct 1105 ms 110896 KB Output is correct
9 Correct 896 ms 106876 KB Output is correct
10 Correct 906 ms 106520 KB Output is correct
11 Correct 725 ms 106220 KB Output is correct
12 Correct 838 ms 106788 KB Output is correct
13 Correct 833 ms 109536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 17 ms 1364 KB Output is correct
8 Correct 16 ms 1428 KB Output is correct
9 Correct 18 ms 1364 KB Output is correct
10 Correct 16 ms 1364 KB Output is correct
11 Correct 16 ms 1420 KB Output is correct
12 Correct 17 ms 1348 KB Output is correct
13 Correct 16 ms 1416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 3 ms 780 KB Output is correct
6 Correct 666 ms 108900 KB Output is correct
7 Correct 881 ms 109228 KB Output is correct
8 Correct 932 ms 109748 KB Output is correct
9 Correct 1006 ms 113740 KB Output is correct
10 Correct 660 ms 108924 KB Output is correct
11 Correct 876 ms 113204 KB Output is correct
12 Correct 658 ms 110540 KB Output is correct
13 Correct 727 ms 109072 KB Output is correct
14 Correct 944 ms 109156 KB Output is correct
15 Correct 1038 ms 109856 KB Output is correct
16 Correct 705 ms 108708 KB Output is correct
17 Correct 684 ms 109060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 65 ms 3036 KB Output is correct
7 Correct 135 ms 13564 KB Output is correct
8 Correct 541 ms 106264 KB Output is correct
9 Correct 563 ms 106220 KB Output is correct
10 Correct 623 ms 106164 KB Output is correct
11 Correct 643 ms 106244 KB Output is correct
12 Correct 688 ms 106060 KB Output is correct
13 Correct 712 ms 106164 KB Output is correct
14 Correct 744 ms 106172 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 5 ms 852 KB Output is correct
21 Correct 82 ms 5888 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 4 ms 852 KB Output is correct
24 Correct 106 ms 6004 KB Output is correct
25 Correct 79 ms 5792 KB Output is correct
26 Correct 84 ms 5968 KB Output is correct
27 Correct 81 ms 5936 KB Output is correct
28 Correct 153 ms 14120 KB Output is correct
29 Correct 1127 ms 112472 KB Output is correct
30 Correct 150 ms 14100 KB Output is correct
31 Correct 1025 ms 112492 KB Output is correct
32 Correct 791 ms 111368 KB Output is correct
33 Correct 787 ms 111420 KB Output is correct
34 Correct 1014 ms 116072 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 92 ms 4064 KB Output is correct
38 Correct 846 ms 108136 KB Output is correct
39 Correct 897 ms 106804 KB Output is correct
40 Correct 1080 ms 106732 KB Output is correct
41 Correct 1144 ms 107140 KB Output is correct
42 Correct 1105 ms 110896 KB Output is correct
43 Correct 896 ms 106876 KB Output is correct
44 Correct 906 ms 106520 KB Output is correct
45 Correct 725 ms 106220 KB Output is correct
46 Correct 838 ms 106788 KB Output is correct
47 Correct 833 ms 109536 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 1 ms 308 KB Output is correct
53 Correct 3 ms 468 KB Output is correct
54 Correct 17 ms 1364 KB Output is correct
55 Correct 16 ms 1428 KB Output is correct
56 Correct 18 ms 1364 KB Output is correct
57 Correct 16 ms 1364 KB Output is correct
58 Correct 16 ms 1420 KB Output is correct
59 Correct 17 ms 1348 KB Output is correct
60 Correct 16 ms 1416 KB Output is correct
61 Correct 74 ms 5820 KB Output is correct
62 Correct 127 ms 15680 KB Output is correct
63 Correct 598 ms 109460 KB Output is correct
64 Correct 767 ms 109768 KB Output is correct
65 Correct 1008 ms 110016 KB Output is correct
66 Correct 1110 ms 110620 KB Output is correct
67 Correct 1098 ms 114584 KB Output is correct
68 Correct 769 ms 109956 KB Output is correct
69 Correct 997 ms 114108 KB Output is correct
70 Correct 838 ms 111184 KB Output is correct
71 Correct 853 ms 109864 KB Output is correct
72 Correct 1129 ms 110020 KB Output is correct
73 Correct 1206 ms 110620 KB Output is correct
74 Correct 651 ms 109676 KB Output is correct
75 Correct 762 ms 109820 KB Output is correct