#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 8;
const int mlog = 20;
const int inf = 1e7;
int N,K,seg[4*maxn],lazy[4*maxn],indexing=1,arr[maxn],rev[maxn],lf[mlog][maxn],sl[mlog][maxn],rg[mlog][maxn],sr[mlog][maxn];
void push(int id){
lazy[id<<1] += lazy[id];
lazy[id<<1|1] += lazy[id];
seg[id<<1] += lazy[id];
seg[id<<1|1] += lazy[id];
lazy[id] = 0;
}
void upd(int id,int tl,int tr,int ul,int ur,int v){
if(tl > tr || ul > tr || ur < tl) return;
if(ul <= tl && tr <= ur){
seg[id] += v;
lazy[id] += v;
return;
}
int tm = (tl+tr)/2;
push(id);
upd(id<<1,tl,tm,ul,ur,v);
upd(id<<1|1,tm+1,tr,ul,ur,v);
seg[id] = min(seg[id<<1],seg[id<<1|1]);
}
int qryfull(int id,int tl,int tr){
if(seg[id] != 0) return -1;
if(tl == tr) return tl;
int tm = (tl+tr)/2;
push(id);
if(seg[id<<1] != 0) return qryfull(id<<1|1,tm+1,tr);
else return qryfull(id<<1,tl,tm);
}
void init(int k, std::vector<int> r){
K = k;
N = r.size();
for(int i=0;i<N;i++) upd(1,0,N-1,i,i,r[i]);
set<int> lst,vld;
int temp = N;
auto Check = [&](int x){
x %= N;
if(lst.empty()) return true;
int tx = *prev(lst.lower_bound(x+N));
if(x+N-tx < K) return false;
return true;
};
while(temp--){
while(true){
int id = qryfull(1,0,N-1);
if(id == -1) break;
upd(1,0,N-1,id,id,inf);
if(Check(id)) vld.insert(id);
lst.insert(id);
lst.insert(N+id);
if(!lst.empty() && !Check(*(lst.upper_bound(id)))) vld.erase((*(lst.upper_bound(id)))%N);
}
int target = *(vld.begin());
rev[indexing] = target;
arr[target] = indexing++;
if(target > 0) upd(1,0,N-1,max(0,target-K+1),target-1,-1);
if(0 > target-K+1) upd(1,0,N-1,N+target-K+1,N-1,-1);
vld.erase(target);
lst.erase(target);
lst.erase(target+N);
if(!lst.empty() && Check(*(lst.upper_bound(target)))) vld.insert((*(lst.upper_bound(target)))%N);
}
set<int> st;
for(int i=1-K;i<0;i++) st.insert(arr[N+i]);
for(int i=0;i<N;i++){
auto it = st.lower_bound(arr[i]);
if(it == st.begin()) lf[0][i] = i;
else lf[0][i] = rev[*prev(it)];
sl[0][i] = (N+i-lf[0][i])%N;
st.insert(arr[i]);
st.erase(arr[(N+i-K+1)%N]);
}
st.clear();
for(int i=1;i<K;i++) st.insert(arr[i]);
for(int i=0;i<N;i++){
auto it = st.lower_bound(arr[i]);
if(it == st.begin()) rg[0][i] = i;
else rg[0][i] = rev[*prev(it)];
sr[0][i] = (N+rg[0][i]-i)%N;
st.insert(arr[(i+K)%N]);
st.erase(arr[(i+1)%N]);
}
st.clear();
for(int i=1;i<mlog;i++){
for(int j=0;j<N;j++){
lf[i][j] = lf[i-1][lf[i-1][j]];
sl[i][j] = min(inf,sl[i-1][j]+sl[i-1][lf[i-1][j]]);
rg[i][j] = rg[i-1][rg[i-1][j]];
sr[i][j] = min(inf,sr[i-1][j]+sr[i-1][rg[i-1][j]]);
}
}
}
bool comp(int x,int y){
int tx = x;
int dx = (N+x-y)%N;
for(int i=mlog-1;i>=0;i--){
if(sl[i][tx] <= dx){
dx -= sl[i][tx];
tx = lf[i][tx];
}
}
int ty = x;
int dy = (N+y-x)%N;
for(int i=mlog-1;i>=0;i--){
if(sr[i][ty] <= dy){
dy -= sr[i][ty];
ty = rg[i][ty];
}
}
if(dx <= sl[0][tx] && arr[y] <= arr[tx]) return true;
if(dy <= sr[0][ty] && arr[y] <= arr[ty]) return true;
return false;
}
int compare_plants(int x, int y){
bool a = comp(x,y);
if(a) return -1;
bool b = comp(y,x);
if(b) return 1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
688 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
95 ms |
3620 KB |
Output is correct |
7 |
Correct |
234 ms |
10948 KB |
Output is correct |
8 |
Correct |
1068 ms |
82292 KB |
Output is correct |
9 |
Correct |
1082 ms |
81520 KB |
Output is correct |
10 |
Correct |
1100 ms |
81628 KB |
Output is correct |
11 |
Correct |
1227 ms |
82764 KB |
Output is correct |
12 |
Correct |
1231 ms |
82356 KB |
Output is correct |
13 |
Correct |
1427 ms |
91320 KB |
Output is correct |
14 |
Correct |
723 ms |
72416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
4 ms |
1100 KB |
Output is correct |
7 |
Correct |
98 ms |
5492 KB |
Output is correct |
8 |
Correct |
3 ms |
820 KB |
Output is correct |
9 |
Correct |
4 ms |
1228 KB |
Output is correct |
10 |
Correct |
122 ms |
5572 KB |
Output is correct |
11 |
Correct |
181 ms |
5604 KB |
Output is correct |
12 |
Correct |
115 ms |
5740 KB |
Output is correct |
13 |
Correct |
93 ms |
5688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
4 ms |
1100 KB |
Output is correct |
7 |
Correct |
98 ms |
5492 KB |
Output is correct |
8 |
Correct |
3 ms |
820 KB |
Output is correct |
9 |
Correct |
4 ms |
1228 KB |
Output is correct |
10 |
Correct |
122 ms |
5572 KB |
Output is correct |
11 |
Correct |
181 ms |
5604 KB |
Output is correct |
12 |
Correct |
115 ms |
5740 KB |
Output is correct |
13 |
Correct |
93 ms |
5688 KB |
Output is correct |
14 |
Correct |
148 ms |
11180 KB |
Output is correct |
15 |
Correct |
1055 ms |
78320 KB |
Output is correct |
16 |
Correct |
161 ms |
13252 KB |
Output is correct |
17 |
Correct |
1043 ms |
81976 KB |
Output is correct |
18 |
Correct |
1428 ms |
85184 KB |
Output is correct |
19 |
Correct |
919 ms |
81012 KB |
Output is correct |
20 |
Correct |
1103 ms |
85572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
676 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
130 ms |
4232 KB |
Output is correct |
4 |
Correct |
991 ms |
78880 KB |
Output is correct |
5 |
Correct |
977 ms |
73956 KB |
Output is correct |
6 |
Correct |
922 ms |
72900 KB |
Output is correct |
7 |
Correct |
915 ms |
73128 KB |
Output is correct |
8 |
Correct |
974 ms |
76976 KB |
Output is correct |
9 |
Correct |
1255 ms |
80216 KB |
Output is correct |
10 |
Correct |
970 ms |
76856 KB |
Output is correct |
11 |
Correct |
1373 ms |
94260 KB |
Output is correct |
12 |
Correct |
772 ms |
75552 KB |
Output is correct |
13 |
Correct |
1518 ms |
88720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
684 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
3 ms |
844 KB |
Output is correct |
7 |
Correct |
27 ms |
1612 KB |
Output is correct |
8 |
Correct |
26 ms |
1592 KB |
Output is correct |
9 |
Correct |
26 ms |
1532 KB |
Output is correct |
10 |
Correct |
18 ms |
1612 KB |
Output is correct |
11 |
Correct |
25 ms |
1540 KB |
Output is correct |
12 |
Correct |
24 ms |
1604 KB |
Output is correct |
13 |
Correct |
16 ms |
1612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
4 ms |
1000 KB |
Output is correct |
6 |
Correct |
710 ms |
72780 KB |
Output is correct |
7 |
Correct |
600 ms |
72516 KB |
Output is correct |
8 |
Correct |
834 ms |
72948 KB |
Output is correct |
9 |
Correct |
775 ms |
76812 KB |
Output is correct |
10 |
Correct |
882 ms |
79188 KB |
Output is correct |
11 |
Correct |
836 ms |
79184 KB |
Output is correct |
12 |
Correct |
707 ms |
80736 KB |
Output is correct |
13 |
Correct |
775 ms |
76100 KB |
Output is correct |
14 |
Correct |
773 ms |
74948 KB |
Output is correct |
15 |
Correct |
631 ms |
75528 KB |
Output is correct |
16 |
Correct |
660 ms |
77448 KB |
Output is correct |
17 |
Correct |
603 ms |
75076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
716 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
1 ms |
688 KB |
Output is correct |
5 |
Correct |
1 ms |
716 KB |
Output is correct |
6 |
Correct |
95 ms |
3620 KB |
Output is correct |
7 |
Correct |
234 ms |
10948 KB |
Output is correct |
8 |
Correct |
1068 ms |
82292 KB |
Output is correct |
9 |
Correct |
1082 ms |
81520 KB |
Output is correct |
10 |
Correct |
1100 ms |
81628 KB |
Output is correct |
11 |
Correct |
1227 ms |
82764 KB |
Output is correct |
12 |
Correct |
1231 ms |
82356 KB |
Output is correct |
13 |
Correct |
1427 ms |
91320 KB |
Output is correct |
14 |
Correct |
723 ms |
72416 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
16 |
Correct |
1 ms |
716 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
716 KB |
Output is correct |
20 |
Correct |
4 ms |
1100 KB |
Output is correct |
21 |
Correct |
98 ms |
5492 KB |
Output is correct |
22 |
Correct |
3 ms |
820 KB |
Output is correct |
23 |
Correct |
4 ms |
1228 KB |
Output is correct |
24 |
Correct |
122 ms |
5572 KB |
Output is correct |
25 |
Correct |
181 ms |
5604 KB |
Output is correct |
26 |
Correct |
115 ms |
5740 KB |
Output is correct |
27 |
Correct |
93 ms |
5688 KB |
Output is correct |
28 |
Correct |
148 ms |
11180 KB |
Output is correct |
29 |
Correct |
1055 ms |
78320 KB |
Output is correct |
30 |
Correct |
161 ms |
13252 KB |
Output is correct |
31 |
Correct |
1043 ms |
81976 KB |
Output is correct |
32 |
Correct |
1428 ms |
85184 KB |
Output is correct |
33 |
Correct |
919 ms |
81012 KB |
Output is correct |
34 |
Correct |
1103 ms |
85572 KB |
Output is correct |
35 |
Correct |
1 ms |
676 KB |
Output is correct |
36 |
Correct |
1 ms |
716 KB |
Output is correct |
37 |
Correct |
130 ms |
4232 KB |
Output is correct |
38 |
Correct |
991 ms |
78880 KB |
Output is correct |
39 |
Correct |
977 ms |
73956 KB |
Output is correct |
40 |
Correct |
922 ms |
72900 KB |
Output is correct |
41 |
Correct |
915 ms |
73128 KB |
Output is correct |
42 |
Correct |
974 ms |
76976 KB |
Output is correct |
43 |
Correct |
1255 ms |
80216 KB |
Output is correct |
44 |
Correct |
970 ms |
76856 KB |
Output is correct |
45 |
Correct |
1373 ms |
94260 KB |
Output is correct |
46 |
Correct |
772 ms |
75552 KB |
Output is correct |
47 |
Correct |
1518 ms |
88720 KB |
Output is correct |
48 |
Correct |
1 ms |
716 KB |
Output is correct |
49 |
Correct |
1 ms |
716 KB |
Output is correct |
50 |
Correct |
1 ms |
684 KB |
Output is correct |
51 |
Correct |
1 ms |
716 KB |
Output is correct |
52 |
Correct |
1 ms |
716 KB |
Output is correct |
53 |
Correct |
3 ms |
844 KB |
Output is correct |
54 |
Correct |
27 ms |
1612 KB |
Output is correct |
55 |
Correct |
26 ms |
1592 KB |
Output is correct |
56 |
Correct |
26 ms |
1532 KB |
Output is correct |
57 |
Correct |
18 ms |
1612 KB |
Output is correct |
58 |
Correct |
25 ms |
1540 KB |
Output is correct |
59 |
Correct |
24 ms |
1604 KB |
Output is correct |
60 |
Correct |
16 ms |
1612 KB |
Output is correct |
61 |
Correct |
149 ms |
5928 KB |
Output is correct |
62 |
Correct |
295 ms |
12960 KB |
Output is correct |
63 |
Correct |
1308 ms |
77840 KB |
Output is correct |
64 |
Correct |
885 ms |
75732 KB |
Output is correct |
65 |
Correct |
1038 ms |
75708 KB |
Output is correct |
66 |
Correct |
918 ms |
76336 KB |
Output is correct |
67 |
Correct |
980 ms |
80320 KB |
Output is correct |
68 |
Correct |
1252 ms |
80100 KB |
Output is correct |
69 |
Correct |
1104 ms |
79796 KB |
Output is correct |
70 |
Correct |
1063 ms |
81660 KB |
Output is correct |
71 |
Correct |
1084 ms |
76876 KB |
Output is correct |
72 |
Correct |
939 ms |
75876 KB |
Output is correct |
73 |
Correct |
907 ms |
76496 KB |
Output is correct |
74 |
Correct |
1299 ms |
77132 KB |
Output is correct |
75 |
Correct |
930 ms |
75880 KB |
Output is correct |