#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
struct T{
int mn,pos;
}st[800001];
T operator +(T a, T b){
return (a.mn>b.mn?b:a);
}
int n,k,lazy[800001],p[200001],id,l[200001][20],r[200001][20],dl[200001][20],dr[200001][20],pos[200001];
vector <int> a;
void down(int node, int l, int r){
if (!lazy[node]||l==r)
return;
st[node<<1].mn+=lazy[node];
st[node<<1|1].mn+=lazy[node];
lazy[node<<1]+=lazy[node];
lazy[node<<1|1]+=lazy[node];
lazy[node]=0;
}
void build(int node, int l, int r){
lazy[node]=0;
if (l==r){
st[node]={a[l],l};
return;
}
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
st[node]=st[node<<1]+st[node<<1|1];
}
void update(int node, int l, int r, int u, int v, int val){
if (l>v||r<u||u>v)
return;
if (l>=u&&r<=v){
st[node].mn+=val;
lazy[node]+=val;
return;
}
down(node,l,r);
int mid=(l+r)>>1;
update(node<<1,l,mid,u,v,val);
update(node<<1|1,mid+1,r,u,v,val);
st[node]=st[node<<1]+st[node<<1|1];
}
T get(int node, int l, int r, int u, int v){
if (l>v||r<u||u>v)
return {INF,0};
if (l>=u&&r<=v)
return st[node];
down(node,l,r);
int mid=(l+r)>>1;
return get(node<<1,l,mid,u,v)+get(node<<1|1,mid+1,r,u,v);
}
T calc(int j){
if (j<k)
return get(1,0,n-1,0,j-1)+get(1,0,n-1,n+j-k+1,n-1);
else
return get(1,0,n-1,j-k+1,j-1);
}
void dfs(int i){
while (!calc(i).mn)
dfs(calc(i).pos);
p[i]=id;
id--;
if (i<k){
update(1,0,n-1,0,i-1,-1);
update(1,0,n-1,n+i-k+1,n-1,-1);
}
else
update(1,0,n-1,i-k+1,i-1,-1);
update(1,0,n-1,i,i,INF);
}
void init(int K, vector <int> R){
k=K;
a=R;
n=id=a.size();
build(1,0,n-1);
while (!st[1].mn)
dfs(st[1].pos);
for (int i=0;i<n;i++){
a[i]=-p[i];
pos[p[i]]=i;
}
build(1,0,n-1);
for (int j=n;j;j--){
int i=pos[j];
update(1,0,n-1,i,i,INF);
l[i][0]=(calc(i).mn>0?i:calc(i).pos);
dl[i][0]=(i-l[i][0]+n)%n;
r[i][0]=(calc((i+k)%n).mn>0?i:calc((i+k)%n).pos);
dr[i][0]=(r[i][0]-i+n)%n;
}
for (int j=1;j<20;j++)
for (int i=0;i<n;i++){
l[i][j]=l[l[i][j-1]][j-1];
dl[i][j]=min(dl[i][j-1]+dl[l[i][j-1]][j-1],n);
r[i][j]=r[r[i][j-1]][j-1];
dr[i][j]=min(dr[i][j-1]+dr[r[i][j-1]][j-1],n);
}
}
int check(int x, int y){
int z=x,dist=(x-y+n)%n;
for (int i=19;i>=0;i--)
if (dl[z][i]<dist){
dist-=dl[z][i];
z=l[z][i];
}
if (dl[z][0]>=dist&&p[z]>p[y])
return 1;
z=x,dist=(y-x+n)%n;
for (int i=19;i>=0;i--)
if (dr[z][i]<dist){
dist-=dr[z][i];
z=r[z][i];
}
return (dr[z][0]>=dist&&p[z]>p[y]);
}
int compare_plants(int x, int y){
return (check(x,y)?1:(check(y,x)?-1:0));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14792 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
70 ms |
17404 KB |
Output is correct |
7 |
Correct |
128 ms |
23892 KB |
Output is correct |
8 |
Correct |
582 ms |
77116 KB |
Output is correct |
9 |
Correct |
573 ms |
77136 KB |
Output is correct |
10 |
Correct |
597 ms |
77104 KB |
Output is correct |
11 |
Correct |
569 ms |
77112 KB |
Output is correct |
12 |
Correct |
622 ms |
77412 KB |
Output is correct |
13 |
Correct |
649 ms |
77096 KB |
Output is correct |
14 |
Correct |
627 ms |
77376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
5 ms |
14872 KB |
Output is correct |
7 |
Correct |
70 ms |
19540 KB |
Output is correct |
8 |
Correct |
4 ms |
14680 KB |
Output is correct |
9 |
Correct |
8 ms |
14684 KB |
Output is correct |
10 |
Correct |
68 ms |
19540 KB |
Output is correct |
11 |
Correct |
79 ms |
19536 KB |
Output is correct |
12 |
Correct |
98 ms |
19796 KB |
Output is correct |
13 |
Correct |
66 ms |
19540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
5 ms |
14872 KB |
Output is correct |
7 |
Correct |
70 ms |
19540 KB |
Output is correct |
8 |
Correct |
4 ms |
14680 KB |
Output is correct |
9 |
Correct |
8 ms |
14684 KB |
Output is correct |
10 |
Correct |
68 ms |
19540 KB |
Output is correct |
11 |
Correct |
79 ms |
19536 KB |
Output is correct |
12 |
Correct |
98 ms |
19796 KB |
Output is correct |
13 |
Correct |
66 ms |
19540 KB |
Output is correct |
14 |
Correct |
155 ms |
23892 KB |
Output is correct |
15 |
Correct |
1149 ms |
77132 KB |
Output is correct |
16 |
Correct |
126 ms |
23892 KB |
Output is correct |
17 |
Correct |
1101 ms |
77136 KB |
Output is correct |
18 |
Correct |
865 ms |
77132 KB |
Output is correct |
19 |
Correct |
875 ms |
77132 KB |
Output is correct |
20 |
Correct |
1035 ms |
77132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14796 KB |
Output is correct |
3 |
Correct |
85 ms |
19704 KB |
Output is correct |
4 |
Correct |
861 ms |
77540 KB |
Output is correct |
5 |
Correct |
906 ms |
77140 KB |
Output is correct |
6 |
Correct |
1081 ms |
76884 KB |
Output is correct |
7 |
Correct |
1166 ms |
77356 KB |
Output is correct |
8 |
Correct |
1151 ms |
77064 KB |
Output is correct |
9 |
Correct |
804 ms |
77160 KB |
Output is correct |
10 |
Correct |
736 ms |
76880 KB |
Output is correct |
11 |
Correct |
590 ms |
77060 KB |
Output is correct |
12 |
Correct |
743 ms |
77140 KB |
Output is correct |
13 |
Correct |
820 ms |
77088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
4 ms |
14872 KB |
Output is correct |
7 |
Correct |
22 ms |
15704 KB |
Output is correct |
8 |
Correct |
14 ms |
15708 KB |
Output is correct |
9 |
Correct |
18 ms |
15668 KB |
Output is correct |
10 |
Correct |
14 ms |
15708 KB |
Output is correct |
11 |
Correct |
19 ms |
15708 KB |
Output is correct |
12 |
Correct |
19 ms |
15672 KB |
Output is correct |
13 |
Correct |
13 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
5 ms |
14684 KB |
Output is correct |
6 |
Correct |
747 ms |
79360 KB |
Output is correct |
7 |
Correct |
922 ms |
79556 KB |
Output is correct |
8 |
Correct |
988 ms |
79952 KB |
Output is correct |
9 |
Correct |
1072 ms |
79956 KB |
Output is correct |
10 |
Correct |
699 ms |
79316 KB |
Output is correct |
11 |
Correct |
881 ms |
79908 KB |
Output is correct |
12 |
Correct |
697 ms |
79572 KB |
Output is correct |
13 |
Correct |
817 ms |
79388 KB |
Output is correct |
14 |
Correct |
930 ms |
79496 KB |
Output is correct |
15 |
Correct |
1042 ms |
79968 KB |
Output is correct |
16 |
Correct |
644 ms |
79232 KB |
Output is correct |
17 |
Correct |
736 ms |
79624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14792 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
70 ms |
17404 KB |
Output is correct |
7 |
Correct |
128 ms |
23892 KB |
Output is correct |
8 |
Correct |
582 ms |
77116 KB |
Output is correct |
9 |
Correct |
573 ms |
77136 KB |
Output is correct |
10 |
Correct |
597 ms |
77104 KB |
Output is correct |
11 |
Correct |
569 ms |
77112 KB |
Output is correct |
12 |
Correct |
622 ms |
77412 KB |
Output is correct |
13 |
Correct |
649 ms |
77096 KB |
Output is correct |
14 |
Correct |
627 ms |
77376 KB |
Output is correct |
15 |
Correct |
2 ms |
14684 KB |
Output is correct |
16 |
Correct |
2 ms |
14684 KB |
Output is correct |
17 |
Correct |
2 ms |
14684 KB |
Output is correct |
18 |
Correct |
2 ms |
14684 KB |
Output is correct |
19 |
Correct |
2 ms |
14684 KB |
Output is correct |
20 |
Correct |
5 ms |
14872 KB |
Output is correct |
21 |
Correct |
70 ms |
19540 KB |
Output is correct |
22 |
Correct |
4 ms |
14680 KB |
Output is correct |
23 |
Correct |
8 ms |
14684 KB |
Output is correct |
24 |
Correct |
68 ms |
19540 KB |
Output is correct |
25 |
Correct |
79 ms |
19536 KB |
Output is correct |
26 |
Correct |
98 ms |
19796 KB |
Output is correct |
27 |
Correct |
66 ms |
19540 KB |
Output is correct |
28 |
Correct |
155 ms |
23892 KB |
Output is correct |
29 |
Correct |
1149 ms |
77132 KB |
Output is correct |
30 |
Correct |
126 ms |
23892 KB |
Output is correct |
31 |
Correct |
1101 ms |
77136 KB |
Output is correct |
32 |
Correct |
865 ms |
77132 KB |
Output is correct |
33 |
Correct |
875 ms |
77132 KB |
Output is correct |
34 |
Correct |
1035 ms |
77132 KB |
Output is correct |
35 |
Correct |
2 ms |
14684 KB |
Output is correct |
36 |
Correct |
2 ms |
14796 KB |
Output is correct |
37 |
Correct |
85 ms |
19704 KB |
Output is correct |
38 |
Correct |
861 ms |
77540 KB |
Output is correct |
39 |
Correct |
906 ms |
77140 KB |
Output is correct |
40 |
Correct |
1081 ms |
76884 KB |
Output is correct |
41 |
Correct |
1166 ms |
77356 KB |
Output is correct |
42 |
Correct |
1151 ms |
77064 KB |
Output is correct |
43 |
Correct |
804 ms |
77160 KB |
Output is correct |
44 |
Correct |
736 ms |
76880 KB |
Output is correct |
45 |
Correct |
590 ms |
77060 KB |
Output is correct |
46 |
Correct |
743 ms |
77140 KB |
Output is correct |
47 |
Correct |
820 ms |
77088 KB |
Output is correct |
48 |
Correct |
2 ms |
14684 KB |
Output is correct |
49 |
Correct |
2 ms |
14684 KB |
Output is correct |
50 |
Correct |
2 ms |
14684 KB |
Output is correct |
51 |
Correct |
2 ms |
14684 KB |
Output is correct |
52 |
Correct |
2 ms |
14684 KB |
Output is correct |
53 |
Correct |
4 ms |
14872 KB |
Output is correct |
54 |
Correct |
22 ms |
15704 KB |
Output is correct |
55 |
Correct |
14 ms |
15708 KB |
Output is correct |
56 |
Correct |
18 ms |
15668 KB |
Output is correct |
57 |
Correct |
14 ms |
15708 KB |
Output is correct |
58 |
Correct |
19 ms |
15708 KB |
Output is correct |
59 |
Correct |
19 ms |
15672 KB |
Output is correct |
60 |
Correct |
13 ms |
15708 KB |
Output is correct |
61 |
Correct |
88 ms |
21236 KB |
Output is correct |
62 |
Correct |
137 ms |
26036 KB |
Output is correct |
63 |
Correct |
690 ms |
80212 KB |
Output is correct |
64 |
Correct |
818 ms |
80236 KB |
Output is correct |
65 |
Correct |
1026 ms |
80428 KB |
Output is correct |
66 |
Correct |
1145 ms |
80616 KB |
Output is correct |
67 |
Correct |
1106 ms |
80812 KB |
Output is correct |
68 |
Correct |
784 ms |
80124 KB |
Output is correct |
69 |
Correct |
991 ms |
80876 KB |
Output is correct |
70 |
Correct |
783 ms |
80416 KB |
Output is correct |
71 |
Correct |
882 ms |
80252 KB |
Output is correct |
72 |
Correct |
1043 ms |
80464 KB |
Output is correct |
73 |
Correct |
1182 ms |
80592 KB |
Output is correct |
74 |
Correct |
689 ms |
80124 KB |
Output is correct |
75 |
Correct |
770 ms |
80280 KB |
Output is correct |