#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);
| ~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 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 |
46 ms |
16464 KB |
Output is correct |
7 |
Correct |
65 ms |
24896 KB |
Output is correct |
8 |
Correct |
251 ms |
58964 KB |
Output is correct |
9 |
Correct |
260 ms |
58452 KB |
Output is correct |
10 |
Correct |
271 ms |
58708 KB |
Output is correct |
11 |
Correct |
306 ms |
59732 KB |
Output is correct |
12 |
Correct |
297 ms |
59220 KB |
Output is correct |
13 |
Correct |
368 ms |
68204 KB |
Output is correct |
14 |
Correct |
166 ms |
49396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 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 |
12956 KB |
Output is correct |
7 |
Correct |
40 ms |
15796 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12880 KB |
Output is correct |
10 |
Correct |
43 ms |
15788 KB |
Output is correct |
11 |
Correct |
51 ms |
15700 KB |
Output is correct |
12 |
Correct |
50 ms |
15788 KB |
Output is correct |
13 |
Correct |
41 ms |
15828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 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 |
12956 KB |
Output is correct |
7 |
Correct |
40 ms |
15796 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
3 ms |
12880 KB |
Output is correct |
10 |
Correct |
43 ms |
15788 KB |
Output is correct |
11 |
Correct |
51 ms |
15700 KB |
Output is correct |
12 |
Correct |
50 ms |
15788 KB |
Output is correct |
13 |
Correct |
41 ms |
15828 KB |
Output is correct |
14 |
Correct |
69 ms |
22612 KB |
Output is correct |
15 |
Correct |
535 ms |
52592 KB |
Output is correct |
16 |
Correct |
70 ms |
22608 KB |
Output is correct |
17 |
Correct |
529 ms |
52596 KB |
Output is correct |
18 |
Correct |
477 ms |
56400 KB |
Output is correct |
19 |
Correct |
279 ms |
51536 KB |
Output is correct |
20 |
Correct |
538 ms |
56384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
13144 KB |
Output is correct |
2 |
Correct |
2 ms |
12888 KB |
Output is correct |
3 |
Correct |
48 ms |
15776 KB |
Output is correct |
4 |
Correct |
297 ms |
53076 KB |
Output is correct |
5 |
Correct |
342 ms |
48348 KB |
Output is correct |
6 |
Correct |
384 ms |
46928 KB |
Output is correct |
7 |
Correct |
423 ms |
47480 KB |
Output is correct |
8 |
Correct |
511 ms |
51064 KB |
Output is correct |
9 |
Correct |
313 ms |
51608 KB |
Output is correct |
10 |
Correct |
310 ms |
48464 KB |
Output is correct |
11 |
Correct |
360 ms |
65364 KB |
Output is correct |
12 |
Correct |
190 ms |
46928 KB |
Output is correct |
13 |
Correct |
457 ms |
60068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12888 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
11 ms |
13760 KB |
Output is correct |
8 |
Correct |
13 ms |
13660 KB |
Output is correct |
9 |
Correct |
12 ms |
13656 KB |
Output is correct |
10 |
Correct |
8 ms |
13660 KB |
Output is correct |
11 |
Correct |
10 ms |
13660 KB |
Output is correct |
12 |
Correct |
10 ms |
13660 KB |
Output is correct |
13 |
Correct |
8 ms |
13780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12892 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 |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
337 ms |
47040 KB |
Output is correct |
7 |
Correct |
367 ms |
46928 KB |
Output is correct |
8 |
Correct |
472 ms |
47700 KB |
Output is correct |
9 |
Correct |
566 ms |
51056 KB |
Output is correct |
10 |
Correct |
340 ms |
51604 KB |
Output is correct |
11 |
Correct |
469 ms |
50712 KB |
Output is correct |
12 |
Correct |
303 ms |
53220 KB |
Output is correct |
13 |
Correct |
344 ms |
48300 KB |
Output is correct |
14 |
Correct |
395 ms |
47108 KB |
Output is correct |
15 |
Correct |
433 ms |
47224 KB |
Output is correct |
16 |
Correct |
333 ms |
50004 KB |
Output is correct |
17 |
Correct |
348 ms |
47312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 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 |
46 ms |
16464 KB |
Output is correct |
7 |
Correct |
65 ms |
24896 KB |
Output is correct |
8 |
Correct |
251 ms |
58964 KB |
Output is correct |
9 |
Correct |
260 ms |
58452 KB |
Output is correct |
10 |
Correct |
271 ms |
58708 KB |
Output is correct |
11 |
Correct |
306 ms |
59732 KB |
Output is correct |
12 |
Correct |
297 ms |
59220 KB |
Output is correct |
13 |
Correct |
368 ms |
68204 KB |
Output is correct |
14 |
Correct |
166 ms |
49396 KB |
Output is correct |
15 |
Correct |
2 ms |
12888 KB |
Output is correct |
16 |
Correct |
2 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 |
12956 KB |
Output is correct |
21 |
Correct |
40 ms |
15796 KB |
Output is correct |
22 |
Correct |
2 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12880 KB |
Output is correct |
24 |
Correct |
43 ms |
15788 KB |
Output is correct |
25 |
Correct |
51 ms |
15700 KB |
Output is correct |
26 |
Correct |
50 ms |
15788 KB |
Output is correct |
27 |
Correct |
41 ms |
15828 KB |
Output is correct |
28 |
Correct |
69 ms |
22612 KB |
Output is correct |
29 |
Correct |
535 ms |
52592 KB |
Output is correct |
30 |
Correct |
70 ms |
22608 KB |
Output is correct |
31 |
Correct |
529 ms |
52596 KB |
Output is correct |
32 |
Correct |
477 ms |
56400 KB |
Output is correct |
33 |
Correct |
279 ms |
51536 KB |
Output is correct |
34 |
Correct |
538 ms |
56384 KB |
Output is correct |
35 |
Correct |
1 ms |
13144 KB |
Output is correct |
36 |
Correct |
2 ms |
12888 KB |
Output is correct |
37 |
Correct |
48 ms |
15776 KB |
Output is correct |
38 |
Correct |
297 ms |
53076 KB |
Output is correct |
39 |
Correct |
342 ms |
48348 KB |
Output is correct |
40 |
Correct |
384 ms |
46928 KB |
Output is correct |
41 |
Correct |
423 ms |
47480 KB |
Output is correct |
42 |
Correct |
511 ms |
51064 KB |
Output is correct |
43 |
Correct |
313 ms |
51608 KB |
Output is correct |
44 |
Correct |
310 ms |
48464 KB |
Output is correct |
45 |
Correct |
360 ms |
65364 KB |
Output is correct |
46 |
Correct |
190 ms |
46928 KB |
Output is correct |
47 |
Correct |
457 ms |
60068 KB |
Output is correct |
48 |
Correct |
2 ms |
12892 KB |
Output is correct |
49 |
Correct |
1 ms |
12892 KB |
Output is correct |
50 |
Correct |
1 ms |
12892 KB |
Output is correct |
51 |
Correct |
2 ms |
12892 KB |
Output is correct |
52 |
Correct |
2 ms |
12888 KB |
Output is correct |
53 |
Correct |
3 ms |
12892 KB |
Output is correct |
54 |
Correct |
11 ms |
13760 KB |
Output is correct |
55 |
Correct |
13 ms |
13660 KB |
Output is correct |
56 |
Correct |
12 ms |
13656 KB |
Output is correct |
57 |
Correct |
8 ms |
13660 KB |
Output is correct |
58 |
Correct |
10 ms |
13660 KB |
Output is correct |
59 |
Correct |
10 ms |
13660 KB |
Output is correct |
60 |
Correct |
8 ms |
13780 KB |
Output is correct |
61 |
Correct |
48 ms |
17232 KB |
Output is correct |
62 |
Correct |
68 ms |
24288 KB |
Output is correct |
63 |
Correct |
303 ms |
51864 KB |
Output is correct |
64 |
Correct |
386 ms |
50260 KB |
Output is correct |
65 |
Correct |
456 ms |
50316 KB |
Output is correct |
66 |
Correct |
452 ms |
50844 KB |
Output is correct |
67 |
Correct |
553 ms |
54872 KB |
Output is correct |
68 |
Correct |
353 ms |
54424 KB |
Output is correct |
69 |
Correct |
426 ms |
54220 KB |
Output is correct |
70 |
Correct |
301 ms |
56144 KB |
Output is correct |
71 |
Correct |
379 ms |
51284 KB |
Output is correct |
72 |
Correct |
403 ms |
50260 KB |
Output is correct |
73 |
Correct |
462 ms |
50772 KB |
Output is correct |
74 |
Correct |
284 ms |
51284 KB |
Output is correct |
75 |
Correct |
364 ms |
50424 KB |
Output is correct |