#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<long long,long long> 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;
}
}
long long needL = x - y;
if(needL < 0) needL += n;
needL -= (K-1);
long long 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 |
3436 KB |
Output is correct |
7 |
Correct |
242 ms |
18068 KB |
Output is correct |
8 |
Correct |
570 ms |
149228 KB |
Output is correct |
9 |
Correct |
620 ms |
149228 KB |
Output is correct |
10 |
Correct |
662 ms |
149228 KB |
Output is correct |
11 |
Correct |
698 ms |
149228 KB |
Output is correct |
12 |
Correct |
697 ms |
149228 KB |
Output is correct |
13 |
Correct |
824 ms |
149228 KB |
Output is correct |
14 |
Correct |
813 ms |
149356 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 |
748 KB |
Output is correct |
6 |
Correct |
7 ms |
1536 KB |
Output is correct |
7 |
Correct |
185 ms |
7788 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
7 ms |
1516 KB |
Output is correct |
10 |
Correct |
182 ms |
7788 KB |
Output is correct |
11 |
Correct |
140 ms |
7532 KB |
Output is correct |
12 |
Correct |
142 ms |
7660 KB |
Output is correct |
13 |
Correct |
184 ms |
7916 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 |
748 KB |
Output is correct |
6 |
Correct |
7 ms |
1536 KB |
Output is correct |
7 |
Correct |
185 ms |
7788 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
7 ms |
1516 KB |
Output is correct |
10 |
Correct |
182 ms |
7788 KB |
Output is correct |
11 |
Correct |
140 ms |
7532 KB |
Output is correct |
12 |
Correct |
142 ms |
7660 KB |
Output is correct |
13 |
Correct |
184 ms |
7916 KB |
Output is correct |
14 |
Correct |
529 ms |
19708 KB |
Output is correct |
15 |
Correct |
2643 ms |
164716 KB |
Output is correct |
16 |
Correct |
511 ms |
21612 KB |
Output is correct |
17 |
Correct |
2681 ms |
168460 KB |
Output is correct |
18 |
Correct |
1162 ms |
164844 KB |
Output is correct |
19 |
Correct |
1236 ms |
165356 KB |
Output is correct |
20 |
Correct |
2481 ms |
177908 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 |
118 ms |
4972 KB |
Output is correct |
4 |
Correct |
902 ms |
152684 KB |
Output is correct |
5 |
Correct |
1041 ms |
149740 KB |
Output is correct |
6 |
Correct |
1374 ms |
149484 KB |
Output is correct |
7 |
Correct |
1692 ms |
150508 KB |
Output is correct |
8 |
Correct |
2473 ms |
160708 KB |
Output is correct |
9 |
Correct |
923 ms |
152300 KB |
Output is correct |
10 |
Correct |
907 ms |
152044 KB |
Output is correct |
11 |
Correct |
818 ms |
151916 KB |
Output is correct |
12 |
Correct |
915 ms |
152172 KB |
Output is correct |
13 |
Correct |
1109 ms |
159776 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 |
3 ms |
748 KB |
Output is correct |
7 |
Correct |
21 ms |
1516 KB |
Output is correct |
8 |
Correct |
23 ms |
1516 KB |
Output is correct |
9 |
Correct |
22 ms |
1516 KB |
Output is correct |
10 |
Correct |
24 ms |
1516 KB |
Output is correct |
11 |
Correct |
22 ms |
1516 KB |
Output is correct |
12 |
Correct |
22 ms |
1644 KB |
Output is correct |
13 |
Correct |
24 ms |
1516 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 |
4 ms |
1388 KB |
Output is correct |
6 |
Correct |
609 ms |
149260 KB |
Output is correct |
7 |
Correct |
1121 ms |
149612 KB |
Output is correct |
8 |
Correct |
1307 ms |
150508 KB |
Output is correct |
9 |
Correct |
2204 ms |
160564 KB |
Output is correct |
10 |
Correct |
557 ms |
151532 KB |
Output is correct |
11 |
Correct |
1105 ms |
161900 KB |
Output is correct |
12 |
Correct |
655 ms |
155500 KB |
Output is correct |
13 |
Correct |
698 ms |
151660 KB |
Output is correct |
14 |
Correct |
1069 ms |
151788 KB |
Output is correct |
15 |
Correct |
1462 ms |
152940 KB |
Output is correct |
16 |
Correct |
670 ms |
151148 KB |
Output is correct |
17 |
Correct |
656 ms |
151404 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 |
77 ms |
3436 KB |
Output is correct |
7 |
Correct |
242 ms |
18068 KB |
Output is correct |
8 |
Correct |
570 ms |
149228 KB |
Output is correct |
9 |
Correct |
620 ms |
149228 KB |
Output is correct |
10 |
Correct |
662 ms |
149228 KB |
Output is correct |
11 |
Correct |
698 ms |
149228 KB |
Output is correct |
12 |
Correct |
697 ms |
149228 KB |
Output is correct |
13 |
Correct |
824 ms |
149228 KB |
Output is correct |
14 |
Correct |
813 ms |
149356 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 |
748 KB |
Output is correct |
20 |
Correct |
7 ms |
1536 KB |
Output is correct |
21 |
Correct |
185 ms |
7788 KB |
Output is correct |
22 |
Correct |
3 ms |
748 KB |
Output is correct |
23 |
Correct |
7 ms |
1516 KB |
Output is correct |
24 |
Correct |
182 ms |
7788 KB |
Output is correct |
25 |
Correct |
140 ms |
7532 KB |
Output is correct |
26 |
Correct |
142 ms |
7660 KB |
Output is correct |
27 |
Correct |
184 ms |
7916 KB |
Output is correct |
28 |
Correct |
529 ms |
19708 KB |
Output is correct |
29 |
Correct |
2643 ms |
164716 KB |
Output is correct |
30 |
Correct |
511 ms |
21612 KB |
Output is correct |
31 |
Correct |
2681 ms |
168460 KB |
Output is correct |
32 |
Correct |
1162 ms |
164844 KB |
Output is correct |
33 |
Correct |
1236 ms |
165356 KB |
Output is correct |
34 |
Correct |
2481 ms |
177908 KB |
Output is correct |
35 |
Correct |
1 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
620 KB |
Output is correct |
37 |
Correct |
118 ms |
4972 KB |
Output is correct |
38 |
Correct |
902 ms |
152684 KB |
Output is correct |
39 |
Correct |
1041 ms |
149740 KB |
Output is correct |
40 |
Correct |
1374 ms |
149484 KB |
Output is correct |
41 |
Correct |
1692 ms |
150508 KB |
Output is correct |
42 |
Correct |
2473 ms |
160708 KB |
Output is correct |
43 |
Correct |
923 ms |
152300 KB |
Output is correct |
44 |
Correct |
907 ms |
152044 KB |
Output is correct |
45 |
Correct |
818 ms |
151916 KB |
Output is correct |
46 |
Correct |
915 ms |
152172 KB |
Output is correct |
47 |
Correct |
1109 ms |
159776 KB |
Output is correct |
48 |
Correct |
1 ms |
620 KB |
Output is correct |
49 |
Correct |
1 ms |
620 KB |
Output is correct |
50 |
Correct |
1 ms |
620 KB |
Output is correct |
51 |
Correct |
1 ms |
620 KB |
Output is correct |
52 |
Correct |
1 ms |
620 KB |
Output is correct |
53 |
Correct |
3 ms |
748 KB |
Output is correct |
54 |
Correct |
21 ms |
1516 KB |
Output is correct |
55 |
Correct |
23 ms |
1516 KB |
Output is correct |
56 |
Correct |
22 ms |
1516 KB |
Output is correct |
57 |
Correct |
24 ms |
1516 KB |
Output is correct |
58 |
Correct |
22 ms |
1516 KB |
Output is correct |
59 |
Correct |
22 ms |
1644 KB |
Output is correct |
60 |
Correct |
24 ms |
1516 KB |
Output is correct |
61 |
Correct |
101 ms |
6636 KB |
Output is correct |
62 |
Correct |
232 ms |
20204 KB |
Output is correct |
63 |
Correct |
681 ms |
152044 KB |
Output is correct |
64 |
Correct |
872 ms |
152232 KB |
Output is correct |
65 |
Correct |
1299 ms |
152684 KB |
Output is correct |
66 |
Correct |
1683 ms |
153836 KB |
Output is correct |
67 |
Correct |
2444 ms |
164104 KB |
Output is correct |
68 |
Correct |
847 ms |
152684 KB |
Output is correct |
69 |
Correct |
1364 ms |
162924 KB |
Output is correct |
70 |
Correct |
825 ms |
156012 KB |
Output is correct |
71 |
Correct |
962 ms |
152556 KB |
Output is correct |
72 |
Correct |
1363 ms |
152556 KB |
Output is correct |
73 |
Correct |
1693 ms |
153908 KB |
Output is correct |
74 |
Correct |
713 ms |
152044 KB |
Output is correct |
75 |
Correct |
907 ms |
152300 KB |
Output is correct |