#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),(*tt)));
}else{
st.insert(make_pair((*tt)-(*it),(*tt)));
}*/
if(tt==s.end()) tt=s.begin();
st.insert(make_pair(((*tt)+a-(*it))%a,(*tt)));
}
cnt=a;
/*cout<<cnt<<" JJF";
exit(0);*/
//cout<<s.size()<<" "<<st.size()<<endl;
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);
}
/*cout<<i<<" kala1"<<endl;
cout<<s.size()<<" "<<st.size()<<endl;
exit(0);*/
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)));
/*cout<<i<<" kala2";
exit(0);*/
l=i+1;r=i+1;z=a+3;
upd(1,za,1);
/*cout<<i<<" kala3";
exit(0);*/
}
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;
}
}
}
/*
for(j=0; j<=3; j++){
for(i=0; i<a; i++){
cout<<lf[i][j]<<" ";
}
cout<<endl;
}
exit(0);
*/
return;
}
int compare_plants(int x, int y) {
/*if(f[x]>f[y]){
return 1;
}else{
return -1;
}*/
c=x;d=y;
/*if(c<=d){
l=c;r=d;
ll=l;rr=r;
}else{
l=c;r=a-1;
ll=0;rr=d;
}*/
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) break;
}
}
if((d+a-c)%a<=k&&f[c]>=f[d]){
return 1;
}
c=x;d=y;
/*if(c>=d){
l=d;r=c;
ll=l;rr=r;
}else{
l=d;r=a-1;
ll=0;rr=c;
}*/
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) break;
}
}
if((c+a-d)%a<=k&&f[c]>=f[d]){
return 1;
}
// c=y;d=x;
c=y;d=x;
/*if(c<=d){
l=c;r=d;
ll=l;rr=r;
}else{
l=c;r=a-1;
ll=0;rr=d;
}*/
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) break;
}
}
if((d+a-c)%a<=k&&f[c]>=f[d]){
return -1;
}
c=y;d=x;
/*if(c>=d){
l=d;r=c;
ll=l;rr=r;
}else{
l=d;r=a-1;
ll=0;rr=c;
}*/
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) break;
}
}
if((c+a-d)%a<=k&&f[c]>=f[d]){
return -1;
}
return 0;
}
/*int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
freopen("cowtour.out","r",stdin);
int TES=0,TE=0;
cin>>a>>k>>TES;
vector <int> Rr;
Rr.resize(a);
for(i=0; i<a; i++){
cin>>R[i];
RE[i]=R[i];
//Rr.push_back(c);
}
for(i=0; i<a; i++){
for(j=i+1; j<i+k; j++){
if(R[j%a]>R[i]) Rr[i]++;
}
}
init(k,Rr);
// for(i=0; i<a; i++){
// cout<<f[i]<<" ";
// }
// cout<<endl;
// return 0;
// for(TE=1; TE<=TES; TE++){
// cin>>c>>d;
// cout<<compare_plants(c,d)<<endl;
// }
for(int I=0; I<a; I++){
for(int J=0; J<a; J++){
if(I==J) continue;
int O=compare_plants(I,J);
if(RE[I]>RE[J]){
if(O!=1){
cout<<"WRONG ANSWER: "<<I<<" "<<J<<endl;
cout<<RE[I]<<" "<<RE[J]<<endl;
cout<<O<<endl;
return 0;
}
}else{
if(O!=-1){
cout<<"WRONG ANSWER: "<<I<<" "<<J<<endl;
cout<<RE[I]<<" "<<RE[J]<<endl;
cout<<O<<endl;
return 0;
}
}
}
}
cout<<"ACCEPTED";
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 |
300 KB |
Output is correct |
6 |
Correct |
106 ms |
4048 KB |
Output is correct |
7 |
Correct |
191 ms |
10104 KB |
Output is correct |
8 |
Correct |
739 ms |
54584 KB |
Output is correct |
9 |
Correct |
764 ms |
54264 KB |
Output is correct |
10 |
Correct |
852 ms |
54504 KB |
Output is correct |
11 |
Correct |
915 ms |
55364 KB |
Output is correct |
12 |
Correct |
848 ms |
55076 KB |
Output is correct |
13 |
Correct |
800 ms |
64028 KB |
Output is correct |
14 |
Correct |
749 ms |
45240 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 |
300 KB |
Output is correct |
6 |
Correct |
7 ms |
588 KB |
Output is correct |
7 |
Correct |
142 ms |
6224 KB |
Output is correct |
8 |
Correct |
4 ms |
460 KB |
Output is correct |
9 |
Correct |
6 ms |
588 KB |
Output is correct |
10 |
Correct |
145 ms |
6248 KB |
Output is correct |
11 |
Correct |
120 ms |
6212 KB |
Output is correct |
12 |
Correct |
175 ms |
6220 KB |
Output is correct |
13 |
Correct |
114 ms |
6236 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 |
300 KB |
Output is correct |
6 |
Correct |
7 ms |
588 KB |
Output is correct |
7 |
Correct |
142 ms |
6224 KB |
Output is correct |
8 |
Correct |
4 ms |
460 KB |
Output is correct |
9 |
Correct |
6 ms |
588 KB |
Output is correct |
10 |
Correct |
145 ms |
6248 KB |
Output is correct |
11 |
Correct |
120 ms |
6212 KB |
Output is correct |
12 |
Correct |
175 ms |
6220 KB |
Output is correct |
13 |
Correct |
114 ms |
6236 KB |
Output is correct |
14 |
Correct |
198 ms |
9900 KB |
Output is correct |
15 |
Correct |
1637 ms |
51840 KB |
Output is correct |
16 |
Correct |
235 ms |
9872 KB |
Output is correct |
17 |
Correct |
1692 ms |
51948 KB |
Output is correct |
18 |
Correct |
1199 ms |
54948 KB |
Output is correct |
19 |
Correct |
1141 ms |
50764 KB |
Output is correct |
20 |
Correct |
1695 ms |
55596 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 |
110 ms |
5340 KB |
Output is correct |
4 |
Correct |
966 ms |
51504 KB |
Output is correct |
5 |
Correct |
1053 ms |
46728 KB |
Output is correct |
6 |
Correct |
1258 ms |
45640 KB |
Output is correct |
7 |
Correct |
1371 ms |
46276 KB |
Output is correct |
8 |
Correct |
1527 ms |
50228 KB |
Output is correct |
9 |
Correct |
964 ms |
50000 KB |
Output is correct |
10 |
Correct |
1045 ms |
46912 KB |
Output is correct |
11 |
Correct |
841 ms |
64020 KB |
Output is correct |
12 |
Correct |
857 ms |
45480 KB |
Output is correct |
13 |
Correct |
1138 ms |
58636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
444 KB |
Output is correct |
7 |
Correct |
26 ms |
1276 KB |
Output is correct |
8 |
Correct |
23 ms |
1292 KB |
Output is correct |
9 |
Correct |
25 ms |
1284 KB |
Output is correct |
10 |
Correct |
25 ms |
1264 KB |
Output is correct |
11 |
Correct |
26 ms |
1260 KB |
Output is correct |
12 |
Correct |
30 ms |
1288 KB |
Output is correct |
13 |
Correct |
21 ms |
1316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
4 ms |
548 KB |
Output is correct |
6 |
Correct |
1039 ms |
44772 KB |
Output is correct |
7 |
Correct |
1129 ms |
44776 KB |
Output is correct |
8 |
Correct |
1117 ms |
45500 KB |
Output is correct |
9 |
Correct |
1476 ms |
49284 KB |
Output is correct |
10 |
Correct |
957 ms |
49108 KB |
Output is correct |
11 |
Correct |
1312 ms |
48764 KB |
Output is correct |
12 |
Correct |
813 ms |
50504 KB |
Output is correct |
13 |
Correct |
999 ms |
45852 KB |
Output is correct |
14 |
Correct |
1275 ms |
44808 KB |
Output is correct |
15 |
Correct |
1277 ms |
45392 KB |
Output is correct |
16 |
Correct |
898 ms |
47520 KB |
Output is correct |
17 |
Correct |
956 ms |
44996 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 |
300 KB |
Output is correct |
6 |
Correct |
106 ms |
4048 KB |
Output is correct |
7 |
Correct |
191 ms |
10104 KB |
Output is correct |
8 |
Correct |
739 ms |
54584 KB |
Output is correct |
9 |
Correct |
764 ms |
54264 KB |
Output is correct |
10 |
Correct |
852 ms |
54504 KB |
Output is correct |
11 |
Correct |
915 ms |
55364 KB |
Output is correct |
12 |
Correct |
848 ms |
55076 KB |
Output is correct |
13 |
Correct |
800 ms |
64028 KB |
Output is correct |
14 |
Correct |
749 ms |
45240 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
7 ms |
588 KB |
Output is correct |
21 |
Correct |
142 ms |
6224 KB |
Output is correct |
22 |
Correct |
4 ms |
460 KB |
Output is correct |
23 |
Correct |
6 ms |
588 KB |
Output is correct |
24 |
Correct |
145 ms |
6248 KB |
Output is correct |
25 |
Correct |
120 ms |
6212 KB |
Output is correct |
26 |
Correct |
175 ms |
6220 KB |
Output is correct |
27 |
Correct |
114 ms |
6236 KB |
Output is correct |
28 |
Correct |
198 ms |
9900 KB |
Output is correct |
29 |
Correct |
1637 ms |
51840 KB |
Output is correct |
30 |
Correct |
235 ms |
9872 KB |
Output is correct |
31 |
Correct |
1692 ms |
51948 KB |
Output is correct |
32 |
Correct |
1199 ms |
54948 KB |
Output is correct |
33 |
Correct |
1141 ms |
50764 KB |
Output is correct |
34 |
Correct |
1695 ms |
55596 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
110 ms |
5340 KB |
Output is correct |
38 |
Correct |
966 ms |
51504 KB |
Output is correct |
39 |
Correct |
1053 ms |
46728 KB |
Output is correct |
40 |
Correct |
1258 ms |
45640 KB |
Output is correct |
41 |
Correct |
1371 ms |
46276 KB |
Output is correct |
42 |
Correct |
1527 ms |
50228 KB |
Output is correct |
43 |
Correct |
964 ms |
50000 KB |
Output is correct |
44 |
Correct |
1045 ms |
46912 KB |
Output is correct |
45 |
Correct |
841 ms |
64020 KB |
Output is correct |
46 |
Correct |
857 ms |
45480 KB |
Output is correct |
47 |
Correct |
1138 ms |
58636 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
296 KB |
Output is correct |
50 |
Correct |
1 ms |
300 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
3 ms |
444 KB |
Output is correct |
54 |
Correct |
26 ms |
1276 KB |
Output is correct |
55 |
Correct |
23 ms |
1292 KB |
Output is correct |
56 |
Correct |
25 ms |
1284 KB |
Output is correct |
57 |
Correct |
25 ms |
1264 KB |
Output is correct |
58 |
Correct |
26 ms |
1260 KB |
Output is correct |
59 |
Correct |
30 ms |
1288 KB |
Output is correct |
60 |
Correct |
21 ms |
1316 KB |
Output is correct |
61 |
Correct |
117 ms |
5216 KB |
Output is correct |
62 |
Correct |
181 ms |
9416 KB |
Output is correct |
63 |
Correct |
885 ms |
47332 KB |
Output is correct |
64 |
Correct |
1149 ms |
45580 KB |
Output is correct |
65 |
Correct |
1214 ms |
45668 KB |
Output is correct |
66 |
Correct |
1271 ms |
46368 KB |
Output is correct |
67 |
Correct |
1619 ms |
50120 KB |
Output is correct |
68 |
Correct |
1068 ms |
50000 KB |
Output is correct |
69 |
Correct |
1523 ms |
49648 KB |
Output is correct |
70 |
Correct |
1082 ms |
51428 KB |
Output is correct |
71 |
Correct |
1213 ms |
46728 KB |
Output is correct |
72 |
Correct |
1333 ms |
45768 KB |
Output is correct |
73 |
Correct |
1317 ms |
46256 KB |
Output is correct |
74 |
Correct |
928 ms |
46708 KB |
Output is correct |
75 |
Correct |
1000 ms |
45968 KB |
Output is correct |