#include<bits/stdc++.h>
#include "plants.h"
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=4e5+10;
const int SS=1<<19;
const int INFi=1e9;
const ll INFl=1e16;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=2027865967;
const ll p=70032301;
const ull p2=913;
const int L=20;
int t[N],n,lazy[SS*2+10],k,li,t2[N],g[N],jp[N][L],jp2[N][L];
pair<int,int> seg[SS*2+10];
void push(int v){
seg[v*2].fi+=lazy[v],seg[v*2+1].fi+=lazy[v];
lazy[v*2]+=lazy[v],lazy[v*2+1]+=lazy[v];
lazy[v]=0;
}
void upd(int a,int b,int x,int p=0,int k=SS-1,int v=1){
if(p>b or k<a) return;
if(p>=a and k<=b){
lazy[v]+=x,seg[v].fi+=x;
return;
}
int sr=(p+k)/2;
push(v);
upd(a,b,x,p,sr,v*2),upd(a,b,x,sr+1,k,v*2+1);
if(seg[v*2].fi<seg[v*2+1].fi) seg[v]=seg[v*2];
else seg[v]=seg[v*2+1];
}
pair<int,int> qr(int a,int b,int p=0,int k=SS-1,int v=1){
if(p>b or k<a) return {INFi,-1};
if(p>=a and k<=b) return seg[v];
push(v);
int sr=(p+k)/2;
pair<int,int> q1=qr(a,b,p,sr,v*2),q2=qr(a,b,sr+1,k,v*2+1);
if(q1.fi<q2.fi) return q1;
return q2;
}
void build(int v){
lazy[v]=0;
if(v>=SS){
int id=v-SS;
if(id>=1 and id<=n) seg[v]={t[id],id};
else seg[v]={INFi,id};
}else{
build(v*2),build(v*2+1);
if(seg[v*2].fi<seg[v*2+1].fi) seg[v]=seg[v*2];
else seg[v]=seg[v*2+1];
}
}
void zn(int a){
if(a>=k){
pair<int,int> xd=qr(a-k+1,a-1);
while(xd.fi==0){
zn(xd.se);
xd=qr(a-k+1,a-1);
}
t2[a]=--li;
}else{
pair<int,int> xd=qr(1,a-1);
if(xd.fi!=0) xd=qr(n-(k-a)+1,n);
while(xd.fi==0){
zn(xd.se);
xd=qr(1,a-1);
if(xd.fi!=0) xd=qr(n-(k-a)+1,n);
}
t2[a]=--li;
}
if(a>=k) upd(a-k+1,a-1,-1);
else{
upd(1,a-1,-1);
upd(n-(k-a)+1,n,-1);
}
upd(a,a,INFi);
}
void init(int kk,vi r){
k=kk;
n=r.size();
for(int i=1;i<=n;i++) t[i]=r[i-1];
build(1);
li=n+1;
pair<int,int> curr=qr(1,n);
while(curr.fi==0){
zn(curr.se);
curr=qr(1,n);
}
for(int i=n+1;i<=n*2;i++) t2[i]=t2[i-n];
for(int i=1;i<=n;i++) g[t2[i]]=i;
n*=2;
for(int i=1;i<=n;i++) t[i]=INFi;
build(1);
n/=2;
for(int i=n;i>=1;i--){
upd(g[i],g[i],-INFi+i);
upd(g[i]+n,g[i]+n,-INFi+i);
pair<int,int> xd=qr(g[i]+1,g[i]+k-1);
if(xd.fi>=INFi) jp[g[i]][0]=g[i],jp[g[i]+n][0]=n+g[i];
else{
jp[g[i]][0]=xd.se;
if(xd.se<=n) jp[g[i]+n][0]=xd.se+n;
else jp[g[i]+n][0]=g[i]+n;
}
xd=qr(g[i]+n-k+1,g[i]+n-1);
if(xd.fi>=INFi) jp2[g[i]][0]=g[i],jp2[g[i]+n][0]=n+g[i];
else{
jp2[g[i]+n][0]=xd.se;
if(xd.se>n) jp2[g[i]][0]=xd.se-n;
else jp2[g[i]][0]=g[i];
}
}
//for(int i=1;i<=n;i++) cout<<t2[i]<<" ";
//cout<<"\n";
//for(int i=1;i<=n;i++) cout<<jp[i][0]<<" ";
//cout<<"\n";
for(int i2=1;i2<L;i2++){
for(int i=1;i<=n*2;i++){
jp[i][i2]=jp[jp[i][i2-1]][i2-1];
jp2[i][i2]=jp2[jp2[i][i2-1]][i2-1];
}
}
}
int compare_plants(int x,int y){
x++,y++;
int curr=x;
for(int i=L-1;i>=0;i--)
if(jp[curr][i]<=y) curr=jp[curr][i];
int curr2=y;
for(int i=L-1;i>=0;i--)
if(jp[curr2][i]<=x+n) curr2=jp[curr2][i];
if(t2[curr]<=t2[y] and y-curr<k) return -1;
if(t2[curr2]<=t2[x+n] and x+n-curr2<k) return 1;
curr=y;
for(int i=L-1;i>=0;i--){
if(jp2[curr][i]>=x) curr=jp2[curr][i];
}
curr2=x+n;
for(int i=L-1;i>=0;i--){
if(jp2[curr2][i]>=y) curr2=jp2[curr2][i];
}
if(t2[curr]<=t2[x] and curr-x<k) return 1;
if(t2[curr2]<=t2[y] and curr2-y<k) return -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
19036 KB |
Output is correct |
3 |
Correct |
7 ms |
19084 KB |
Output is correct |
4 |
Correct |
7 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
19036 KB |
Output is correct |
6 |
Correct |
62 ms |
21692 KB |
Output is correct |
7 |
Correct |
101 ms |
28240 KB |
Output is correct |
8 |
Correct |
449 ms |
83972 KB |
Output is correct |
9 |
Correct |
508 ms |
83792 KB |
Output is correct |
10 |
Correct |
451 ms |
84052 KB |
Output is correct |
11 |
Correct |
467 ms |
85072 KB |
Output is correct |
12 |
Correct |
485 ms |
87124 KB |
Output is correct |
13 |
Correct |
482 ms |
90192 KB |
Output is correct |
14 |
Correct |
461 ms |
83796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
18968 KB |
Output is correct |
3 |
Correct |
7 ms |
19036 KB |
Output is correct |
4 |
Correct |
7 ms |
19064 KB |
Output is correct |
5 |
Correct |
7 ms |
19036 KB |
Output is correct |
6 |
Correct |
10 ms |
19036 KB |
Output is correct |
7 |
Correct |
69 ms |
25748 KB |
Output is correct |
8 |
Correct |
8 ms |
19032 KB |
Output is correct |
9 |
Correct |
14 ms |
19100 KB |
Output is correct |
10 |
Correct |
82 ms |
25684 KB |
Output is correct |
11 |
Correct |
88 ms |
25684 KB |
Output is correct |
12 |
Correct |
70 ms |
25936 KB |
Output is correct |
13 |
Correct |
69 ms |
25680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
18968 KB |
Output is correct |
3 |
Correct |
7 ms |
19036 KB |
Output is correct |
4 |
Correct |
7 ms |
19064 KB |
Output is correct |
5 |
Correct |
7 ms |
19036 KB |
Output is correct |
6 |
Correct |
10 ms |
19036 KB |
Output is correct |
7 |
Correct |
69 ms |
25748 KB |
Output is correct |
8 |
Correct |
8 ms |
19032 KB |
Output is correct |
9 |
Correct |
14 ms |
19100 KB |
Output is correct |
10 |
Correct |
82 ms |
25684 KB |
Output is correct |
11 |
Correct |
88 ms |
25684 KB |
Output is correct |
12 |
Correct |
70 ms |
25936 KB |
Output is correct |
13 |
Correct |
69 ms |
25680 KB |
Output is correct |
14 |
Correct |
119 ms |
30544 KB |
Output is correct |
15 |
Correct |
894 ms |
87632 KB |
Output is correct |
16 |
Correct |
129 ms |
30292 KB |
Output is correct |
17 |
Correct |
974 ms |
87632 KB |
Output is correct |
18 |
Correct |
697 ms |
90196 KB |
Output is correct |
19 |
Correct |
671 ms |
87636 KB |
Output is correct |
20 |
Correct |
866 ms |
87636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19036 KB |
Output is correct |
2 |
Correct |
11 ms |
19036 KB |
Output is correct |
3 |
Correct |
116 ms |
18092 KB |
Output is correct |
4 |
Correct |
603 ms |
87888 KB |
Output is correct |
5 |
Correct |
621 ms |
87376 KB |
Output is correct |
6 |
Correct |
770 ms |
87124 KB |
Output is correct |
7 |
Correct |
762 ms |
87268 KB |
Output is correct |
8 |
Correct |
814 ms |
87636 KB |
Output is correct |
9 |
Correct |
619 ms |
87632 KB |
Output is correct |
10 |
Correct |
564 ms |
87456 KB |
Output is correct |
11 |
Correct |
490 ms |
93012 KB |
Output is correct |
12 |
Correct |
497 ms |
86868 KB |
Output is correct |
13 |
Correct |
660 ms |
91472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19036 KB |
Output is correct |
2 |
Correct |
8 ms |
19036 KB |
Output is correct |
3 |
Correct |
7 ms |
19036 KB |
Output is correct |
4 |
Correct |
7 ms |
19036 KB |
Output is correct |
5 |
Correct |
7 ms |
19088 KB |
Output is correct |
6 |
Correct |
8 ms |
19036 KB |
Output is correct |
7 |
Correct |
20 ms |
19944 KB |
Output is correct |
8 |
Correct |
18 ms |
19804 KB |
Output is correct |
9 |
Correct |
21 ms |
19804 KB |
Output is correct |
10 |
Correct |
18 ms |
19800 KB |
Output is correct |
11 |
Correct |
24 ms |
19800 KB |
Output is correct |
12 |
Correct |
20 ms |
19804 KB |
Output is correct |
13 |
Correct |
23 ms |
20056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
19032 KB |
Output is correct |
3 |
Correct |
7 ms |
19036 KB |
Output is correct |
4 |
Correct |
7 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
549 ms |
86068 KB |
Output is correct |
7 |
Correct |
649 ms |
86360 KB |
Output is correct |
8 |
Correct |
704 ms |
86592 KB |
Output is correct |
9 |
Correct |
828 ms |
86656 KB |
Output is correct |
10 |
Correct |
509 ms |
86896 KB |
Output is correct |
11 |
Correct |
660 ms |
86864 KB |
Output is correct |
12 |
Correct |
503 ms |
87892 KB |
Output is correct |
13 |
Correct |
592 ms |
86608 KB |
Output is correct |
14 |
Correct |
630 ms |
86440 KB |
Output is correct |
15 |
Correct |
726 ms |
86352 KB |
Output is correct |
16 |
Correct |
486 ms |
86864 KB |
Output is correct |
17 |
Correct |
534 ms |
86352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19032 KB |
Output is correct |
2 |
Correct |
7 ms |
19036 KB |
Output is correct |
3 |
Correct |
7 ms |
19084 KB |
Output is correct |
4 |
Correct |
7 ms |
19036 KB |
Output is correct |
5 |
Correct |
8 ms |
19036 KB |
Output is correct |
6 |
Correct |
62 ms |
21692 KB |
Output is correct |
7 |
Correct |
101 ms |
28240 KB |
Output is correct |
8 |
Correct |
449 ms |
83972 KB |
Output is correct |
9 |
Correct |
508 ms |
83792 KB |
Output is correct |
10 |
Correct |
451 ms |
84052 KB |
Output is correct |
11 |
Correct |
467 ms |
85072 KB |
Output is correct |
12 |
Correct |
485 ms |
87124 KB |
Output is correct |
13 |
Correct |
482 ms |
90192 KB |
Output is correct |
14 |
Correct |
461 ms |
83796 KB |
Output is correct |
15 |
Correct |
7 ms |
19032 KB |
Output is correct |
16 |
Correct |
7 ms |
18968 KB |
Output is correct |
17 |
Correct |
7 ms |
19036 KB |
Output is correct |
18 |
Correct |
7 ms |
19064 KB |
Output is correct |
19 |
Correct |
7 ms |
19036 KB |
Output is correct |
20 |
Correct |
10 ms |
19036 KB |
Output is correct |
21 |
Correct |
69 ms |
25748 KB |
Output is correct |
22 |
Correct |
8 ms |
19032 KB |
Output is correct |
23 |
Correct |
14 ms |
19100 KB |
Output is correct |
24 |
Correct |
82 ms |
25684 KB |
Output is correct |
25 |
Correct |
88 ms |
25684 KB |
Output is correct |
26 |
Correct |
70 ms |
25936 KB |
Output is correct |
27 |
Correct |
69 ms |
25680 KB |
Output is correct |
28 |
Correct |
119 ms |
30544 KB |
Output is correct |
29 |
Correct |
894 ms |
87632 KB |
Output is correct |
30 |
Correct |
129 ms |
30292 KB |
Output is correct |
31 |
Correct |
974 ms |
87632 KB |
Output is correct |
32 |
Correct |
697 ms |
90196 KB |
Output is correct |
33 |
Correct |
671 ms |
87636 KB |
Output is correct |
34 |
Correct |
866 ms |
87636 KB |
Output is correct |
35 |
Correct |
7 ms |
19036 KB |
Output is correct |
36 |
Correct |
11 ms |
19036 KB |
Output is correct |
37 |
Correct |
116 ms |
18092 KB |
Output is correct |
38 |
Correct |
603 ms |
87888 KB |
Output is correct |
39 |
Correct |
621 ms |
87376 KB |
Output is correct |
40 |
Correct |
770 ms |
87124 KB |
Output is correct |
41 |
Correct |
762 ms |
87268 KB |
Output is correct |
42 |
Correct |
814 ms |
87636 KB |
Output is correct |
43 |
Correct |
619 ms |
87632 KB |
Output is correct |
44 |
Correct |
564 ms |
87456 KB |
Output is correct |
45 |
Correct |
490 ms |
93012 KB |
Output is correct |
46 |
Correct |
497 ms |
86868 KB |
Output is correct |
47 |
Correct |
660 ms |
91472 KB |
Output is correct |
48 |
Correct |
7 ms |
19036 KB |
Output is correct |
49 |
Correct |
8 ms |
19036 KB |
Output is correct |
50 |
Correct |
7 ms |
19036 KB |
Output is correct |
51 |
Correct |
7 ms |
19036 KB |
Output is correct |
52 |
Correct |
7 ms |
19088 KB |
Output is correct |
53 |
Correct |
8 ms |
19036 KB |
Output is correct |
54 |
Correct |
20 ms |
19944 KB |
Output is correct |
55 |
Correct |
18 ms |
19804 KB |
Output is correct |
56 |
Correct |
21 ms |
19804 KB |
Output is correct |
57 |
Correct |
18 ms |
19800 KB |
Output is correct |
58 |
Correct |
24 ms |
19800 KB |
Output is correct |
59 |
Correct |
20 ms |
19804 KB |
Output is correct |
60 |
Correct |
23 ms |
20056 KB |
Output is correct |
61 |
Correct |
69 ms |
25496 KB |
Output is correct |
62 |
Correct |
111 ms |
30288 KB |
Output is correct |
63 |
Correct |
518 ms |
86868 KB |
Output is correct |
64 |
Correct |
556 ms |
86868 KB |
Output is correct |
65 |
Correct |
667 ms |
87124 KB |
Output is correct |
66 |
Correct |
735 ms |
87376 KB |
Output is correct |
67 |
Correct |
832 ms |
87396 KB |
Output is correct |
68 |
Correct |
585 ms |
87888 KB |
Output is correct |
69 |
Correct |
692 ms |
87888 KB |
Output is correct |
70 |
Correct |
561 ms |
88404 KB |
Output is correct |
71 |
Correct |
611 ms |
87380 KB |
Output is correct |
72 |
Correct |
683 ms |
87120 KB |
Output is correct |
73 |
Correct |
738 ms |
87284 KB |
Output is correct |
74 |
Correct |
505 ms |
86828 KB |
Output is correct |
75 |
Correct |
555 ms |
87120 KB |
Output is correct |