#include <bits/stdc++.h>
#include "plants.h"
using namespace std;
int curVal, n;
vector<int> p;
vector<long long> Left[18], Right[18];
struct SegmentTree{
int n;
vector<int> d, lazy;
void build(int id, int l, int r, const vector<int> &a){
if (l == r){
d[id] = a[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid, a);
build(id << 1 | 1, mid + 1, r, a);
d[id] = min(d[id << 1], d[id << 1 | 1]);
}
void init(const vector<int> &a){
n = a.size();
d.assign(n * 4 + 1, 0);
lazy.assign(n * 4 + 1, 0);
build(1, 0, n - 1, a);
}
void doLazy(int id, int l, int r){
d[id] += lazy[id];
if (l < r){
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
lazy[id] = 0;
return;
}
void update(int id, int l, int r, int u, int v, int val){
doLazy(id, l, r);
if (v < l || r < u)
return;
if (u <= l && r <= v){
lazy[id] += val;
doLazy(id, l, r);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
d[id] = min(d[id << 1], d[id << 1 | 1]);
}
int getMin(int id, int l, int r, int u, int v){
doLazy(id, l, r);
if (v < l || r < u)
return 1e9;
if (u <= l && r <= v)
return d[id];
int mid = (l + r) >> 1;
return min(getMin(id << 1, l, mid, u, v), getMin(id << 1 | 1, mid + 1, r, u, v));
}
int getMin(int l, int r){
if (l <= r)
return getMin(1, 0, n - 1, l, r);
else
return min(getMin(1, 0, n - 1, l, n - 1), getMin(1, 0, n - 1, 0, r));
}
void update(int l, int r, int val){
if (l <= r){
update(1, 0, n - 1, l, r, val);
} else {
update(1, 0, n - 1, l, n - 1, val);
update(1, 0, n - 1, 0, r, val);
}
}
int findPos0(int id, int l, int r, int u, int v){
doLazy(id, l, r);
if (v < l || r < u || d[id] > 0)
return -1;
if (l == r)
return l;
int mid = (l + r) >> 1;
int res = findPos0(id << 1, l, mid, u, v);
if (res == -1)
res = findPos0(id << 1 | 1, mid + 1, r, u, v);
return res;
}
int findPos0(int l, int r){
if (l <= r)
return findPos0(1, 0, n - 1, l, r);
int res = findPos0(1, 0, n - 1, l, n - 1);
if (res == -1)
res = findPos0(1, 0, n - 1, 0, r);
return res;
}
} st;
void process(int pos, int n, int k){
while (true){
int nxt = st.findPos0((pos - k + 1 + n) % n, (pos - 1 + n) % n);
if (nxt == -1)
break;
else
process(nxt, n, k);
}
p[pos] = curVal--;
st.update(pos, pos, 1e9);
st.update((pos - k + 1 + n) % n, (pos - 1 + n) % n, -1);
}
int dist(int x, int y, int n){
if (x <= y)
return y - x;
return n - x + y;
}
void init(int k, vector<int> r) {
n = r.size();
p.assign(n, -1);
st.init(r);
curVal = n - 1;
while (curVal >= 0){
int pos = st.findPos0(0, n - 1);
assert(pos != -1);
process(pos, n, k);
}
for(int i = 0; i < 18; i++){
Left[i].assign(n, -1);
Right[i].assign(n, -1);
}
vector<int> ord(n);
for(int i = 0; i < n; i++)
ord[p[i]] = i;
st.init(vector<int>(n, 1e9));
for(int i = 0; i < n; i++){
int p = ord[i];
int x = -st.getMin((p - k + 1 + n) % n, (p - 1 + n) % n);
Left[0][p] = (x >= 0? ord[x] : p);
Left[0][p] = dist(Left[0][p], p, n);
x = -st.getMin((p + 1) % n, (p + k - 1) % n);
Right[0][p] = (x >= 0? ord[x] : p);
Right[0][p] = dist(p, Right[0][p], n);
st.update(p, p, -1e9 - i);
}
for(int i = 1; i <= 17; i++)
for(int u = 0; u < n; u++){
Left[i][u] = Left[i - 1][u] + Left[i - 1][(u - Left[i - 1][u] % n + n) % n];
Right[i][u] = Right[i - 1][u] + Right[i - 1][(u + Right[i - 1][u]) % n];
}
return;
}
int compare_plants(int x, int y) {
int u;
u = x;
for(int i = 17; i >= 0; i--)
if (dist(u, x, n) + Left[i][u] < dist(y, x, n))
u = (u - Left[i][u] + n) % n;
if (Left[0][u] > 0 && p[u] > p[y])
return 1;
u = x;
for(int i = 17; i >= 0; i--)
if (dist(x, u, n) + Right[i][u] < dist(x, y, n))
u = (u + Right[i][u] + n) % n;
if (Right[0][u] > 0 && p[u] > p[y])
return 1;
u = y;
for(int i = 17; i >= 0; i--)
if (dist(u, y, n) + Left[i][u] < dist(x, y, n))
u = (u - Left[i][u] + n) % n;
if (Left[0][u] > 0 && p[u] > p[x])
return -1;
u = y;
for(int i = 17; i >= 0; i--)
if (dist(y, u, n) + Right[i][u] < dist(y, x, n))
u = (u + Right[i][u]) % n;
if (Right[0][u] > 0 && p[u] > p[x])
return -1;
return 0;
}
Compilation message
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:145:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
145 | for(int i = 1; i <= 17; i++)
| ^~~
plants.cpp:151:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
151 | return;
| ^~~~~~
plants.cpp: In function 'int compare_plants(int, int)':
plants.cpp:178:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
178 | if (Right[0][u] > 0 && p[u] > p[x])
| ^~
plants.cpp:180:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
180 | return 0;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
136 ms |
3028 KB |
Output is correct |
7 |
Correct |
377 ms |
9800 KB |
Output is correct |
8 |
Correct |
1613 ms |
69316 KB |
Output is correct |
9 |
Correct |
1553 ms |
69252 KB |
Output is correct |
10 |
Correct |
1509 ms |
69244 KB |
Output is correct |
11 |
Correct |
1469 ms |
69252 KB |
Output is correct |
12 |
Correct |
1462 ms |
69252 KB |
Output is correct |
13 |
Correct |
1346 ms |
69248 KB |
Output is correct |
14 |
Correct |
1445 ms |
69256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
5 ms |
588 KB |
Output is correct |
7 |
Correct |
109 ms |
4688 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
5 ms |
588 KB |
Output is correct |
10 |
Correct |
108 ms |
4904 KB |
Output is correct |
11 |
Correct |
156 ms |
4688 KB |
Output is correct |
12 |
Correct |
203 ms |
4844 KB |
Output is correct |
13 |
Correct |
108 ms |
4720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
5 ms |
588 KB |
Output is correct |
7 |
Correct |
109 ms |
4688 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
5 ms |
588 KB |
Output is correct |
10 |
Correct |
108 ms |
4904 KB |
Output is correct |
11 |
Correct |
156 ms |
4688 KB |
Output is correct |
12 |
Correct |
203 ms |
4844 KB |
Output is correct |
13 |
Correct |
108 ms |
4720 KB |
Output is correct |
14 |
Correct |
209 ms |
9896 KB |
Output is correct |
15 |
Correct |
1338 ms |
69252 KB |
Output is correct |
16 |
Correct |
217 ms |
10064 KB |
Output is correct |
17 |
Correct |
1310 ms |
69444 KB |
Output is correct |
18 |
Correct |
1457 ms |
69520 KB |
Output is correct |
19 |
Correct |
1557 ms |
69620 KB |
Output is correct |
20 |
Correct |
1309 ms |
69520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
137 ms |
3688 KB |
Output is correct |
4 |
Correct |
1304 ms |
70812 KB |
Output is correct |
5 |
Correct |
1385 ms |
69284 KB |
Output is correct |
6 |
Correct |
1446 ms |
69224 KB |
Output is correct |
7 |
Correct |
1391 ms |
69312 KB |
Output is correct |
8 |
Correct |
1349 ms |
69232 KB |
Output is correct |
9 |
Correct |
1433 ms |
69620 KB |
Output is correct |
10 |
Correct |
1420 ms |
69444 KB |
Output is correct |
11 |
Correct |
1387 ms |
69544 KB |
Output is correct |
12 |
Correct |
1512 ms |
69524 KB |
Output is correct |
13 |
Correct |
1602 ms |
69524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
5 ms |
380 KB |
Output is correct |
7 |
Correct |
34 ms |
928 KB |
Output is correct |
8 |
Correct |
20 ms |
1008 KB |
Output is correct |
9 |
Correct |
28 ms |
964 KB |
Output is correct |
10 |
Correct |
23 ms |
960 KB |
Output is correct |
11 |
Correct |
40 ms |
948 KB |
Output is correct |
12 |
Correct |
30 ms |
928 KB |
Output is correct |
13 |
Correct |
18 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
3 ms |
588 KB |
Output is correct |
6 |
Correct |
1261 ms |
69248 KB |
Output is correct |
7 |
Correct |
1225 ms |
69232 KB |
Output is correct |
8 |
Correct |
1162 ms |
69256 KB |
Output is correct |
9 |
Correct |
1415 ms |
69252 KB |
Output is correct |
10 |
Correct |
1394 ms |
69368 KB |
Output is correct |
11 |
Correct |
1140 ms |
72024 KB |
Output is correct |
12 |
Correct |
1093 ms |
73248 KB |
Output is correct |
13 |
Correct |
1197 ms |
71632 KB |
Output is correct |
14 |
Correct |
1061 ms |
71652 KB |
Output is correct |
15 |
Correct |
1255 ms |
71872 KB |
Output is correct |
16 |
Correct |
980 ms |
71340 KB |
Output is correct |
17 |
Correct |
1080 ms |
71492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
136 ms |
3028 KB |
Output is correct |
7 |
Correct |
377 ms |
9800 KB |
Output is correct |
8 |
Correct |
1613 ms |
69316 KB |
Output is correct |
9 |
Correct |
1553 ms |
69252 KB |
Output is correct |
10 |
Correct |
1509 ms |
69244 KB |
Output is correct |
11 |
Correct |
1469 ms |
69252 KB |
Output is correct |
12 |
Correct |
1462 ms |
69252 KB |
Output is correct |
13 |
Correct |
1346 ms |
69248 KB |
Output is correct |
14 |
Correct |
1445 ms |
69256 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
5 ms |
588 KB |
Output is correct |
21 |
Correct |
109 ms |
4688 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
5 ms |
588 KB |
Output is correct |
24 |
Correct |
108 ms |
4904 KB |
Output is correct |
25 |
Correct |
156 ms |
4688 KB |
Output is correct |
26 |
Correct |
203 ms |
4844 KB |
Output is correct |
27 |
Correct |
108 ms |
4720 KB |
Output is correct |
28 |
Correct |
209 ms |
9896 KB |
Output is correct |
29 |
Correct |
1338 ms |
69252 KB |
Output is correct |
30 |
Correct |
217 ms |
10064 KB |
Output is correct |
31 |
Correct |
1310 ms |
69444 KB |
Output is correct |
32 |
Correct |
1457 ms |
69520 KB |
Output is correct |
33 |
Correct |
1557 ms |
69620 KB |
Output is correct |
34 |
Correct |
1309 ms |
69520 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
137 ms |
3688 KB |
Output is correct |
38 |
Correct |
1304 ms |
70812 KB |
Output is correct |
39 |
Correct |
1385 ms |
69284 KB |
Output is correct |
40 |
Correct |
1446 ms |
69224 KB |
Output is correct |
41 |
Correct |
1391 ms |
69312 KB |
Output is correct |
42 |
Correct |
1349 ms |
69232 KB |
Output is correct |
43 |
Correct |
1433 ms |
69620 KB |
Output is correct |
44 |
Correct |
1420 ms |
69444 KB |
Output is correct |
45 |
Correct |
1387 ms |
69544 KB |
Output is correct |
46 |
Correct |
1512 ms |
69524 KB |
Output is correct |
47 |
Correct |
1602 ms |
69524 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
5 ms |
380 KB |
Output is correct |
54 |
Correct |
34 ms |
928 KB |
Output is correct |
55 |
Correct |
20 ms |
1008 KB |
Output is correct |
56 |
Correct |
28 ms |
964 KB |
Output is correct |
57 |
Correct |
23 ms |
960 KB |
Output is correct |
58 |
Correct |
40 ms |
948 KB |
Output is correct |
59 |
Correct |
30 ms |
928 KB |
Output is correct |
60 |
Correct |
18 ms |
972 KB |
Output is correct |
61 |
Correct |
238 ms |
5456 KB |
Output is correct |
62 |
Correct |
535 ms |
11968 KB |
Output is correct |
63 |
Correct |
2136 ms |
72240 KB |
Output is correct |
64 |
Correct |
1446 ms |
72292 KB |
Output is correct |
65 |
Correct |
1459 ms |
72532 KB |
Output is correct |
66 |
Correct |
1419 ms |
72632 KB |
Output is correct |
67 |
Correct |
1329 ms |
72924 KB |
Output is correct |
68 |
Correct |
1640 ms |
72388 KB |
Output is correct |
69 |
Correct |
1322 ms |
72780 KB |
Output is correct |
70 |
Correct |
1461 ms |
73944 KB |
Output is correct |
71 |
Correct |
1623 ms |
72380 KB |
Output is correct |
72 |
Correct |
1436 ms |
72544 KB |
Output is correct |
73 |
Correct |
1326 ms |
72628 KB |
Output is correct |
74 |
Correct |
1963 ms |
72232 KB |
Output is correct |
75 |
Correct |
1272 ms |
72288 KB |
Output is correct |