#ifdef LIS05ST
#define _GLIBCXX_DEBUG
#define _GLIBCXX_DEBUG_PEDANTIC
#endif
#pragma GCC optimize("O3")
#pragma GCC target("avx2,popcnt,lzcnt")
#include"plants.h"
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
const int NMAX=2e6;
int pref[NMAX+5];
int n,k;
int get(int l,int r){
return pref[r]-pref[l-1];
}
bool one(int l,int r){
return get(l,r)==r-l+1;
}
bool zero(int l,int r){
return get(l,r)==0;
}
vector<int>h;
int id(int x){
return (x%n+n)%n;
}
struct Node{
int min;
int pos;
ll padd;
};
Node tree[4*NMAX+5];
Node merge(Node a,Node b){
Node res;
res.min=min(a.min,b.min);
res.pos=-1;
if(a.min==res.min){
res.pos=max(res.pos,a.pos);
}
if(b.min==res.min){
res.pos=max(res.pos,b.pos);
}
res.padd=0;
return res;
}
vector<int>vec;
void build(int v,int l,int r){
if(l==r){
tree[v].min=vec[l];
tree[v].pos=l;
tree[v].padd=0;
return;
}
int m=(l+r)/2;
build(2*v,l,m);
build(2*v+1,m+1,r);
tree[v]=merge(tree[2*v],tree[2*v+1]);
}
void pushAdd(int v,int val){
tree[v].min+=val;
tree[v].padd+=val;
}
void push(int v,int l,int r){
pushAdd(2*v,tree[v].padd);
pushAdd(2*v+1,tree[v].padd);
tree[v].padd=0;
}
pair<int,int> get(int v,int l,int r,int l0,int r0,bool p=1){
//if(p)cout<<l<<":"<<r<<" "<<l0<<":"<<r0<<"\n";
//if(p)cout<<tree[2*v].min<<" "<<tree[2*v].pos<<"\n";
push(v,l,r);
if(r0<l||r<l0||tree[v].min!=0)return {1e9,1e9};
if(l0<=l&&r<=r0&&l==r)return {tree[v].min,tree[v].pos};
int m=(l+r)/2;
if(tree[2*v].min==0&&tree[2*v].pos>=l0)return get(2*v,l,m,l0,r0,p);
return get(2*v+1,m+1,r,l0,r0,p);
}
void add(int v,int l,int r,int l0,int r0,int val){
push(v,l,r);
if(r0<l||r<l0)return;
if(l0<=l&&r<=r0){
pushAdd(v,val);
return;
}
int m=(l+r)/2;
add(2*v,l,m,l0,r0,val);
add(2*v+1,m+1,r,l0,r0,val);
tree[v]=merge(tree[2*v],tree[2*v+1]);
}
void add(int l,int r,int val){
l-=3*n;r-=3*n;
for(auto e:vector<int>{0,1,2,3,4,5,6,7,8}){
if(0<=r&&r<=3*n-1){
int L=min(3*n-1,max(l,0));
int R=min(3*n-1,max(r,0));
add(1,0,3*n-1,L,R,val);
}
else if(0<=l&&l<=3*n-1){
int L=min(3*n-1,max(l,0));
int R=min(3*n-1,max(r,0));
add(1,0,3*n-1,L,R,val);
}
l+=n;r+=n;
}
}
int step;
void extract(int pos){
while(true){
auto [a,b]=get(1,0,3*n-1,pos-k+1,pos-1);
if(a==0)extract(b);
else break;
}
h[pos%n]=step--;
add(pos-k+1,pos-1,-1);
add(pos,pos,1e9);
}
int lefte[30][NMAX+5];
int righte[30][NMAX+5];
void init(int K, std::vector<int> rr){
n=rr.size();h.resize(n);
k=K;
for(int i=0;i<n;i++)pref[i+1]=pref[i]+rr[i];
for(int e=0;e<3;e++)for(int i=0;i<n;i++){
vec.push_back(rr[i]);
}
//for(auto e:vec)cout<<e<<" ";cout<<"\n";
build(1,0,3*n-1);
int L=0+n+n;int R=n-1+n+n;
step=n;
while(step>=1){
auto[val,pos]=get(1,0,3*n-1,L,R);
extract(pos);
}
for(int i=0;;i++){
if(h.size()==2*n)break;
h.push_back(h[i]);
}
//for(auto e:h)cout<<e<<" ";cout<<"\n";
set<pair<int,int>>st;
int N=2*n;
for(int i=0;i<N;i++){
if(i-1>=0)st.insert({h[i-1],i-1});
if(i-k>=0)st.erase({h[i-k],i-k});
auto it=st.lower_bound({h[i],-1e9});
if(it==st.begin()||st.empty()){
lefte[0][i]=i;
continue;
}
it=prev(it);
lefte[0][i]=it->second;
}
st.clear();
for(int i=N-1;i>=0;i--){
if(i+1<N)st.insert({h[i+1],i+1});
if(i+k<N)st.erase({h[i+k],i+k});
auto it=st.lower_bound({h[i],-1e9});
if(it==st.begin()||st.empty()){
righte[0][i]=i;
continue;
}
it=prev(it);
righte[0][i]=it->second;
}
for(int lvl=1;lvl<21;lvl++){
for(int i=0;i<N;i++){
lefte[lvl][i]=lefte[lvl-1][lefte[lvl-1][i]];
righte[lvl][i]=righte[lvl-1][righte[lvl-1][i]];
}
}
};
int ddist(int u,int v){
return min({abs(u-v),abs(u+n-v),abs(v+n-u)});
}
int lgt(int x,int y){
for(int lvl=20;lvl>=0;lvl--){
int pos=lefte[lvl][x];
if(pos<y)continue;
x=pos;
}
if(ddist(x,y)<k&&h[x]>=h[y])return 1;
return 0;
}
int rgt(int x,int y){
for(int lvl=20;lvl>=0;lvl--){
int pos=righte[lvl][x];
if(pos>y)continue;
x=pos;
}
if(ddist(x,y)<k&&h[x]>=h[y])return 1;
return 0;
}
int gt(int X,int y){
while(X-n>=0)X-=n;
while(y-n>=0)y-=n;
for(int x=X;x<2*n;x+=n){
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
while(y-n>=0){
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
y-=n;
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
}
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
while(y+n<2*n){
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
y+=n;
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
}
if(x<y)if(rgt(x,y))return 1;
if(y<x)if(lgt(x,y))return 1;
}
return 0;
}
int compare_plants(int x, int y){
if(gt(x,y))return 1;
if(gt(y,x))return -1;
return 0;
}
#define MULTITESTS false
void solve(int testCase){
}
// x y
void precalc(){
}
/*
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
precalc();
int t=1;
if(MULTITESTS)cin>>t;
for(int i=1;i<=t;i++){
auto t1=clock();
solve(i);
auto t2=clock();
float delta=t2-t1;
delta/=CLOCKS_PER_SEC;
#ifdef LIS05ST
cout<<"("<<i<<")------------"<<fixed<<setprecision(2)<<delta<<"s\n";
#endif
}
}*/
Compilation message
plants.cpp: In function 'void add(int, int, int)':
plants.cpp:104:14: warning: unused variable 'e' [-Wunused-variable]
104 | for(auto e:vector<int>{0,1,2,3,4,5,6,7,8}){
| ^
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:154:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
154 | if(h.size()==2*n)break;
| ~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
596 KB |
Output is correct |
6 |
Correct |
227 ms |
3392 KB |
Output is correct |
7 |
Correct |
655 ms |
14752 KB |
Output is correct |
8 |
Correct |
2297 ms |
140616 KB |
Output is correct |
9 |
Correct |
2390 ms |
140696 KB |
Output is correct |
10 |
Correct |
2156 ms |
140620 KB |
Output is correct |
11 |
Correct |
1795 ms |
140812 KB |
Output is correct |
12 |
Correct |
1670 ms |
140620 KB |
Output is correct |
13 |
Correct |
779 ms |
140888 KB |
Output is correct |
14 |
Correct |
1888 ms |
140600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
8 ms |
1320 KB |
Output is correct |
7 |
Correct |
296 ms |
6412 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
8 ms |
1320 KB |
Output is correct |
10 |
Correct |
237 ms |
6416 KB |
Output is correct |
11 |
Correct |
86 ms |
6284 KB |
Output is correct |
12 |
Correct |
346 ms |
6544 KB |
Output is correct |
13 |
Correct |
234 ms |
6460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
8 ms |
1320 KB |
Output is correct |
7 |
Correct |
296 ms |
6412 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
8 ms |
1320 KB |
Output is correct |
10 |
Correct |
237 ms |
6416 KB |
Output is correct |
11 |
Correct |
86 ms |
6284 KB |
Output is correct |
12 |
Correct |
346 ms |
6544 KB |
Output is correct |
13 |
Correct |
234 ms |
6460 KB |
Output is correct |
14 |
Correct |
533 ms |
15092 KB |
Output is correct |
15 |
Correct |
2864 ms |
146612 KB |
Output is correct |
16 |
Correct |
484 ms |
15200 KB |
Output is correct |
17 |
Correct |
2997 ms |
146472 KB |
Output is correct |
18 |
Correct |
1207 ms |
145404 KB |
Output is correct |
19 |
Correct |
2315 ms |
145380 KB |
Output is correct |
20 |
Correct |
2568 ms |
150216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
175 ms |
4644 KB |
Output is correct |
4 |
Correct |
1670 ms |
144168 KB |
Output is correct |
5 |
Correct |
1896 ms |
143760 KB |
Output is correct |
6 |
Correct |
2239 ms |
144144 KB |
Output is correct |
7 |
Correct |
2490 ms |
144784 KB |
Output is correct |
8 |
Correct |
2708 ms |
148592 KB |
Output is correct |
9 |
Correct |
1284 ms |
143716 KB |
Output is correct |
10 |
Correct |
1676 ms |
143784 KB |
Output is correct |
11 |
Correct |
752 ms |
143708 KB |
Output is correct |
12 |
Correct |
1904 ms |
143828 KB |
Output is correct |
13 |
Correct |
1129 ms |
146636 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 |
7 ms |
720 KB |
Output is correct |
7 |
Correct |
66 ms |
1740 KB |
Output is correct |
8 |
Correct |
36 ms |
1684 KB |
Output is correct |
9 |
Correct |
47 ms |
1680 KB |
Output is correct |
10 |
Correct |
33 ms |
1680 KB |
Output is correct |
11 |
Correct |
62 ms |
1680 KB |
Output is correct |
12 |
Correct |
49 ms |
1664 KB |
Output is correct |
13 |
Correct |
33 ms |
1744 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 |
0 ms |
596 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1236 KB |
Output is correct |
6 |
Correct |
1550 ms |
142936 KB |
Output is correct |
7 |
Correct |
1482 ms |
143220 KB |
Output is correct |
8 |
Correct |
1514 ms |
143804 KB |
Output is correct |
9 |
Correct |
1782 ms |
147788 KB |
Output is correct |
10 |
Correct |
1868 ms |
142824 KB |
Output is correct |
11 |
Correct |
1343 ms |
147108 KB |
Output is correct |
12 |
Correct |
1111 ms |
143536 KB |
Output is correct |
13 |
Correct |
1512 ms |
142880 KB |
Output is correct |
14 |
Correct |
1396 ms |
143152 KB |
Output is correct |
15 |
Correct |
1552 ms |
143776 KB |
Output is correct |
16 |
Correct |
1084 ms |
142756 KB |
Output is correct |
17 |
Correct |
1336 ms |
143052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
596 KB |
Output is correct |
6 |
Correct |
227 ms |
3392 KB |
Output is correct |
7 |
Correct |
655 ms |
14752 KB |
Output is correct |
8 |
Correct |
2297 ms |
140616 KB |
Output is correct |
9 |
Correct |
2390 ms |
140696 KB |
Output is correct |
10 |
Correct |
2156 ms |
140620 KB |
Output is correct |
11 |
Correct |
1795 ms |
140812 KB |
Output is correct |
12 |
Correct |
1670 ms |
140620 KB |
Output is correct |
13 |
Correct |
779 ms |
140888 KB |
Output is correct |
14 |
Correct |
1888 ms |
140600 KB |
Output is correct |
15 |
Correct |
0 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 |
8 ms |
1320 KB |
Output is correct |
21 |
Correct |
296 ms |
6412 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
8 ms |
1320 KB |
Output is correct |
24 |
Correct |
237 ms |
6416 KB |
Output is correct |
25 |
Correct |
86 ms |
6284 KB |
Output is correct |
26 |
Correct |
346 ms |
6544 KB |
Output is correct |
27 |
Correct |
234 ms |
6460 KB |
Output is correct |
28 |
Correct |
533 ms |
15092 KB |
Output is correct |
29 |
Correct |
2864 ms |
146612 KB |
Output is correct |
30 |
Correct |
484 ms |
15200 KB |
Output is correct |
31 |
Correct |
2997 ms |
146472 KB |
Output is correct |
32 |
Correct |
1207 ms |
145404 KB |
Output is correct |
33 |
Correct |
2315 ms |
145380 KB |
Output is correct |
34 |
Correct |
2568 ms |
150216 KB |
Output is correct |
35 |
Correct |
0 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
175 ms |
4644 KB |
Output is correct |
38 |
Correct |
1670 ms |
144168 KB |
Output is correct |
39 |
Correct |
1896 ms |
143760 KB |
Output is correct |
40 |
Correct |
2239 ms |
144144 KB |
Output is correct |
41 |
Correct |
2490 ms |
144784 KB |
Output is correct |
42 |
Correct |
2708 ms |
148592 KB |
Output is correct |
43 |
Correct |
1284 ms |
143716 KB |
Output is correct |
44 |
Correct |
1676 ms |
143784 KB |
Output is correct |
45 |
Correct |
752 ms |
143708 KB |
Output is correct |
46 |
Correct |
1904 ms |
143828 KB |
Output is correct |
47 |
Correct |
1129 ms |
146636 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 |
7 ms |
720 KB |
Output is correct |
54 |
Correct |
66 ms |
1740 KB |
Output is correct |
55 |
Correct |
36 ms |
1684 KB |
Output is correct |
56 |
Correct |
47 ms |
1680 KB |
Output is correct |
57 |
Correct |
33 ms |
1680 KB |
Output is correct |
58 |
Correct |
62 ms |
1680 KB |
Output is correct |
59 |
Correct |
49 ms |
1664 KB |
Output is correct |
60 |
Correct |
33 ms |
1744 KB |
Output is correct |
61 |
Correct |
424 ms |
6348 KB |
Output is correct |
62 |
Correct |
830 ms |
16936 KB |
Output is correct |
63 |
Correct |
3344 ms |
143560 KB |
Output is correct |
64 |
Correct |
1991 ms |
143796 KB |
Output is correct |
65 |
Correct |
2486 ms |
144040 KB |
Output is correct |
66 |
Correct |
2501 ms |
144828 KB |
Output is correct |
67 |
Correct |
2719 ms |
148716 KB |
Output is correct |
68 |
Correct |
2513 ms |
143724 KB |
Output is correct |
69 |
Correct |
2322 ms |
148160 KB |
Output is correct |
70 |
Correct |
2025 ms |
144336 KB |
Output is correct |
71 |
Correct |
2790 ms |
143872 KB |
Output is correct |
72 |
Correct |
2459 ms |
143924 KB |
Output is correct |
73 |
Correct |
2514 ms |
144716 KB |
Output is correct |
74 |
Correct |
3040 ms |
143644 KB |
Output is correct |
75 |
Correct |
1768 ms |
143776 KB |
Output is correct |