#include "plants.h"
#include "bits/stdc++.h"
#define MAXN 200009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int h[MAXN],n,k,arr[MAXN];
vector<int>tmp;
struct tree{
PII s[MAXN<<2];int lazy[MAXN<<2];
void build(int nd=1,int x=0,int y=n-1){
lazy[nd]=0;
if(x==y){
s[nd]=mp(arr[x],x);
return;
}
int mid=(x+y)>>1;
build(nd<<1,x,mid);
build(nd<<1|1,mid+1,y);
s[nd]=min(s[nd<<1],s[nd<<1|1]);
}
void shift(int nd){
int &ret=lazy[nd];if(!ret)return;
lazy[nd<<1]+=ret;s[nd<<1].ff+=ret;
lazy[nd<<1|1]+=ret;s[nd<<1|1].ff+=ret;
ret=0;
}
void inc(int l,int r,int val,int nd=1,int x=0,int y=n-1){
if(l>y or x>r)
return;
if(l<=x and y<=r){
s[nd].ff+=val;
lazy[nd]+=val;
return;
}
shift(nd);
int mid=(x+y)>>1;
inc(l,r,val,nd<<1,x,mid);
inc(l,r,val,nd<<1|1,mid+1,y);
s[nd]=min(s[nd<<1],s[nd<<1|1]);
}
void print(int nd=1,int x=0,int y=n-1){
if(x==y){
printf("%d ",s[nd].ff);
return;
}shift(nd);
int mid=(x+y)>>1;
print(nd<<1,x,mid);
print(nd<<1|1,mid+1,y);
s[nd]=min(s[nd<<1],s[nd<<1|1]);
}
void tap(int l,int r,int nd=1,int x=0,int y=n-1){
if(l>y or x>r or s[nd].ff>0)return;
if(x==y){
tmp.pb(x);
return;
}shift(nd);
int mid=(x+y)>>1;
tap(l,r,nd<<1,x,mid);
tap(l,r,nd<<1|1,mid+1,y);
s[nd]=min(s[nd<<1],s[nd<<1|1]);
}
}ds[2];
void upd(int x,int y,int val,int bro=1){
if(x<=y){
ds[0].inc(x,y,val);
if(bro)ds[1].inc(x,y,val);
}
else{
ds[0].inc(x,n-1,val),ds[0].inc(0,y,val);
if(bro)ds[1].inc(x,n-1,val),ds[1].inc(0,y,val);
}
}
int get(){
int a=ds[0].s[1].ff;
int b=ds[0].s[1].ss;
assert(a==0);return b;
}
int vis[MAXN];
void zero(int i){
if(vis[i])return;vis[i]=1;
ds[1].inc(i,i,INF);
upd(i+1,(i+k-1)%n,1,0);
}
void flex(int x,int y){
if(x<=y)ds[1].tap(x,y);
else ds[1].tap(x,n-1),ds[1].tap(0,y);
}
int d(int x,int y){
if(x>y)swap(x,y);
return min(y-x,n-y+x);
}
set<PII>st;
const int LOG=19;
int l[MAXN],r[MAXN],pre[MAXN],suf[MAXN];
int A[MAXN][LOG],B[MAXN][LOG];
void init(int K, vector<int> rz) {
n=int(rz.size());k=K;
for(int i=0;i<n;i++)arr[i]=rz[i];
ds[0].build();ds[1].build();
for(int i=0;i<n;i++)
if(!arr[i])
zero(i);
for(int i=n-1;i>=0;i--){
int res=get();assert(res>=0 and res<n);h[res]=i;
int pre=(res-K+1+n)%n;
int nex=(res+K-1)%n;
upd(res,res,INF);
upd(pre,res,-1);upd(res,nex,-1,0);
flex(pre,res);flex(res,nex);
tr(it,tmp)zero(*it);tmp.clear();
}
int p=n-k+1;
for(int i=p;i<n;i++)
st.insert(mp(h[i],i));
for(int i=0;i<n;i++){
__typeof((st).begin())it=st.lower_bound(mp(h[i],-1));
if(it!=st.begin()){
it--;l[i]=it->ss;
}
else
l[i]=-1;
st.erase(mp(h[p],p));p=(p+1)%n;
st.insert(mp(h[i],i));
}st.clear();p=k-2;
for(int i=0;i<=p;i++)st.insert(mp(h[i],i));
for(int i=n-1;i>=0;i--){
__typeof((st).begin())it=st.lower_bound(mp(h[i],-1));
if(it!=st.begin()){
it--;r[i]=it->ss;
}
else
r[i]=-1;
st.erase(mp(h[p],p));p=(p-1+n)%n;
st.insert(mp(h[i],i));
}
for(int i=0;i<n;i++){
if(l[i]==-1)pre[i]=i;
else if(l[i]<i)pre[i]=pre[l[i]];
else pre[i]=l[i];
}
for(int i=n-1;i>=0;i--){
if(r[i]==-1)suf[i]=i;
else if(i<r[i])suf[i]=suf[r[i]];
else suf[i]=r[i];
}
memset(A,-1,sizeof A);
memset(B,-1,sizeof B);
for(int i=0;i<n;i++){
if(i>l[i])A[i][0]=l[i];
if(i<r[i])B[i][0]=r[i];
}
for(int j=1;j<LOG;j++)
for(int i=0;i<n;i++){
if(~A[i][j-1] and A[i][j-1]>l[A[i][j-1]])
A[i][j]=A[A[i][j-1]][j-1];
if(~B[i][j-1] and B[i][j-1]<r[B[i][j-1]])
B[i][j]=B[B[i][j-1]][j-1];
}
}
int cep(int x,int y){
for(int j=LOG-1;j>=0;j--)
if(A[x][j]>y)
x=A[x][j];
//while(x>l[x] and l[x]>y)x=l[x];
return x;
}
int sag(int x,int y){
for(int j=LOG-1;j>=0;j--)
if(~B[x][j] and B[x][j]<y)
x=B[x][j];
//while(r[x]<y and x<r[x])x=r[x];
return x;
}
int compare_plants(int x, int y) {
if(d(x,y)<k){
if(h[x]>h[y])return 1;
else return -1;
}
else{int a;
a=pre[x];if(a==y)return 1;a=cep(a,y);
if(d(a,y)<k and h[a]>h[y])return 1;
a=sag(x,y);if(d(a,y)<k and h[a]>h[y])return 1;
a=cep(y,x);if(d(a,x)<k and h[a]>h[x])return -1;
a=suf[y];if(a==x)return -1;a=sag(a,x);
if(d(a,x)<k and h[a]>h[x])return -1;
}
return 0;
}
Compilation message
plants.cpp: In function 'void zero(int)':
plants.cpp:95:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
95 | if(vis[i])return;vis[i]=1;
| ^~
plants.cpp:95:19: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
95 | if(vis[i])return;vis[i]=1;
| ^~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:10:18: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
10 | #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
| ^~~
plants.cpp:125:3: note: in expansion of macro 'tr'
125 | tr(it,tmp)zero(*it);tmp.clear();
| ^~
plants.cpp:125:23: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
125 | tr(it,tmp)zero(*it);tmp.clear();
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
42604 KB |
Output is correct |
2 |
Correct |
23 ms |
42604 KB |
Output is correct |
3 |
Correct |
24 ms |
42604 KB |
Output is correct |
4 |
Correct |
24 ms |
42604 KB |
Output is correct |
5 |
Correct |
24 ms |
42604 KB |
Output is correct |
6 |
Correct |
123 ms |
45420 KB |
Output is correct |
7 |
Correct |
210 ms |
46828 KB |
Output is correct |
8 |
Correct |
824 ms |
56196 KB |
Output is correct |
9 |
Correct |
831 ms |
56428 KB |
Output is correct |
10 |
Correct |
848 ms |
56392 KB |
Output is correct |
11 |
Correct |
866 ms |
56192 KB |
Output is correct |
12 |
Correct |
868 ms |
56428 KB |
Output is correct |
13 |
Correct |
821 ms |
56264 KB |
Output is correct |
14 |
Correct |
935 ms |
56300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
42604 KB |
Output is correct |
2 |
Correct |
24 ms |
42604 KB |
Output is correct |
3 |
Correct |
24 ms |
42604 KB |
Output is correct |
4 |
Correct |
24 ms |
42604 KB |
Output is correct |
5 |
Correct |
24 ms |
42732 KB |
Output is correct |
6 |
Correct |
29 ms |
42860 KB |
Output is correct |
7 |
Correct |
113 ms |
46060 KB |
Output is correct |
8 |
Correct |
25 ms |
42732 KB |
Output is correct |
9 |
Correct |
29 ms |
42860 KB |
Output is correct |
10 |
Correct |
112 ms |
46060 KB |
Output is correct |
11 |
Correct |
101 ms |
45804 KB |
Output is correct |
12 |
Correct |
103 ms |
46060 KB |
Output is correct |
13 |
Correct |
112 ms |
46060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
42604 KB |
Output is correct |
2 |
Correct |
24 ms |
42604 KB |
Output is correct |
3 |
Correct |
24 ms |
42604 KB |
Output is correct |
4 |
Correct |
24 ms |
42604 KB |
Output is correct |
5 |
Correct |
24 ms |
42732 KB |
Output is correct |
6 |
Correct |
29 ms |
42860 KB |
Output is correct |
7 |
Correct |
113 ms |
46060 KB |
Output is correct |
8 |
Correct |
25 ms |
42732 KB |
Output is correct |
9 |
Correct |
29 ms |
42860 KB |
Output is correct |
10 |
Correct |
112 ms |
46060 KB |
Output is correct |
11 |
Correct |
101 ms |
45804 KB |
Output is correct |
12 |
Correct |
103 ms |
46060 KB |
Output is correct |
13 |
Correct |
112 ms |
46060 KB |
Output is correct |
14 |
Correct |
204 ms |
47212 KB |
Output is correct |
15 |
Correct |
2118 ms |
61932 KB |
Output is correct |
16 |
Correct |
204 ms |
47212 KB |
Output is correct |
17 |
Correct |
2162 ms |
62096 KB |
Output is correct |
18 |
Correct |
1075 ms |
60788 KB |
Output is correct |
19 |
Correct |
1128 ms |
60904 KB |
Output is correct |
20 |
Correct |
2041 ms |
65540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
42604 KB |
Output is correct |
2 |
Correct |
24 ms |
42604 KB |
Output is correct |
3 |
Correct |
141 ms |
45548 KB |
Output is correct |
4 |
Correct |
1031 ms |
56232 KB |
Output is correct |
5 |
Correct |
1221 ms |
56300 KB |
Output is correct |
6 |
Correct |
1522 ms |
56428 KB |
Output is correct |
7 |
Correct |
1736 ms |
60268 KB |
Output is correct |
8 |
Correct |
1988 ms |
64364 KB |
Output is correct |
9 |
Correct |
1102 ms |
59244 KB |
Output is correct |
10 |
Correct |
1160 ms |
59244 KB |
Output is correct |
11 |
Correct |
814 ms |
58988 KB |
Output is correct |
12 |
Correct |
1074 ms |
59336 KB |
Output is correct |
13 |
Correct |
1113 ms |
62444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
42604 KB |
Output is correct |
2 |
Correct |
24 ms |
42604 KB |
Output is correct |
3 |
Correct |
24 ms |
42604 KB |
Output is correct |
4 |
Correct |
23 ms |
42732 KB |
Output is correct |
5 |
Correct |
24 ms |
42604 KB |
Output is correct |
6 |
Correct |
27 ms |
42732 KB |
Output is correct |
7 |
Correct |
51 ms |
43244 KB |
Output is correct |
8 |
Correct |
42 ms |
43244 KB |
Output is correct |
9 |
Correct |
49 ms |
43244 KB |
Output is correct |
10 |
Correct |
42 ms |
43244 KB |
Output is correct |
11 |
Correct |
51 ms |
43244 KB |
Output is correct |
12 |
Correct |
51 ms |
43244 KB |
Output is correct |
13 |
Correct |
38 ms |
43372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
42732 KB |
Output is correct |
2 |
Correct |
24 ms |
42604 KB |
Output is correct |
3 |
Correct |
23 ms |
42604 KB |
Output is correct |
4 |
Correct |
25 ms |
42604 KB |
Output is correct |
5 |
Correct |
27 ms |
42732 KB |
Output is correct |
6 |
Correct |
1126 ms |
56172 KB |
Output is correct |
7 |
Correct |
1421 ms |
56172 KB |
Output is correct |
8 |
Correct |
1626 ms |
56684 KB |
Output is correct |
9 |
Correct |
1914 ms |
60448 KB |
Output is correct |
10 |
Correct |
997 ms |
56300 KB |
Output is correct |
11 |
Correct |
1332 ms |
60044 KB |
Output is correct |
12 |
Correct |
935 ms |
56172 KB |
Output is correct |
13 |
Correct |
1140 ms |
58408 KB |
Output is correct |
14 |
Correct |
1406 ms |
58732 KB |
Output is correct |
15 |
Correct |
1699 ms |
59244 KB |
Output is correct |
16 |
Correct |
963 ms |
58220 KB |
Output is correct |
17 |
Correct |
1082 ms |
58408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
42604 KB |
Output is correct |
2 |
Correct |
23 ms |
42604 KB |
Output is correct |
3 |
Correct |
24 ms |
42604 KB |
Output is correct |
4 |
Correct |
24 ms |
42604 KB |
Output is correct |
5 |
Correct |
24 ms |
42604 KB |
Output is correct |
6 |
Correct |
123 ms |
45420 KB |
Output is correct |
7 |
Correct |
210 ms |
46828 KB |
Output is correct |
8 |
Correct |
824 ms |
56196 KB |
Output is correct |
9 |
Correct |
831 ms |
56428 KB |
Output is correct |
10 |
Correct |
848 ms |
56392 KB |
Output is correct |
11 |
Correct |
866 ms |
56192 KB |
Output is correct |
12 |
Correct |
868 ms |
56428 KB |
Output is correct |
13 |
Correct |
821 ms |
56264 KB |
Output is correct |
14 |
Correct |
935 ms |
56300 KB |
Output is correct |
15 |
Correct |
24 ms |
42604 KB |
Output is correct |
16 |
Correct |
24 ms |
42604 KB |
Output is correct |
17 |
Correct |
24 ms |
42604 KB |
Output is correct |
18 |
Correct |
24 ms |
42604 KB |
Output is correct |
19 |
Correct |
24 ms |
42732 KB |
Output is correct |
20 |
Correct |
29 ms |
42860 KB |
Output is correct |
21 |
Correct |
113 ms |
46060 KB |
Output is correct |
22 |
Correct |
25 ms |
42732 KB |
Output is correct |
23 |
Correct |
29 ms |
42860 KB |
Output is correct |
24 |
Correct |
112 ms |
46060 KB |
Output is correct |
25 |
Correct |
101 ms |
45804 KB |
Output is correct |
26 |
Correct |
103 ms |
46060 KB |
Output is correct |
27 |
Correct |
112 ms |
46060 KB |
Output is correct |
28 |
Correct |
204 ms |
47212 KB |
Output is correct |
29 |
Correct |
2118 ms |
61932 KB |
Output is correct |
30 |
Correct |
204 ms |
47212 KB |
Output is correct |
31 |
Correct |
2162 ms |
62096 KB |
Output is correct |
32 |
Correct |
1075 ms |
60788 KB |
Output is correct |
33 |
Correct |
1128 ms |
60904 KB |
Output is correct |
34 |
Correct |
2041 ms |
65540 KB |
Output is correct |
35 |
Correct |
24 ms |
42604 KB |
Output is correct |
36 |
Correct |
24 ms |
42604 KB |
Output is correct |
37 |
Correct |
141 ms |
45548 KB |
Output is correct |
38 |
Correct |
1031 ms |
56232 KB |
Output is correct |
39 |
Correct |
1221 ms |
56300 KB |
Output is correct |
40 |
Correct |
1522 ms |
56428 KB |
Output is correct |
41 |
Correct |
1736 ms |
60268 KB |
Output is correct |
42 |
Correct |
1988 ms |
64364 KB |
Output is correct |
43 |
Correct |
1102 ms |
59244 KB |
Output is correct |
44 |
Correct |
1160 ms |
59244 KB |
Output is correct |
45 |
Correct |
814 ms |
58988 KB |
Output is correct |
46 |
Correct |
1074 ms |
59336 KB |
Output is correct |
47 |
Correct |
1113 ms |
62444 KB |
Output is correct |
48 |
Correct |
24 ms |
42604 KB |
Output is correct |
49 |
Correct |
24 ms |
42604 KB |
Output is correct |
50 |
Correct |
24 ms |
42604 KB |
Output is correct |
51 |
Correct |
23 ms |
42732 KB |
Output is correct |
52 |
Correct |
24 ms |
42604 KB |
Output is correct |
53 |
Correct |
27 ms |
42732 KB |
Output is correct |
54 |
Correct |
51 ms |
43244 KB |
Output is correct |
55 |
Correct |
42 ms |
43244 KB |
Output is correct |
56 |
Correct |
49 ms |
43244 KB |
Output is correct |
57 |
Correct |
42 ms |
43244 KB |
Output is correct |
58 |
Correct |
51 ms |
43244 KB |
Output is correct |
59 |
Correct |
51 ms |
43244 KB |
Output is correct |
60 |
Correct |
38 ms |
43372 KB |
Output is correct |
61 |
Correct |
155 ms |
47280 KB |
Output is correct |
62 |
Correct |
226 ms |
48876 KB |
Output is correct |
63 |
Correct |
1006 ms |
59116 KB |
Output is correct |
64 |
Correct |
1210 ms |
59372 KB |
Output is correct |
65 |
Correct |
1541 ms |
59500 KB |
Output is correct |
66 |
Correct |
1780 ms |
60140 KB |
Output is correct |
67 |
Correct |
1983 ms |
64364 KB |
Output is correct |
68 |
Correct |
1110 ms |
59572 KB |
Output is correct |
69 |
Correct |
1368 ms |
63596 KB |
Output is correct |
70 |
Correct |
1030 ms |
59244 KB |
Output is correct |
71 |
Correct |
1271 ms |
59372 KB |
Output is correct |
72 |
Correct |
1545 ms |
59488 KB |
Output is correct |
73 |
Correct |
1743 ms |
60140 KB |
Output is correct |
74 |
Correct |
1039 ms |
59244 KB |
Output is correct |
75 |
Correct |
1155 ms |
59580 KB |
Output is correct |