#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
int n, pos[200100],CA;
vector<int> R;
set<int> CD;
set<pair<int,int>> gaps;
int bjl[200100][20],bjr[200100][20], gooverL[200100], gooverR[200100];
struct segtree{
pair<int,int> T[1<<19];
int lz[1<<20];
inline void pd(int i){
if(lz[i]){
T[i].first+=lz[i];
lz[i*2]+=lz[i];
lz[i*2+1]+=lz[i];
lz[i]=0;
}
}
void init(int i,int l,int r){
if(l==r)return void(T[i]={R[l],l});
init(i*2,l,l+r>>1);
init(i*2+1,l+r+2>>1,r);
T[i]=min(T[i*2],T[i*2+1]);
}
void upd(int i,int l,int r,int ll,int rr){
pd(i);if(ll<=l&&r<=rr)
return lz[i]=-1,pd(i);
if(l>rr||ll>r)return;
upd(i*2,l,l+r>>1,ll,rr);
upd(i*2+1,l+r+2>>1,r,ll,rr);
T[i]=min(T[i*2],T[i*2+1]);
}
void upd2(int i,int l,int r,int ll,int rr){
pd(i);if(ll<=l&&r<=rr)
return lz[i]=1e9,pd(i);
if(l>rr||ll>r)return;
upd2(i*2,l,l+r>>1,ll,rr);
upd2(i*2+1,l+r+2>>1,r,ll,rr);
T[i]=min(T[i*2],T[i*2+1]);
}
}ST;
inline int AGL(int k){
return (k+n)%n;
}
inline void add(int i){
if(!CD.size()){
gaps.insert({n,i});
CD.insert(i);
return;
}
if(CD.size()==1){
CD.insert(i);
gaps.clear();
int a=*CD.begin();
int b=*--CD.end();
gaps.insert({b-a,b});
gaps.insert({n+a-b,a});
return;
}
auto it1=CD.upper_bound(i);
auto it2=it1;
if(it2==CD.begin())
it2=--CD.end();
else --it2;
if(it1==CD.end())
it1=CD.begin();
gaps.insert({AGL(i-*it2),i});
gaps.insert({AGL(*it1-i),*it1});
gaps.erase({AGL(*it1-*it2),*it1});
CD.insert(i);
}
inline void del(int i){
if(CD.size()==1)
return CD.clear(),gaps.clear();
else if(CD.size()==2){
CD.erase(i);
gaps.clear();
gaps.insert({n,*CD.begin()});
return;
}
auto it1=CD.upper_bound(i);
auto it2=CD.find(i);
if(it2==CD.begin())
it2=--CD.end();
else --it2;
if(it1==CD.end())
it1=CD.begin();
gaps.erase({AGL(i-*it2),i});
gaps.erase({AGL(*it1-i),*it1});
gaps.insert({AGL(*it1-*it2),*it1});
CD.erase(i);
}
inline int possiblemin(){
auto[a,b]=ST.T[1];
if(!a) {
ST.upd2(1,0,n-1,b,b);
add(b);
return 1;
}
return 0;
}
void init(int k, std::vector<int> r) {
R=r;
k--;
n=r.size();
ST.init(1,0,n-1);
while(possiblemin());
while(CD.size()){
int bst=(--gaps.end())->second;
pos[bst]=++CA;
if(bst<k)ST.upd(1,0,n-1,0,bst),
ST.upd(1,0,n-1,bst-k+n,n-1);
else ST.upd(1,0,n-1,bst-k,bst);
while(possiblemin());
del(bst);
}
set<pair<int,int>> st;
st.insert({1e9,n});
for(int i=n-k;i<n;i++)
st.insert({pos[i],i});
for(int i=0;i<n;i++)bjl[i][0]=st.upper_bound({pos[i],0})->second,
st.erase({pos[(i+n-k)%n],(i+n-k)%n}),st.insert({pos[i],i});
st.clear();
for(int i=0;i<k;i++)
st.insert({pos[i],i});
st.insert({1e9,n});
bjl[n][0]=bjr[n][0]=n;
for(int i=n;i--;)bjr[i][0]=st.upper_bound({pos[i],0})->second,
st.erase({pos[(i+k)%n],(i+k)%n}),st.insert({pos[i],i});
for(int i=0;i<n;i++)
if(bjl[i][0]>i&&bjl[i][0]-n)
gooverL[i]=bjl[i][0],bjl[i][0]=i;
for(int i=0;i<n;i++)
if(bjr[i][0]<i&&bjr[i][0]-n)
gooverR[i]=bjr[i][0],bjr[i][0]=i;
for(int j=1;j<20;j++) for(int i=0;i<=n;i++)
bjl[i][j]=bjl[bjl[i][j-1]][j-1],
bjr[i][j]=bjr[bjr[i][j-1]][j-1];
pos[n]=1e9;
gooverL[n]=gooverR[n]=n;
}
int biggerl(int x,int y){
int prevx=x;
if(x<y)x=gooverL[bjl[x][19]];
if(x<=y) return pos[bjl[prevx][19]]<=pos[y];
for(int i=20;i--;)
if(bjl[x][i]>y&&bjl[x][i]-n)
x=bjl[x][i];
if(bjl[x][0]==n)
return 0;
return pos[x]<=pos[y];
}
int biggerr(int x,int y){
int prevx=x;
if(x>y)x=gooverR[bjr[x][19]];
if(x>=y) return pos[bjr[prevx][19]]<=pos[y];
for(int i=20;i--;)
if(bjr[x][i]<y&&bjr[x][i]-n)
x=bjr[x][i];
if(bjr[x][0]==n)
return 0;
return pos[x]<=pos[y];
}
inline int bigger(int x,int y){
return biggerl(x,y)||biggerr(x,y);
}
int compare_plants(int x, int y) {
if(pos[x]<pos[y])
return bigger(x,y)?1:0;
return bigger(y,x)?-1:0;
}
Compilation message
plants.cpp: In member function 'void segtree::init(int, int, int)':
plants.cpp:22:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | init(i*2,l,l+r>>1);
| ~^~
plants.cpp:23:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | init(i*2+1,l+r+2>>1,r);
| ~~~^~
plants.cpp: In member function 'void segtree::upd(int, int, int, int, int)':
plants.cpp:30:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | upd(i*2,l,l+r>>1,ll,rr);
| ~^~
plants.cpp:31:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | upd(i*2+1,l+r+2>>1,r,ll,rr);
| ~~~^~
plants.cpp: In member function 'void segtree::upd2(int, int, int, int, int)':
plants.cpp:38:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | upd2(i*2,l,l+r>>1,ll,rr);
| ~^~
plants.cpp:39:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | upd2(i*2+1,l+r+2>>1,r,ll,rr);
| ~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
12888 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
1 ms |
12892 KB |
Output is correct |
5 |
Correct |
1 ms |
12892 KB |
Output is correct |
6 |
Correct |
38 ms |
15520 KB |
Output is correct |
7 |
Correct |
76 ms |
22868 KB |
Output is correct |
8 |
Correct |
289 ms |
56000 KB |
Output is correct |
9 |
Correct |
259 ms |
55680 KB |
Output is correct |
10 |
Correct |
277 ms |
55664 KB |
Output is correct |
11 |
Correct |
297 ms |
56916 KB |
Output is correct |
12 |
Correct |
291 ms |
56492 KB |
Output is correct |
13 |
Correct |
378 ms |
65256 KB |
Output is correct |
14 |
Correct |
164 ms |
46504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
1 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
1 ms |
12892 KB |
Output is correct |
5 |
Correct |
1 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
43 ms |
15824 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
40 ms |
15836 KB |
Output is correct |
11 |
Correct |
50 ms |
15700 KB |
Output is correct |
12 |
Correct |
47 ms |
15956 KB |
Output is correct |
13 |
Correct |
42 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
1 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
1 ms |
12892 KB |
Output is correct |
5 |
Correct |
1 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
43 ms |
15824 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
40 ms |
15836 KB |
Output is correct |
11 |
Correct |
50 ms |
15700 KB |
Output is correct |
12 |
Correct |
47 ms |
15956 KB |
Output is correct |
13 |
Correct |
42 ms |
15956 KB |
Output is correct |
14 |
Correct |
69 ms |
22372 KB |
Output is correct |
15 |
Correct |
550 ms |
52820 KB |
Output is correct |
16 |
Correct |
68 ms |
22612 KB |
Output is correct |
17 |
Correct |
522 ms |
52560 KB |
Output is correct |
18 |
Correct |
459 ms |
56232 KB |
Output is correct |
19 |
Correct |
309 ms |
51584 KB |
Output is correct |
20 |
Correct |
521 ms |
56152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12888 KB |
Output is correct |
2 |
Correct |
1 ms |
12892 KB |
Output is correct |
3 |
Correct |
47 ms |
15604 KB |
Output is correct |
4 |
Correct |
263 ms |
53156 KB |
Output is correct |
5 |
Correct |
331 ms |
48408 KB |
Output is correct |
6 |
Correct |
364 ms |
46928 KB |
Output is correct |
7 |
Correct |
402 ms |
47480 KB |
Output is correct |
8 |
Correct |
550 ms |
51028 KB |
Output is correct |
9 |
Correct |
326 ms |
51608 KB |
Output is correct |
10 |
Correct |
315 ms |
48464 KB |
Output is correct |
11 |
Correct |
369 ms |
65616 KB |
Output is correct |
12 |
Correct |
184 ms |
47016 KB |
Output is correct |
13 |
Correct |
444 ms |
59964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
1 ms |
12892 KB |
Output is correct |
5 |
Correct |
1 ms |
12892 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
10 ms |
13540 KB |
Output is correct |
8 |
Correct |
8 ms |
13404 KB |
Output is correct |
9 |
Correct |
11 ms |
13516 KB |
Output is correct |
10 |
Correct |
8 ms |
13456 KB |
Output is correct |
11 |
Correct |
12 ms |
13404 KB |
Output is correct |
12 |
Correct |
10 ms |
13404 KB |
Output is correct |
13 |
Correct |
8 ms |
13432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
13144 KB |
Output is correct |
2 |
Correct |
1 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
1 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12892 KB |
Output is correct |
6 |
Correct |
325 ms |
47204 KB |
Output is correct |
7 |
Correct |
365 ms |
47176 KB |
Output is correct |
8 |
Correct |
405 ms |
47440 KB |
Output is correct |
9 |
Correct |
512 ms |
51280 KB |
Output is correct |
10 |
Correct |
290 ms |
51516 KB |
Output is correct |
11 |
Correct |
400 ms |
50768 KB |
Output is correct |
12 |
Correct |
296 ms |
53176 KB |
Output is correct |
13 |
Correct |
325 ms |
48340 KB |
Output is correct |
14 |
Correct |
341 ms |
46932 KB |
Output is correct |
15 |
Correct |
393 ms |
47244 KB |
Output is correct |
16 |
Correct |
291 ms |
50240 KB |
Output is correct |
17 |
Correct |
325 ms |
47496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
12888 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
1 ms |
12892 KB |
Output is correct |
5 |
Correct |
1 ms |
12892 KB |
Output is correct |
6 |
Correct |
38 ms |
15520 KB |
Output is correct |
7 |
Correct |
76 ms |
22868 KB |
Output is correct |
8 |
Correct |
289 ms |
56000 KB |
Output is correct |
9 |
Correct |
259 ms |
55680 KB |
Output is correct |
10 |
Correct |
277 ms |
55664 KB |
Output is correct |
11 |
Correct |
297 ms |
56916 KB |
Output is correct |
12 |
Correct |
291 ms |
56492 KB |
Output is correct |
13 |
Correct |
378 ms |
65256 KB |
Output is correct |
14 |
Correct |
164 ms |
46504 KB |
Output is correct |
15 |
Correct |
2 ms |
12888 KB |
Output is correct |
16 |
Correct |
1 ms |
12892 KB |
Output is correct |
17 |
Correct |
1 ms |
12892 KB |
Output is correct |
18 |
Correct |
1 ms |
12892 KB |
Output is correct |
19 |
Correct |
1 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
43 ms |
15824 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
40 ms |
15836 KB |
Output is correct |
25 |
Correct |
50 ms |
15700 KB |
Output is correct |
26 |
Correct |
47 ms |
15956 KB |
Output is correct |
27 |
Correct |
42 ms |
15956 KB |
Output is correct |
28 |
Correct |
69 ms |
22372 KB |
Output is correct |
29 |
Correct |
550 ms |
52820 KB |
Output is correct |
30 |
Correct |
68 ms |
22612 KB |
Output is correct |
31 |
Correct |
522 ms |
52560 KB |
Output is correct |
32 |
Correct |
459 ms |
56232 KB |
Output is correct |
33 |
Correct |
309 ms |
51584 KB |
Output is correct |
34 |
Correct |
521 ms |
56152 KB |
Output is correct |
35 |
Correct |
1 ms |
12888 KB |
Output is correct |
36 |
Correct |
1 ms |
12892 KB |
Output is correct |
37 |
Correct |
47 ms |
15604 KB |
Output is correct |
38 |
Correct |
263 ms |
53156 KB |
Output is correct |
39 |
Correct |
331 ms |
48408 KB |
Output is correct |
40 |
Correct |
364 ms |
46928 KB |
Output is correct |
41 |
Correct |
402 ms |
47480 KB |
Output is correct |
42 |
Correct |
550 ms |
51028 KB |
Output is correct |
43 |
Correct |
326 ms |
51608 KB |
Output is correct |
44 |
Correct |
315 ms |
48464 KB |
Output is correct |
45 |
Correct |
369 ms |
65616 KB |
Output is correct |
46 |
Correct |
184 ms |
47016 KB |
Output is correct |
47 |
Correct |
444 ms |
59964 KB |
Output is correct |
48 |
Correct |
1 ms |
12888 KB |
Output is correct |
49 |
Correct |
2 ms |
12888 KB |
Output is correct |
50 |
Correct |
1 ms |
12892 KB |
Output is correct |
51 |
Correct |
1 ms |
12892 KB |
Output is correct |
52 |
Correct |
1 ms |
12892 KB |
Output is correct |
53 |
Correct |
2 ms |
12892 KB |
Output is correct |
54 |
Correct |
10 ms |
13540 KB |
Output is correct |
55 |
Correct |
8 ms |
13404 KB |
Output is correct |
56 |
Correct |
11 ms |
13516 KB |
Output is correct |
57 |
Correct |
8 ms |
13456 KB |
Output is correct |
58 |
Correct |
12 ms |
13404 KB |
Output is correct |
59 |
Correct |
10 ms |
13404 KB |
Output is correct |
60 |
Correct |
8 ms |
13432 KB |
Output is correct |
61 |
Correct |
42 ms |
15700 KB |
Output is correct |
62 |
Correct |
73 ms |
22124 KB |
Output is correct |
63 |
Correct |
315 ms |
49064 KB |
Output is correct |
64 |
Correct |
335 ms |
47184 KB |
Output is correct |
65 |
Correct |
377 ms |
46928 KB |
Output is correct |
66 |
Correct |
423 ms |
47244 KB |
Output is correct |
67 |
Correct |
515 ms |
51284 KB |
Output is correct |
68 |
Correct |
326 ms |
51536 KB |
Output is correct |
69 |
Correct |
408 ms |
50516 KB |
Output is correct |
70 |
Correct |
270 ms |
53072 KB |
Output is correct |
71 |
Correct |
333 ms |
48208 KB |
Output is correct |
72 |
Correct |
367 ms |
47016 KB |
Output is correct |
73 |
Correct |
410 ms |
47480 KB |
Output is correct |
74 |
Correct |
288 ms |
48384 KB |
Output is correct |
75 |
Correct |
332 ms |
47536 KB |
Output is correct |