#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];
ll seg[N<<2],lz[N<<2];
vector<int> vec;
set<int> s,ok;
pair<ll,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){
//cout<<L<<" "<<R<<" "<<id<<" "<<l<<" "<<r<<endl;
//eror(seg[id]);
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);
//dbgv(vec);
}
else{
prt(l,r);
add(l,r,-1);
}
dels(x);
for(auto g : vec) add(g,g+1,n+10);
}
/*int last=0;
f_(T,n-1,0){
f(i,0,2*n){
if(p[i%n]==0){
if(i>=n && i-last>=k){
a[i-n]=T;
f(j,0,k){
p[(i-j)%n]--;
}
break ;
}
last=i;
}
}
}*/
f(i,0,n) pos[a[i]]=i;
//dbga(a,0,n);
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
80 ms |
3468 KB |
Output is correct |
7 |
Correct |
428 ms |
17716 KB |
Output is correct |
8 |
Correct |
1154 ms |
145344 KB |
Output is correct |
9 |
Correct |
1025 ms |
144644 KB |
Output is correct |
10 |
Correct |
971 ms |
144620 KB |
Output is correct |
11 |
Correct |
885 ms |
145100 KB |
Output is correct |
12 |
Correct |
859 ms |
144936 KB |
Output is correct |
13 |
Correct |
558 ms |
149380 KB |
Output is correct |
14 |
Correct |
963 ms |
140064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
1420 KB |
Output is correct |
7 |
Correct |
96 ms |
7100 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
4 ms |
1364 KB |
Output is correct |
10 |
Correct |
104 ms |
7072 KB |
Output is correct |
11 |
Correct |
89 ms |
6976 KB |
Output is correct |
12 |
Correct |
187 ms |
7108 KB |
Output is correct |
13 |
Correct |
84 ms |
7092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
4 ms |
1420 KB |
Output is correct |
7 |
Correct |
96 ms |
7100 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
4 ms |
1364 KB |
Output is correct |
10 |
Correct |
104 ms |
7072 KB |
Output is correct |
11 |
Correct |
89 ms |
6976 KB |
Output is correct |
12 |
Correct |
187 ms |
7108 KB |
Output is correct |
13 |
Correct |
84 ms |
7092 KB |
Output is correct |
14 |
Correct |
216 ms |
17804 KB |
Output is correct |
15 |
Correct |
867 ms |
145804 KB |
Output is correct |
16 |
Correct |
214 ms |
20136 KB |
Output is correct |
17 |
Correct |
1077 ms |
149572 KB |
Output is correct |
18 |
Correct |
837 ms |
148076 KB |
Output is correct |
19 |
Correct |
1082 ms |
148560 KB |
Output is correct |
20 |
Correct |
995 ms |
153196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
0 ms |
596 KB |
Output is correct |
3 |
Correct |
120 ms |
4764 KB |
Output is correct |
4 |
Correct |
1132 ms |
143304 KB |
Output is correct |
5 |
Correct |
1122 ms |
140684 KB |
Output is correct |
6 |
Correct |
878 ms |
140040 KB |
Output is correct |
7 |
Correct |
865 ms |
140520 KB |
Output is correct |
8 |
Correct |
796 ms |
144320 KB |
Output is correct |
9 |
Correct |
799 ms |
145332 KB |
Output is correct |
10 |
Correct |
769 ms |
143948 KB |
Output is correct |
11 |
Correct |
542 ms |
152300 KB |
Output is correct |
12 |
Correct |
1010 ms |
143148 KB |
Output is correct |
13 |
Correct |
725 ms |
149760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1392 KB |
Output is correct |
8 |
Correct |
16 ms |
1492 KB |
Output is correct |
9 |
Correct |
22 ms |
1444 KB |
Output is correct |
10 |
Correct |
17 ms |
1492 KB |
Output is correct |
11 |
Correct |
24 ms |
1392 KB |
Output is correct |
12 |
Correct |
26 ms |
1492 KB |
Output is correct |
13 |
Correct |
15 ms |
1464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
771 ms |
140224 KB |
Output is correct |
7 |
Correct |
686 ms |
140132 KB |
Output is correct |
8 |
Correct |
566 ms |
140528 KB |
Output is correct |
9 |
Correct |
748 ms |
144212 KB |
Output is correct |
10 |
Correct |
1032 ms |
144644 KB |
Output is correct |
11 |
Correct |
696 ms |
146532 KB |
Output is correct |
12 |
Correct |
878 ms |
145172 KB |
Output is correct |
13 |
Correct |
965 ms |
142956 KB |
Output is correct |
14 |
Correct |
614 ms |
142456 KB |
Output is correct |
15 |
Correct |
704 ms |
143100 KB |
Output is correct |
16 |
Correct |
550 ms |
143752 KB |
Output is correct |
17 |
Correct |
553 ms |
142464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
80 ms |
3468 KB |
Output is correct |
7 |
Correct |
428 ms |
17716 KB |
Output is correct |
8 |
Correct |
1154 ms |
145344 KB |
Output is correct |
9 |
Correct |
1025 ms |
144644 KB |
Output is correct |
10 |
Correct |
971 ms |
144620 KB |
Output is correct |
11 |
Correct |
885 ms |
145100 KB |
Output is correct |
12 |
Correct |
859 ms |
144936 KB |
Output is correct |
13 |
Correct |
558 ms |
149380 KB |
Output is correct |
14 |
Correct |
963 ms |
140064 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 |
0 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
1420 KB |
Output is correct |
21 |
Correct |
96 ms |
7100 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
104 ms |
7072 KB |
Output is correct |
25 |
Correct |
89 ms |
6976 KB |
Output is correct |
26 |
Correct |
187 ms |
7108 KB |
Output is correct |
27 |
Correct |
84 ms |
7092 KB |
Output is correct |
28 |
Correct |
216 ms |
17804 KB |
Output is correct |
29 |
Correct |
867 ms |
145804 KB |
Output is correct |
30 |
Correct |
214 ms |
20136 KB |
Output is correct |
31 |
Correct |
1077 ms |
149572 KB |
Output is correct |
32 |
Correct |
837 ms |
148076 KB |
Output is correct |
33 |
Correct |
1082 ms |
148560 KB |
Output is correct |
34 |
Correct |
995 ms |
153196 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
0 ms |
596 KB |
Output is correct |
37 |
Correct |
120 ms |
4764 KB |
Output is correct |
38 |
Correct |
1132 ms |
143304 KB |
Output is correct |
39 |
Correct |
1122 ms |
140684 KB |
Output is correct |
40 |
Correct |
878 ms |
140040 KB |
Output is correct |
41 |
Correct |
865 ms |
140520 KB |
Output is correct |
42 |
Correct |
796 ms |
144320 KB |
Output is correct |
43 |
Correct |
799 ms |
145332 KB |
Output is correct |
44 |
Correct |
769 ms |
143948 KB |
Output is correct |
45 |
Correct |
542 ms |
152300 KB |
Output is correct |
46 |
Correct |
1010 ms |
143148 KB |
Output is correct |
47 |
Correct |
725 ms |
149760 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 |
1392 KB |
Output is correct |
55 |
Correct |
16 ms |
1492 KB |
Output is correct |
56 |
Correct |
22 ms |
1444 KB |
Output is correct |
57 |
Correct |
17 ms |
1492 KB |
Output is correct |
58 |
Correct |
24 ms |
1392 KB |
Output is correct |
59 |
Correct |
26 ms |
1492 KB |
Output is correct |
60 |
Correct |
15 ms |
1464 KB |
Output is correct |
61 |
Correct |
144 ms |
6488 KB |
Output is correct |
62 |
Correct |
466 ms |
19640 KB |
Output is correct |
63 |
Correct |
1561 ms |
144336 KB |
Output is correct |
64 |
Correct |
917 ms |
143264 KB |
Output is correct |
65 |
Correct |
840 ms |
143324 KB |
Output is correct |
66 |
Correct |
794 ms |
143916 KB |
Output is correct |
67 |
Correct |
746 ms |
147908 KB |
Output is correct |
68 |
Correct |
1270 ms |
145420 KB |
Output is correct |
69 |
Correct |
859 ms |
147508 KB |
Output is correct |
70 |
Correct |
1186 ms |
146008 KB |
Output is correct |
71 |
Correct |
1441 ms |
143848 KB |
Output is correct |
72 |
Correct |
948 ms |
143444 KB |
Output is correct |
73 |
Correct |
912 ms |
143912 KB |
Output is correct |
74 |
Correct |
1493 ms |
144044 KB |
Output is correct |
75 |
Correct |
958 ms |
143324 KB |
Output is correct |