#include "plants.h"
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) x.size();
using namespace std;
typedef pair<int,int> ii;
struct node{
int s, e, m;
ii val = ii(0,0); int lazy = 0;
node *l, *r;
node(int S, int E){
s = S, e = E, m = (s+e)/2;
if(s == e) val = ii(0, S);
else{
l = new node(s, m);
r = new node(m+1, e);
val = min(l->val, r->val);
}
}
void apply(int L){
val.first += L;
lazy += L;
}
void push(){
if(s == e) return;
l->apply(lazy);
r->apply(lazy);
lazy = 0;
}
void update(int S, int E, int L){
push();
if(s == S && E == e){
apply(L);
return;
}
else if(E <= m) l->update(S, E, L);
else if(S >= m+1) r->update(S, E, L);
else l->update(S, m, L), r->update(m+1, E, L);
val = min(l->val, r->val);
}
ii query(int S, int E){
push();
if(s == S && E == e) return val;
else if(E <= m) return l->query(S, E);
else if(S >= m+1) return r->query(S, E);
else return min(l->query(S, m), r->query(m+1, E));
}
} *root;
int n, K;
int H[200005];
int height;
void assign(int u){
while(true){
int R = u-1, L = u-K+1;
if(L < 0) L += n; if(R < 0) R += n;
ii res;
if(L <= R) res = root->query(L,R);
else res = min(root->query(L, n-1), root->query(0,R));
if(res.first == 0) assign(res.second);
else break;
}
H[u] = height;
int R = u-1, L = u-K+1;
if(L < 0) L += n; if(R < 0) R += n;
if(L <= R) root->update(L, R, -1);
else root->update(L, n-1, -1), root->update(0, R, -1);
root->update(u, u, 1e9);
height--;
}
ii pl[20][200005];
ii pr[20][200005];
ii get(int i){
if(i < 0) i += n;
if(i >= n) i -= n;
return ii(H[i], i);
}
void init(int _K, vector<int> R) {
n = sz(R); K = _K;
root = new node(0, n-1);
for(int i = 0;i < n;i++) root->update(i,i,R[i]);
fill(H,H+n,-1);
height = n-1;
while(height >= 0){
ii res = root->query(0, n-1);
assign(res.second);
}
set<ii> Left, Right;
for(int k = 1;k < K;k++){
Left.insert(get(-k));
Right.insert(get(k));
}
H[n] = -1; ///dummy height
for(int i = 0;i < n;i++){
auto it = Left.upper_bound(ii(H[i],0));
if(it != Left.begin()){
it--;
int dis = i - it->second;
if(dis < 0) dis += n;
pl[0][i] = ii(it->second, dis);
}
else pl[0][i] = ii(n, -1);
it = Right.upper_bound(ii(H[i],0));
if(it != Right.begin()){
it--;
int dis = it->second - i;
if(dis < 0) dis += n;
pr[0][i] = ii(it->second, dis);
}
else pr[0][i] = ii(n, -1);
Left.erase(get(i-K+1));
Left.insert(get(i));
Right.erase(get(i+1));
Right.insert(get(i+K));
}
for(int k = 0;k <= 18;k++) pl[k][n] = ii(n, -1), pr[k][n] = ii(n, -1); ///dummy
for(int k = 1;k <= 18;k++){
for(int i = 0;i < n;i++){
ii a = pl[k-1][i];
ii b = pl[k-1][a.first];
pl[k][i] = ii(b.first, a.second+b.second);
a = pr[k-1][i];
b = pr[k-1][a.first];
pr[k][i] = ii(b.first, a.second+b.second);
}
}
return;
}
int compare_plants(int x, int y) {
int ans = 1;
if(H[x] < H[y]){
swap(x, y);
ans *= -1;
}
long long dL = 0, dR = 0;
int u = x;
for(int k = 18;k >= 0;k--){
ii toL = pl[k][u];
if(H[toL.first] > H[y]){
dL += toL.second;
u = toL.first;
}
}
u = x;
for(int k = 18;k >= 0;k--){
ii toR = pr[k][u];
if(H[toR.first] > H[y]){
dR += toR.second;
u = toR.first;
}
}
int needL = x - y;
if(needL < 0) needL += n;
needL -= (K-1);
int needR = y - x;
if(needR < 0) needR += n;
needR -= (K-1);
if(dL < needL && dR < needR) ans = 0;
return ans;
}
Compilation message
plants.cpp: In function 'void assign(int)':
plants.cpp:63:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
63 | if(L < 0) L += n; if(R < 0) R += n;
| ^~
plants.cpp:63:21: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
63 | if(L < 0) L += n; if(R < 0) R += n;
| ^~
plants.cpp:75:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
75 | if(L < 0) L += n; if(R < 0) R += n;
| ^~
plants.cpp:75:20: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
75 | if(L < 0) L += n; if(R < 0) R += n;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
77 ms |
3692 KB |
Output is correct |
7 |
Correct |
194 ms |
11756 KB |
Output is correct |
8 |
Correct |
530 ms |
86252 KB |
Output is correct |
9 |
Correct |
573 ms |
86252 KB |
Output is correct |
10 |
Correct |
608 ms |
86292 KB |
Output is correct |
11 |
Correct |
653 ms |
86252 KB |
Output is correct |
12 |
Correct |
652 ms |
86252 KB |
Output is correct |
13 |
Correct |
770 ms |
86252 KB |
Output is correct |
14 |
Correct |
768 ms |
86252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
6 ms |
1132 KB |
Output is correct |
7 |
Correct |
175 ms |
7788 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
6 ms |
1132 KB |
Output is correct |
10 |
Correct |
175 ms |
7808 KB |
Output is correct |
11 |
Correct |
129 ms |
7532 KB |
Output is correct |
12 |
Correct |
132 ms |
7660 KB |
Output is correct |
13 |
Correct |
171 ms |
7788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
6 ms |
1132 KB |
Output is correct |
7 |
Correct |
175 ms |
7788 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
6 ms |
1132 KB |
Output is correct |
10 |
Correct |
175 ms |
7808 KB |
Output is correct |
11 |
Correct |
129 ms |
7532 KB |
Output is correct |
12 |
Correct |
132 ms |
7660 KB |
Output is correct |
13 |
Correct |
171 ms |
7788 KB |
Output is correct |
14 |
Correct |
361 ms |
14700 KB |
Output is correct |
15 |
Incorrect |
2416 ms |
98632 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
114 ms |
4588 KB |
Output is correct |
4 |
Correct |
822 ms |
89324 KB |
Output is correct |
5 |
Correct |
975 ms |
86892 KB |
Output is correct |
6 |
Correct |
1256 ms |
86764 KB |
Output is correct |
7 |
Correct |
1591 ms |
88000 KB |
Output is correct |
8 |
Incorrect |
2242 ms |
95564 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
3 ms |
748 KB |
Output is correct |
7 |
Correct |
21 ms |
1516 KB |
Output is correct |
8 |
Correct |
23 ms |
1644 KB |
Output is correct |
9 |
Correct |
31 ms |
1644 KB |
Output is correct |
10 |
Correct |
23 ms |
1664 KB |
Output is correct |
11 |
Correct |
21 ms |
1644 KB |
Output is correct |
12 |
Correct |
22 ms |
1644 KB |
Output is correct |
13 |
Correct |
24 ms |
1644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
1004 KB |
Output is correct |
6 |
Correct |
574 ms |
85768 KB |
Output is correct |
7 |
Correct |
1030 ms |
85868 KB |
Output is correct |
8 |
Correct |
1228 ms |
87020 KB |
Output is correct |
9 |
Incorrect |
1996 ms |
94700 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
77 ms |
3692 KB |
Output is correct |
7 |
Correct |
194 ms |
11756 KB |
Output is correct |
8 |
Correct |
530 ms |
86252 KB |
Output is correct |
9 |
Correct |
573 ms |
86252 KB |
Output is correct |
10 |
Correct |
608 ms |
86292 KB |
Output is correct |
11 |
Correct |
653 ms |
86252 KB |
Output is correct |
12 |
Correct |
652 ms |
86252 KB |
Output is correct |
13 |
Correct |
770 ms |
86252 KB |
Output is correct |
14 |
Correct |
768 ms |
86252 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
6 ms |
1132 KB |
Output is correct |
21 |
Correct |
175 ms |
7788 KB |
Output is correct |
22 |
Correct |
3 ms |
748 KB |
Output is correct |
23 |
Correct |
6 ms |
1132 KB |
Output is correct |
24 |
Correct |
175 ms |
7808 KB |
Output is correct |
25 |
Correct |
129 ms |
7532 KB |
Output is correct |
26 |
Correct |
132 ms |
7660 KB |
Output is correct |
27 |
Correct |
171 ms |
7788 KB |
Output is correct |
28 |
Correct |
361 ms |
14700 KB |
Output is correct |
29 |
Incorrect |
2416 ms |
98632 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |