#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,int(v.size())) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=2e5+99,lg=20;
int n,k,p[N],a[N],pos[N],seg[N<<2],lz[N<<2];
vector<int> vec;
set<int> s,ok;
pair<int,ll> A[lg][N],B[lg][N];
void check(int x){
int len;
if(x==*s.begin()){
len=x-(*s.rbegin())+n;
}
else{
len=x-(*prev(s.lower_bound(x)));
}
if(len>=k){
ok.insert(x);
}
else{
if(ok.find(x)!=ok.end()){
ok.erase(x);
}
}
}
void adds(int x){
s.insert(x);
check(x);
if(x==*s.rbegin()){
check(*s.begin());
}
else{
check(*s.upper_bound(x));
}
}
void dels(int x){
s.erase(x);
if(*s.rbegin()<x){
check(*s.begin());
}
else{
check(*s.upper_bound(x));
}
}
void upd(int id){
seg[id]=min(seg[id<<1],seg[id<<1|1]);
}
void shift(int id){
lz[id<<1]+=lz[id];
lz[id<<1|1]+=lz[id];
seg[id<<1]+=lz[id];
seg[id<<1|1]+=lz[id];
lz[id]=0;
}
void build(int id=1,int l=0,int r=n){
if(l+1==r){
seg[id]=p[l];
return ;
}
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid,r);
upd(id);
}
void prt(int L,int R,int id=1,int l=0,int r=n){
if(seg[id]>1 || r<=L || R<=l) return ;
if(l+1==r){
vec.pb(l);
adds(l);
return ;
}
int mid=(l+r)>>1;
shift(id);
prt(L,R,id<<1,l,mid);
prt(L,R,id<<1|1,mid,r);
upd(id);
}
void add(int L,int R,int val,int id=1,int l=0,int r=n){
if(r<=L || R<=l) return ;
if(L<=l && r<=R){
lz[id]+=val;
seg[id]+=val;
return ;
}
int mid=(l+r)>>1;
shift(id);
add(L,R,val,id<<1,l,mid);
add(L,R,val,id<<1|1,mid,r);
upd(id);
}
void builda(){
build();
f(i,0,n){
if(p[i]==0){
s.insert(i);
}
}
f(i,0,n){
if(p[i]==0){
add(i,i+1,n+10);
check(i);
}
}
f_(T,n-1,0){
int x=(*ok.begin());
//eror(x);
ok.erase(x);
a[x]=T;
if(T==0) break ;
int l=(x-k+n+1)%n,r=x;
vec.clear();
if(r<l){
prt(0,r);
prt(l,n);
add(0,r,-1);
add(l,n,-1);
}
else{
prt(l,r);
add(l,r,-1);
}
dels(x);
for(auto g : vec) add(g,g+1,n+10);
}
f(i,0,n) pos[a[i]]=i;
}
void AB(){
set<int> s;
f(i,0,k-1) s.insert(a[i]);
f_(i,n-1,0){
int ida=-1,idb=-1;
if(*s.begin()<a[i]){
ida=pos[*prev(s.lower_bound(a[i]))];
}
if(*s.rbegin()>a[i]){
idb=pos[*s.lower_bound(a[i])];
}
s.insert(a[i]);
s.erase(a[(i+k-1)%n]);
if(ida==-1){
A[0][i]={i,0};
}
else{
A[0][i]={ida,(ida-i+n)%n};
}
if(idb==-1){
B[0][i]={i,0};
}
else{
B[0][i]={idb,(idb-i+n)%n};
}
}
f(l,1,lg){
f(i,0,n){
A[l][i]={A[l-1][A[l-1][i].F].F,A[l-1][i].S+A[l-1][A[l-1][i].F].S};
B[l][i]={B[l-1][B[l-1][i].F].F,B[l-1][i].S+B[l-1][B[l-1][i].F].S};
}
}
}
bool checkA(int u,int v){
int len=(v-u+n)%n;
f_(l,lg-1,0){
if(A[l][u].S<=len){
len-=A[l][u].S;
u=A[l][u].F;
}
}
if((v-u+n)%n<k && a[u]>=a[v]) return 1;
return 0;
}
bool checkB(int u,int v){
int len=(v-u+n)%n;
f_(l,lg-1,0){
if(B[l][u].S<=len){
len-=B[l][u].S;
u=B[l][u].F;
}
}
if((v-u+n)%n<k && a[u]<=a[v]) return 1;
return 0;
}
void init(int _k,vector<int> _p) {
n=_p.size(),k=_k;
f(i,0,n) p[i]=_p[i];
builda();
AB();
}
int compare_plants(int x, int y) {
if(checkA(x,y) || checkB(y,x)) return 1;
if(checkB(x,y) || checkA(y,x)) return -1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
92 ms |
3384 KB |
Output is correct |
7 |
Correct |
452 ms |
17228 KB |
Output is correct |
8 |
Correct |
1174 ms |
141248 KB |
Output is correct |
9 |
Correct |
1089 ms |
140560 KB |
Output is correct |
10 |
Correct |
1060 ms |
140620 KB |
Output is correct |
11 |
Correct |
1014 ms |
141028 KB |
Output is correct |
12 |
Correct |
922 ms |
140792 KB |
Output is correct |
13 |
Correct |
596 ms |
145424 KB |
Output is correct |
14 |
Correct |
1090 ms |
135876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
77 ms |
6984 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
1400 KB |
Output is correct |
10 |
Correct |
88 ms |
6920 KB |
Output is correct |
11 |
Correct |
116 ms |
6784 KB |
Output is correct |
12 |
Correct |
166 ms |
6980 KB |
Output is correct |
13 |
Correct |
105 ms |
6968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
1364 KB |
Output is correct |
7 |
Correct |
77 ms |
6984 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
6 ms |
1400 KB |
Output is correct |
10 |
Correct |
88 ms |
6920 KB |
Output is correct |
11 |
Correct |
116 ms |
6784 KB |
Output is correct |
12 |
Correct |
166 ms |
6980 KB |
Output is correct |
13 |
Correct |
105 ms |
6968 KB |
Output is correct |
14 |
Correct |
172 ms |
17372 KB |
Output is correct |
15 |
Correct |
808 ms |
141748 KB |
Output is correct |
16 |
Correct |
182 ms |
17292 KB |
Output is correct |
17 |
Correct |
832 ms |
141668 KB |
Output is correct |
18 |
Correct |
788 ms |
140680 KB |
Output is correct |
19 |
Correct |
1163 ms |
140576 KB |
Output is correct |
20 |
Correct |
855 ms |
145332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
125 ms |
4728 KB |
Output is correct |
4 |
Correct |
974 ms |
139008 KB |
Output is correct |
5 |
Correct |
984 ms |
136556 KB |
Output is correct |
6 |
Correct |
856 ms |
135936 KB |
Output is correct |
7 |
Correct |
790 ms |
136328 KB |
Output is correct |
8 |
Correct |
751 ms |
140352 KB |
Output is correct |
9 |
Correct |
798 ms |
138280 KB |
Output is correct |
10 |
Correct |
777 ms |
136660 KB |
Output is correct |
11 |
Correct |
543 ms |
145364 KB |
Output is correct |
12 |
Correct |
1030 ms |
136028 KB |
Output is correct |
13 |
Correct |
712 ms |
142512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
724 KB |
Output is correct |
7 |
Correct |
24 ms |
1384 KB |
Output is correct |
8 |
Correct |
15 ms |
1492 KB |
Output is correct |
9 |
Correct |
22 ms |
1392 KB |
Output is correct |
10 |
Correct |
17 ms |
1492 KB |
Output is correct |
11 |
Correct |
22 ms |
1376 KB |
Output is correct |
12 |
Correct |
20 ms |
1372 KB |
Output is correct |
13 |
Correct |
14 ms |
1496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
1236 KB |
Output is correct |
6 |
Correct |
788 ms |
136012 KB |
Output is correct |
7 |
Correct |
661 ms |
136020 KB |
Output is correct |
8 |
Correct |
540 ms |
136488 KB |
Output is correct |
9 |
Correct |
697 ms |
140172 KB |
Output is correct |
10 |
Correct |
1036 ms |
138260 KB |
Output is correct |
11 |
Correct |
662 ms |
139676 KB |
Output is correct |
12 |
Correct |
905 ms |
139064 KB |
Output is correct |
13 |
Correct |
957 ms |
136692 KB |
Output is correct |
14 |
Correct |
634 ms |
135928 KB |
Output is correct |
15 |
Correct |
648 ms |
136352 KB |
Output is correct |
16 |
Correct |
560 ms |
137460 KB |
Output is correct |
17 |
Correct |
562 ms |
136144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
92 ms |
3384 KB |
Output is correct |
7 |
Correct |
452 ms |
17228 KB |
Output is correct |
8 |
Correct |
1174 ms |
141248 KB |
Output is correct |
9 |
Correct |
1089 ms |
140560 KB |
Output is correct |
10 |
Correct |
1060 ms |
140620 KB |
Output is correct |
11 |
Correct |
1014 ms |
141028 KB |
Output is correct |
12 |
Correct |
922 ms |
140792 KB |
Output is correct |
13 |
Correct |
596 ms |
145424 KB |
Output is correct |
14 |
Correct |
1090 ms |
135876 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
1364 KB |
Output is correct |
21 |
Correct |
77 ms |
6984 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
6 ms |
1400 KB |
Output is correct |
24 |
Correct |
88 ms |
6920 KB |
Output is correct |
25 |
Correct |
116 ms |
6784 KB |
Output is correct |
26 |
Correct |
166 ms |
6980 KB |
Output is correct |
27 |
Correct |
105 ms |
6968 KB |
Output is correct |
28 |
Correct |
172 ms |
17372 KB |
Output is correct |
29 |
Correct |
808 ms |
141748 KB |
Output is correct |
30 |
Correct |
182 ms |
17292 KB |
Output is correct |
31 |
Correct |
832 ms |
141668 KB |
Output is correct |
32 |
Correct |
788 ms |
140680 KB |
Output is correct |
33 |
Correct |
1163 ms |
140576 KB |
Output is correct |
34 |
Correct |
855 ms |
145332 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
125 ms |
4728 KB |
Output is correct |
38 |
Correct |
974 ms |
139008 KB |
Output is correct |
39 |
Correct |
984 ms |
136556 KB |
Output is correct |
40 |
Correct |
856 ms |
135936 KB |
Output is correct |
41 |
Correct |
790 ms |
136328 KB |
Output is correct |
42 |
Correct |
751 ms |
140352 KB |
Output is correct |
43 |
Correct |
798 ms |
138280 KB |
Output is correct |
44 |
Correct |
777 ms |
136660 KB |
Output is correct |
45 |
Correct |
543 ms |
145364 KB |
Output is correct |
46 |
Correct |
1030 ms |
136028 KB |
Output is correct |
47 |
Correct |
712 ms |
142512 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
596 KB |
Output is correct |
53 |
Correct |
3 ms |
724 KB |
Output is correct |
54 |
Correct |
24 ms |
1384 KB |
Output is correct |
55 |
Correct |
15 ms |
1492 KB |
Output is correct |
56 |
Correct |
22 ms |
1392 KB |
Output is correct |
57 |
Correct |
17 ms |
1492 KB |
Output is correct |
58 |
Correct |
22 ms |
1376 KB |
Output is correct |
59 |
Correct |
20 ms |
1372 KB |
Output is correct |
60 |
Correct |
14 ms |
1496 KB |
Output is correct |
61 |
Correct |
156 ms |
4704 KB |
Output is correct |
62 |
Correct |
522 ms |
16964 KB |
Output is correct |
63 |
Correct |
1573 ms |
137396 KB |
Output is correct |
64 |
Correct |
945 ms |
136120 KB |
Output is correct |
65 |
Correct |
889 ms |
135968 KB |
Output is correct |
66 |
Correct |
794 ms |
136348 KB |
Output is correct |
67 |
Correct |
777 ms |
140136 KB |
Output is correct |
68 |
Correct |
1250 ms |
138264 KB |
Output is correct |
69 |
Correct |
858 ms |
139768 KB |
Output is correct |
70 |
Correct |
1154 ms |
139004 KB |
Output is correct |
71 |
Correct |
1071 ms |
136560 KB |
Output is correct |
72 |
Correct |
888 ms |
135964 KB |
Output is correct |
73 |
Correct |
752 ms |
136476 KB |
Output is correct |
74 |
Correct |
1420 ms |
136920 KB |
Output is correct |
75 |
Correct |
723 ms |
136140 KB |
Output is correct |