Submission #424116

# Submission time Handle Problem Language Result Execution time Memory
424116 2021-06-11T16:56:12 Z mosiashvililuka Comparing Plants (IOI20_plants) C++14
100 / 100
1702 ms 61116 KB
#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,i,j,ii,jj,zx,xc,k,R[200009],cnt,f[200009],seg[800009],seg2[800009],segmn[800009],za,l,r,z,zz,ll,rr,RE[200009];
int rg[200009][20],lf[200009][20];
set <int> s;
set <int>::iterator it,tt;
set <pair <int, int> > st;
set <pair <int, int> >::iterator IT;
void pushdown(int q, int w, int rr){
	if(q!=w){
		seg2[rr*2]+=seg2[rr];
		seg2[rr*2+1]+=seg2[rr];
	}
	segmn[rr]+=seg2[rr];
	seg2[rr]=0;
}
void upd(int q, int w, int rr){
	pushdown(q,w,rr);
	if(q>r||w<l) return;
	if(q>=l&&w<=r){
		seg2[rr]+=z;
		pushdown(q,w,rr);
		return;
	}
	upd(q,(q+w)/2,rr*2);
	upd((q+w)/2+1,w,rr*2+1);
	if(segmn[rr*2]<segmn[rr*2+1]){
		seg[rr]=seg[rr*2];segmn[rr]=segmn[rr*2];
	}
	if(segmn[rr*2]>segmn[rr*2+1]){
		seg[rr]=seg[rr*2+1];segmn[rr]=segmn[rr*2+1];
	}
	if(segmn[rr*2]==segmn[rr*2+1]){
		segmn[rr]=segmn[rr*2+1];
		seg[rr]=min(seg[rr*2],seg[rr*2+1]);
	}
}
void UPD(int q, int w, int rr){
	l=q+1;r=w+1;z=rr;
	upd(1,za,1);
}
void read(int q, int w, int rr){
	pushdown(q,w,rr);
	if(q>r||w<l) return;
	if(q>=l&&w<=r){
		if(z>segmn[rr]){
			z=segmn[rr];zz=seg[rr];
		}else{
			if(z==segmn[rr]){
				if(zz>seg[rr]) zz=seg[rr];
			}
		}
		return;
	}
	read(q,(q+w)/2,rr*2);
	read((q+w)/2+1,w,rr*2+1);
}
void READ(int q, int w){
	q++;w++;
	while(q<=w){
		l=q;r=w;z=a+3;zz=a+3;
		read(1,za,1);
		if(z!=0||zz>w) break;
		zz--;
		it=s.lower_bound(zz);
		if(it==s.end()) it=s.begin();
		if(it!=s.begin()){
			tt=it;tt--;
		}else{
			tt=s.end();tt--;
		}
		st.erase(st.lower_bound(make_pair(((*it)+a-(*tt))%a,(*it))));
		st.insert(make_pair(((*it)+a-zz)%a,(*it)));
		st.insert(make_pair((zz+a-(*tt))%a,zz));
		s.insert(zz);
		zz++;
		q=zz+1;
	}
}
void init(int Kk, vector<int> Rr) {
	a=Rr.size();k=Kk-1;
	for(i=0; i<a; i++){
		R[i]=Rr[i];
	}
	za=1;
	while(za<a) za*=2;
	for(i=0; i<a; i++){
		seg[za+i]=i+1;segmn[za+i]=R[i];
		if(R[i]==0){
			s.insert(i);
		}
	}
	for(i=za-1; i>=1; i--){
		if(segmn[i*2]<segmn[i*2+1]){
			seg[i]=seg[i*2];segmn[i]=segmn[i*2];
		}
		if(segmn[i*2]>segmn[i*2+1]){
			seg[i]=seg[i*2+1];segmn[i]=segmn[i*2+1];
		}
		if(segmn[i*2]==segmn[i*2+1]){
			segmn[i]=segmn[i*2+1];
			seg[i]=min(seg[i*2],seg[i*2+1]);
		}
	}
	for(it=s.begin(); it!=s.end(); it++){
		tt=it;tt++;
		if(tt==s.end()) tt=s.begin();
		st.insert(make_pair(((*tt)+a-(*it))%a,(*tt)));
	}
	cnt=a;
	while(cnt>0){
		IT=st.end();IT--;
		i=(*IT).second;
		f[i]=cnt;cnt--;
		if(cnt==0) break;
		if(i-k>=0){
			UPD(i-k,i-1,-1);
			READ(i-k,i-1);
		}else{
			UPD(0,i-1,-1);
			UPD(a-(k-i),a-1,-1);
			READ(0,i-1);
			READ(a-(k-i),a-1);
		}
		s.erase(s.lower_bound(i));
		it=s.lower_bound(i);
		if(it==s.end()) it=s.begin();
		if(it!=s.begin()){
			tt=it;tt--;
		}else{
			tt=s.end();tt--;
		}
		st.erase(st.lower_bound(make_pair(((*it)+a-i)%a,(*it))));
		st.erase(st.lower_bound(make_pair((i+a-(*tt))%a,i)));
		st.insert(make_pair(((*it)+a-(*tt))%a,(*it)));
		l=i+1;r=i+1;z=a+3;
		upd(1,za,1);
	}
	st.clear();
	for(j=0; j<k; j++){
		st.insert(make_pair(f[j],j));
	}
	for(i=0; i<a; i++){
		st.erase(st.lower_bound(make_pair(f[i],i)));
		st.insert(make_pair(f[(i+k)%a],(i+k)%a));
		IT=st.lower_bound(make_pair(f[i],0));
		if(IT==st.begin()){
			rg[i][0]=-1;
		}else{
			IT--;
			rg[i][0]=(*IT).second;
		}
	}
	st.clear();
	for(j=a-1; j>=a-k; j--){
		st.insert(make_pair(f[j],j));
	}
	for(i=a-1; i>=0; i--){
		st.erase(st.lower_bound(make_pair(f[i],i)));
		st.insert(make_pair(f[(i+a-k)%a],(i+a-k)%a));
		IT=st.lower_bound(make_pair(f[i],0));
		if(IT==st.begin()){
			lf[i][0]=-1;
		}else{
			IT--;
			lf[i][0]=(*IT).second;
		}
	}
	for(j=1; j<=18; j++){
		for(i=0; i<a; i++){
			if(rg[i][j-1]==-1){
				rg[i][j]=-1;
				continue;
			}
			zx=rg[rg[i][j-1]][j-1];
			if(zx==-1){
				rg[i][j]=-1;
				continue;
			}
			if(i<=zx){
				l=i;r=zx;
				ll=l;rr=r;
			}else{
				l=i;r=a-1;
				ll=0;rr=zx;
			}
			if((l<=rg[i][j-1]&&rg[i][j-1]<=r)||(ll<=rg[i][j-1]&&rg[i][j-1]<=rr)){
				rg[i][j]=zx;
			}else{
				rg[i][j]=-1;
				continue;
			}
		}
	}
	for(j=1; j<=18; j++){
		for(i=0; i<a; i++){
			if(lf[i][j-1]==-1){
				lf[i][j]=-1;
				continue;
			}
			zx=lf[lf[i][j-1]][j-1];
			if(zx==-1){
				lf[i][j]=-1;
				continue;
			}
			if(i>=zx){
				l=zx;r=i;
				ll=l;rr=r;
			}else{
				l=zx;r=a-1;
				ll=0;rr=i;
			}
			if((l<=lf[i][j-1]&&lf[i][j-1]<=r)||(ll<=lf[i][j-1]&&lf[i][j-1]<=rr)){
				lf[i][j]=zx;
			}else{
				lf[i][j]=-1;
				continue;
			}
		}
	}
	return;
}
int compare_plants(int x, int y) {
	c=x;d=y;
	for(j=18; j>=0; j--){
		if(c<=d){
			l=c;r=d;
			ll=l;rr=r;
		}else{
			l=c;r=a-1;
			ll=0;rr=d;
		}
		if(rg[c][j]==-1) continue;
		if((l<=rg[c][j]&&rg[c][j]<=r)||(ll<=rg[c][j]&&rg[c][j]<=rr)){
			c=rg[c][j];
		}
	}
	if((d+a-c)%a<=k&&f[c]>=f[d]){
		return 1;
	}
	c=x;d=y;
	for(j=18; j>=0; j--){
		if(c>=d){
			l=d;r=c;
			ll=l;rr=r;
		}else{
			l=d;r=a-1;
			ll=0;rr=c;
		}
		if(lf[c][j]==-1) continue;
		if((l<=lf[c][j]&&lf[c][j]<=r)||(ll<=lf[c][j]&&lf[c][j]<=rr)){
			c=lf[c][j];
		}
	}
	if((c+a-d)%a<=k&&f[c]>=f[d]){
		return 1;
	}
	c=y;d=x;
	for(j=18; j>=0; j--){
		if(c<=d){
			l=c;r=d;
			ll=l;rr=r;
		}else{
			l=c;r=a-1;
			ll=0;rr=d;
		}
		if(rg[c][j]==-1) continue;
		if((l<=rg[c][j]&&rg[c][j]<=r)||(ll<=rg[c][j]&&rg[c][j]<=rr)){
			c=rg[c][j];
		}
	}
	if((d+a-c)%a<=k&&f[c]>=f[d]){
		return -1;
	}
	c=y;d=x;
	for(j=18; j>=0; j--){
		if(c>=d){
			l=d;r=c;
			ll=l;rr=r;
		}else{
			l=d;r=a-1;
			ll=0;rr=c;
		}
		if(lf[c][j]==-1) continue;
		if((l<=lf[c][j]&&lf[c][j]<=r)||(ll<=lf[c][j]&&lf[c][j]<=rr)){
			c=lf[c][j];
		}
	}
	if((c+a-d)%a<=k&&f[c]>=f[d]){
		return -1;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 97 ms 3156 KB Output is correct
7 Correct 171 ms 7956 KB Output is correct
8 Correct 685 ms 51716 KB Output is correct
9 Correct 745 ms 51368 KB Output is correct
10 Correct 833 ms 51460 KB Output is correct
11 Correct 830 ms 52472 KB Output is correct
12 Correct 853 ms 52060 KB Output is correct
13 Correct 820 ms 61112 KB Output is correct
14 Correct 806 ms 42328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 6 ms 636 KB Output is correct
7 Correct 113 ms 4340 KB Output is correct
8 Correct 4 ms 332 KB Output is correct
9 Correct 6 ms 544 KB Output is correct
10 Correct 150 ms 4348 KB Output is correct
11 Correct 106 ms 4320 KB Output is correct
12 Correct 147 ms 4328 KB Output is correct
13 Correct 129 ms 4340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 6 ms 636 KB Output is correct
7 Correct 113 ms 4340 KB Output is correct
8 Correct 4 ms 332 KB Output is correct
9 Correct 6 ms 544 KB Output is correct
10 Correct 150 ms 4348 KB Output is correct
11 Correct 106 ms 4320 KB Output is correct
12 Correct 147 ms 4328 KB Output is correct
13 Correct 129 ms 4340 KB Output is correct
14 Correct 188 ms 7680 KB Output is correct
15 Correct 1525 ms 48200 KB Output is correct
16 Correct 200 ms 7652 KB Output is correct
17 Correct 1686 ms 48064 KB Output is correct
18 Correct 1172 ms 51664 KB Output is correct
19 Correct 1057 ms 47036 KB Output is correct
20 Correct 1702 ms 51776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 124 ms 3524 KB Output is correct
4 Correct 940 ms 48584 KB Output is correct
5 Correct 1099 ms 43728 KB Output is correct
6 Correct 1298 ms 42356 KB Output is correct
7 Correct 1327 ms 42788 KB Output is correct
8 Correct 1518 ms 46664 KB Output is correct
9 Correct 941 ms 46896 KB Output is correct
10 Correct 1002 ms 43856 KB Output is correct
11 Correct 796 ms 61116 KB Output is correct
12 Correct 834 ms 42312 KB Output is correct
13 Correct 1123 ms 55644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 25 ms 1004 KB Output is correct
8 Correct 21 ms 968 KB Output is correct
9 Correct 26 ms 968 KB Output is correct
10 Correct 22 ms 940 KB Output is correct
11 Correct 25 ms 936 KB Output is correct
12 Correct 26 ms 936 KB Output is correct
13 Correct 21 ms 952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 6 ms 460 KB Output is correct
6 Correct 1073 ms 42576 KB Output is correct
7 Correct 1119 ms 42272 KB Output is correct
8 Correct 1136 ms 42768 KB Output is correct
9 Correct 1371 ms 46476 KB Output is correct
10 Correct 925 ms 46904 KB Output is correct
11 Correct 1261 ms 46140 KB Output is correct
12 Correct 829 ms 48536 KB Output is correct
13 Correct 981 ms 43636 KB Output is correct
14 Correct 1050 ms 42356 KB Output is correct
15 Correct 1277 ms 42984 KB Output is correct
16 Correct 958 ms 45520 KB Output is correct
17 Correct 1012 ms 42800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 97 ms 3156 KB Output is correct
7 Correct 171 ms 7956 KB Output is correct
8 Correct 685 ms 51716 KB Output is correct
9 Correct 745 ms 51368 KB Output is correct
10 Correct 833 ms 51460 KB Output is correct
11 Correct 830 ms 52472 KB Output is correct
12 Correct 853 ms 52060 KB Output is correct
13 Correct 820 ms 61112 KB Output is correct
14 Correct 806 ms 42328 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 6 ms 636 KB Output is correct
21 Correct 113 ms 4340 KB Output is correct
22 Correct 4 ms 332 KB Output is correct
23 Correct 6 ms 544 KB Output is correct
24 Correct 150 ms 4348 KB Output is correct
25 Correct 106 ms 4320 KB Output is correct
26 Correct 147 ms 4328 KB Output is correct
27 Correct 129 ms 4340 KB Output is correct
28 Correct 188 ms 7680 KB Output is correct
29 Correct 1525 ms 48200 KB Output is correct
30 Correct 200 ms 7652 KB Output is correct
31 Correct 1686 ms 48064 KB Output is correct
32 Correct 1172 ms 51664 KB Output is correct
33 Correct 1057 ms 47036 KB Output is correct
34 Correct 1702 ms 51776 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 124 ms 3524 KB Output is correct
38 Correct 940 ms 48584 KB Output is correct
39 Correct 1099 ms 43728 KB Output is correct
40 Correct 1298 ms 42356 KB Output is correct
41 Correct 1327 ms 42788 KB Output is correct
42 Correct 1518 ms 46664 KB Output is correct
43 Correct 941 ms 46896 KB Output is correct
44 Correct 1002 ms 43856 KB Output is correct
45 Correct 796 ms 61116 KB Output is correct
46 Correct 834 ms 42312 KB Output is correct
47 Correct 1123 ms 55644 KB Output is correct
48 Correct 0 ms 332 KB Output is correct
49 Correct 0 ms 332 KB Output is correct
50 Correct 0 ms 332 KB Output is correct
51 Correct 0 ms 332 KB Output is correct
52 Correct 1 ms 332 KB Output is correct
53 Correct 3 ms 332 KB Output is correct
54 Correct 25 ms 1004 KB Output is correct
55 Correct 21 ms 968 KB Output is correct
56 Correct 26 ms 968 KB Output is correct
57 Correct 22 ms 940 KB Output is correct
58 Correct 25 ms 936 KB Output is correct
59 Correct 26 ms 936 KB Output is correct
60 Correct 21 ms 952 KB Output is correct
61 Correct 117 ms 3476 KB Output is correct
62 Correct 192 ms 7324 KB Output is correct
63 Correct 888 ms 44424 KB Output is correct
64 Correct 1111 ms 42484 KB Output is correct
65 Correct 1287 ms 42476 KB Output is correct
66 Correct 1304 ms 42860 KB Output is correct
67 Correct 1480 ms 46556 KB Output is correct
68 Correct 1073 ms 46888 KB Output is correct
69 Correct 1571 ms 46020 KB Output is correct
70 Correct 931 ms 48540 KB Output is correct
71 Correct 1145 ms 43624 KB Output is correct
72 Correct 1428 ms 42396 KB Output is correct
73 Correct 1278 ms 42776 KB Output is correct
74 Correct 942 ms 43616 KB Output is correct
75 Correct 1030 ms 42868 KB Output is correct