#include <bits/stdc++.h>
#define f first
#define s second
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define m_p make_pair
#define fast_ioi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int N=2e5+1;
struct segtree{
pii t[4*N];
int p[4*N];
vec<int> a;
void push(int v,int tl,int tr){
for(auto &u : {v<<1,v<<1|1}){
t[u].f+=p[v];
p[u]+=p[v];
}
p[v]=0;
}
void build(int v,int tl,int tr){
p[v]=0;
if(tl==tr){
t[v]={a[tl],tl};
}
else{
int tm=(tl+tr)>>1;
build(v<<1,tl,tm);build(v<<1|1,tm+1,tr);
t[v]=min(t[v<<1],t[v<<1|1]);
}
}
void add(int l,int r,int x,int v,int tl,int tr){
if(tl>=l&&tr<=r){
t[v].f+=x;p[v]+=x;
// cout<<tl<<' '<<tr<<' '<<x<<' '<<t[v].f<<' '<<x<<endl;
return;
}
if(tl>r||tr<l) return;
int tm=(tl+tr)>>1;push(v,tl,tr);
add(l,r,x,v<<1,tl,tm);add(l,r,x,v<<1|1,tm+1,tr);
t[v]=min(t[v<<1],t[v<<1|1]);
}
pii get(int l,int r,int v,int tl,int tr){
if(tl>=l&&tr<=r) return t[v];
if(tl>r||tr<l) return m_p(1e9,-1);
int tm=(tl+tr)>>1;push(v,tl,tr);
return min(get(l,r,v<<1,tl,tm),get(l,r,v<<1|1,tm+1,tr));
}
}sega;
int n,k;
int h[N],it=0;
int _find(int i){
pii c;
if((i-k+1)<0){
int r=k-(i+1);
c=sega.get(n-r,n-1,1,0,n-1);
if(c.f>0){
c=sega.get(0,i-1,1,0,n-1);
}
}
else{
c=sega.get(i-k+1,i-1,1,0,n-1);
}
return (c.f<=0?c.s:-1);
}
int ht[N];
void place(int i){
sega.add(i,i,1e9,1,0,n-1);
int j=_find(i);
// cout<<"PLACE "<<i<<endl;
while(j!=-1){
place(j);
j=_find(i);
}
ht[i]=it++;
if((i-k+1)<0){
int r=k-(i+1);
sega.add(n-r,n-1,-1,1,0,n-1);
sega.add(0,i,-1,1,0,n-1);
}
else{
sega.add(i-k+1,i,-1,1,0,n-1);
}
}
int lft[N][20],rgt[N][20];
void init(int k, std::vector<int> r){
n=sz(r);::k=k;
sega.a=r;
sega.build(1,0,n-1);
fill(ht,ht+n,-1);
while(1){
pii c=sega.t[1];
if(c.f>0){
break;
}
int j=c.s;
place(j);
}
for(int i=0;i<n;i++) ht[i]=n-ht[i];
vec<int> ids(n);iota(all(ids),0);
sort(all(ids),[&](int i,int j){return ht[i]<ht[j];});
const int inf=(int)(1e9)-1;
for(int i=0;i<n;i++) sega.a[i]=inf;
sega.build(1,0,n-1);
for(auto &i : ids){
pii c=((i+1)>=k?sega.get(i-k+1,i-1,1,0,n-1):min(sega.get(0,i-1,1,0,n-1),sega.get(n-(k-(i+1)),n-1,1,0,n-1)));
if(c.f>=inf) lft[i][0]=0;
else lft[i][0]=(i+n-c.s)%n;
c=(i+k-1<n?sega.get(i+1,i+k-1,1,0,n-1):min(sega.get(i+1,n-1,1,0,n-1),sega.get(0,k-(n-i)-1,1,0,n-1)));
if(c.f>=inf) rgt[i][0]=0;
else rgt[i][0]=(c.s-i+n)%n;
sega.add(i,i,-inf-ht[i],1,0,n-1);
}
//
// for(int i=0;i<n;i++) cout<<i<<' '<<ht[i]<<' '<<lft[i][0]<<' '<<rgt[i][0]<<endl;
// cout<<endl;
for(int i=1;i<20;i++){
for(int j=0;j<n;j++){
lft[j][i]=lft[j][i-1]+lft[(j+n-lft[j][i-1])%n][i-1];
rgt[j][i]=rgt[j][i-1]+rgt[(j+rgt[j][i-1])%n][i-1];
if(lft[j][i]>=n) lft[j][i]=0;
if(rgt[j][i]>=n) rgt[j][i]=0;
}
}
// for(int i=0;i<n;i++){
// for(int j=0;j<20;j++)
// cout<<rgt[i][j]<<' ';
// }
}
int compare_plants(int x, int y){
int z=x;
for(int i=19;i>=0;i--){
if(rgt[z][i]<(y-z+n)%n)
z=(z+rgt[z][i])%n;
}
if(ht[z]>ht[y] && (y-z+n)%n<k) return 1;
z=y;
for(int i=19;i>=0;i--){
if(rgt[z][i]<(x-z+n)%n)
z=(z+rgt[z][i])%n;
}
if(ht[z]>ht[x] && (x-z+n)%n<k) return -1;
z=y;
for(int i=19;i>=0;i--){
if(lft[z][i]<(z-x+n)%n)
z=(z-lft[z][i]+n)%n;
}
if(ht[z]>ht[x] && (z-x+n)%n<k) return -1;
z=x;
for(int i=19;i>=0;i--){
if(lft[z][i]<(z-y+n)%n)
z=(z-lft[z][i]+n)%n;
}
if(ht[z]>ht[y] && (z-y+n)%n<k) return 1;
return 0;
}
/*
30 3 1
1 1 0 1 0 2 2 1 0 1 1 1 2 0 2 1 0 1 2 2 0 1 0 2 1 1 2 0 2 0
8 12
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6532 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
5 ms |
6484 KB |
Output is correct |
6 |
Correct |
191 ms |
9280 KB |
Output is correct |
7 |
Correct |
270 ms |
15280 KB |
Output is correct |
8 |
Correct |
541 ms |
49112 KB |
Output is correct |
9 |
Correct |
643 ms |
49120 KB |
Output is correct |
10 |
Correct |
610 ms |
49036 KB |
Output is correct |
11 |
Correct |
663 ms |
49124 KB |
Output is correct |
12 |
Correct |
661 ms |
49120 KB |
Output is correct |
13 |
Correct |
528 ms |
49028 KB |
Output is correct |
14 |
Correct |
710 ms |
49072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6504 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
13 ms |
6836 KB |
Output is correct |
7 |
Correct |
132 ms |
10272 KB |
Output is correct |
8 |
Correct |
8 ms |
6676 KB |
Output is correct |
9 |
Correct |
9 ms |
6748 KB |
Output is correct |
10 |
Correct |
130 ms |
10352 KB |
Output is correct |
11 |
Correct |
109 ms |
10220 KB |
Output is correct |
12 |
Correct |
173 ms |
10444 KB |
Output is correct |
13 |
Correct |
130 ms |
10364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6504 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
13 ms |
6836 KB |
Output is correct |
7 |
Correct |
132 ms |
10272 KB |
Output is correct |
8 |
Correct |
8 ms |
6676 KB |
Output is correct |
9 |
Correct |
9 ms |
6748 KB |
Output is correct |
10 |
Correct |
130 ms |
10352 KB |
Output is correct |
11 |
Correct |
109 ms |
10220 KB |
Output is correct |
12 |
Correct |
173 ms |
10444 KB |
Output is correct |
13 |
Correct |
130 ms |
10364 KB |
Output is correct |
14 |
Correct |
188 ms |
13104 KB |
Output is correct |
15 |
Correct |
1113 ms |
46208 KB |
Output is correct |
16 |
Correct |
195 ms |
13112 KB |
Output is correct |
17 |
Correct |
1185 ms |
46212 KB |
Output is correct |
18 |
Correct |
802 ms |
46120 KB |
Output is correct |
19 |
Correct |
764 ms |
46212 KB |
Output is correct |
20 |
Correct |
951 ms |
46208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
135 ms |
9708 KB |
Output is correct |
4 |
Correct |
758 ms |
50696 KB |
Output is correct |
5 |
Correct |
851 ms |
49468 KB |
Output is correct |
6 |
Correct |
939 ms |
49500 KB |
Output is correct |
7 |
Correct |
1024 ms |
49684 KB |
Output is correct |
8 |
Correct |
964 ms |
49920 KB |
Output is correct |
9 |
Correct |
732 ms |
49336 KB |
Output is correct |
10 |
Correct |
709 ms |
49232 KB |
Output is correct |
11 |
Correct |
569 ms |
49056 KB |
Output is correct |
12 |
Correct |
770 ms |
49288 KB |
Output is correct |
13 |
Correct |
671 ms |
49256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
5 ms |
6484 KB |
Output is correct |
5 |
Correct |
7 ms |
6580 KB |
Output is correct |
6 |
Correct |
10 ms |
6596 KB |
Output is correct |
7 |
Correct |
51 ms |
7484 KB |
Output is correct |
8 |
Correct |
30 ms |
7508 KB |
Output is correct |
9 |
Correct |
45 ms |
7488 KB |
Output is correct |
10 |
Correct |
32 ms |
7492 KB |
Output is correct |
11 |
Correct |
64 ms |
7560 KB |
Output is correct |
12 |
Correct |
40 ms |
7488 KB |
Output is correct |
13 |
Correct |
31 ms |
7488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6492 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6484 KB |
Output is correct |
5 |
Correct |
6 ms |
6716 KB |
Output is correct |
6 |
Correct |
776 ms |
48324 KB |
Output is correct |
7 |
Correct |
1004 ms |
48544 KB |
Output is correct |
8 |
Correct |
935 ms |
48816 KB |
Output is correct |
9 |
Correct |
1036 ms |
49012 KB |
Output is correct |
10 |
Correct |
695 ms |
48536 KB |
Output is correct |
11 |
Correct |
919 ms |
48976 KB |
Output is correct |
12 |
Correct |
710 ms |
50216 KB |
Output is correct |
13 |
Correct |
846 ms |
48596 KB |
Output is correct |
14 |
Correct |
923 ms |
48704 KB |
Output is correct |
15 |
Correct |
999 ms |
48820 KB |
Output is correct |
16 |
Correct |
637 ms |
48296 KB |
Output is correct |
17 |
Correct |
893 ms |
48416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6532 KB |
Output is correct |
4 |
Correct |
3 ms |
6484 KB |
Output is correct |
5 |
Correct |
5 ms |
6484 KB |
Output is correct |
6 |
Correct |
191 ms |
9280 KB |
Output is correct |
7 |
Correct |
270 ms |
15280 KB |
Output is correct |
8 |
Correct |
541 ms |
49112 KB |
Output is correct |
9 |
Correct |
643 ms |
49120 KB |
Output is correct |
10 |
Correct |
610 ms |
49036 KB |
Output is correct |
11 |
Correct |
663 ms |
49124 KB |
Output is correct |
12 |
Correct |
661 ms |
49120 KB |
Output is correct |
13 |
Correct |
528 ms |
49028 KB |
Output is correct |
14 |
Correct |
710 ms |
49072 KB |
Output is correct |
15 |
Correct |
4 ms |
6484 KB |
Output is correct |
16 |
Correct |
4 ms |
6504 KB |
Output is correct |
17 |
Correct |
4 ms |
6484 KB |
Output is correct |
18 |
Correct |
4 ms |
6484 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
13 ms |
6836 KB |
Output is correct |
21 |
Correct |
132 ms |
10272 KB |
Output is correct |
22 |
Correct |
8 ms |
6676 KB |
Output is correct |
23 |
Correct |
9 ms |
6748 KB |
Output is correct |
24 |
Correct |
130 ms |
10352 KB |
Output is correct |
25 |
Correct |
109 ms |
10220 KB |
Output is correct |
26 |
Correct |
173 ms |
10444 KB |
Output is correct |
27 |
Correct |
130 ms |
10364 KB |
Output is correct |
28 |
Correct |
188 ms |
13104 KB |
Output is correct |
29 |
Correct |
1113 ms |
46208 KB |
Output is correct |
30 |
Correct |
195 ms |
13112 KB |
Output is correct |
31 |
Correct |
1185 ms |
46212 KB |
Output is correct |
32 |
Correct |
802 ms |
46120 KB |
Output is correct |
33 |
Correct |
764 ms |
46212 KB |
Output is correct |
34 |
Correct |
951 ms |
46208 KB |
Output is correct |
35 |
Correct |
3 ms |
6484 KB |
Output is correct |
36 |
Correct |
3 ms |
6484 KB |
Output is correct |
37 |
Correct |
135 ms |
9708 KB |
Output is correct |
38 |
Correct |
758 ms |
50696 KB |
Output is correct |
39 |
Correct |
851 ms |
49468 KB |
Output is correct |
40 |
Correct |
939 ms |
49500 KB |
Output is correct |
41 |
Correct |
1024 ms |
49684 KB |
Output is correct |
42 |
Correct |
964 ms |
49920 KB |
Output is correct |
43 |
Correct |
732 ms |
49336 KB |
Output is correct |
44 |
Correct |
709 ms |
49232 KB |
Output is correct |
45 |
Correct |
569 ms |
49056 KB |
Output is correct |
46 |
Correct |
770 ms |
49288 KB |
Output is correct |
47 |
Correct |
671 ms |
49256 KB |
Output is correct |
48 |
Correct |
4 ms |
6484 KB |
Output is correct |
49 |
Correct |
3 ms |
6484 KB |
Output is correct |
50 |
Correct |
3 ms |
6484 KB |
Output is correct |
51 |
Correct |
5 ms |
6484 KB |
Output is correct |
52 |
Correct |
7 ms |
6580 KB |
Output is correct |
53 |
Correct |
10 ms |
6596 KB |
Output is correct |
54 |
Correct |
51 ms |
7484 KB |
Output is correct |
55 |
Correct |
30 ms |
7508 KB |
Output is correct |
56 |
Correct |
45 ms |
7488 KB |
Output is correct |
57 |
Correct |
32 ms |
7492 KB |
Output is correct |
58 |
Correct |
64 ms |
7560 KB |
Output is correct |
59 |
Correct |
40 ms |
7488 KB |
Output is correct |
60 |
Correct |
31 ms |
7488 KB |
Output is correct |
61 |
Correct |
241 ms |
11436 KB |
Output is correct |
62 |
Correct |
285 ms |
15276 KB |
Output is correct |
63 |
Correct |
708 ms |
49120 KB |
Output is correct |
64 |
Correct |
832 ms |
49468 KB |
Output is correct |
65 |
Correct |
1064 ms |
49540 KB |
Output is correct |
66 |
Correct |
1082 ms |
49764 KB |
Output is correct |
67 |
Correct |
1018 ms |
49880 KB |
Output is correct |
68 |
Correct |
759 ms |
49436 KB |
Output is correct |
69 |
Correct |
976 ms |
49844 KB |
Output is correct |
70 |
Correct |
876 ms |
50848 KB |
Output is correct |
71 |
Correct |
990 ms |
49428 KB |
Output is correct |
72 |
Correct |
932 ms |
49412 KB |
Output is correct |
73 |
Correct |
1037 ms |
49684 KB |
Output is correct |
74 |
Correct |
731 ms |
49184 KB |
Output is correct |
75 |
Correct |
797 ms |
49344 KB |
Output is correct |