#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N, K;
vector<int> R, A, pfs;
int dis(int x, int y) {
if (x < y) {
return y - x;
} else {
return N - x + y;
}
}
void init(int k, std::vector<int> r) {
K = k, R = r; N = R.size();
if (K == 2) {
pfs.resize(N + 1);
for (int i = 0; i < N; i++) {
pfs[i + 1] = pfs[i] + R[i];
}
return;
}
A.resize(N, -1);
int M = 2 << __lg(N - 1);
vector<int> st(2 * M - 1), lazy(2 * M - 1);
auto push = [&](int p) {
st[p * 2 + 1] += lazy[p];
st[p * 2 + 2] += lazy[p];
lazy[p * 2 + 1] += lazy[p];
lazy[p * 2 + 2] += lazy[p];
lazy[p] = 0;
};
auto pull = [&](int p) {
st[p] = max(st[p * 2 + 1], st[p * 2 + 2]);
};
for (int i = 0; i < N; i++) {
st[i + M - 1] = R[i];
}
for (int i = M - 2; i >= 0; i--) {
pull(i);
}
auto modify = [&](auto f, int p, int l, int r, int x, int v) -> void {
if (r - l == 1) {
st[p] = v;
return;
}
int m = (l + r + 1) / 2;
push(p);
if (x < m) {
f(f, p * 2 + 1, l, m, x, v);
} else {
f(f, p * 2 + 2, m, r, x, v);
}
pull(p);
};
auto update = [&](auto f, int p, int l, int r, int L, int R) -> void {
if (R <= l || r <= L) {
return;
}
if (L <= l && r <= R) {
st[p]++;
lazy[p]++;
return;
}
int m = (l + r + 1) / 2;
push(p);
f(f, p * 2 + 1, l, m, L, R);
f(f, p * 2 + 2, m, r, L, R);
pull(p);
};
auto find = [&](auto f, int p, int l, int r) {
if (r - l == 1) {
return l;
}
int m = (l + r + 1) / 2;
push(p);
if (st[p * 2 + 1] == K - 1) {
return f(f, p * 2 + 1, l, m);
} else {
return f(f, p * 2 + 2, m, r);
}
};
set<pair<int, int>> s;
set<int> cand;
vector<int> d(N);
for (int i = 0; i < N; i++) {
while (st[0] == K - 1) {
int x = find(find, 0, 0, M);
modify(modify, 0, 0, M, x, -1e9);
if (s.empty()) {
s.emplace(d[x] = 1e9, x);
cand.insert(x);
} else {
auto it = cand.lower_bound(x);
int l, r;
if (it == cand.end()) {
r = *cand.begin();
l = *prev(it);
} else {
r = *it;
if (it == cand.begin()) {
l = *prev(cand.end());
} else {
l = *prev(it);
}
}
s.erase(s.find({d[r], r}));
s.emplace(d[x] = dis(l, x), x);
s.emplace(d[r] = dis(x, r), r);
cand.insert(x);
}
}
auto [t, x] = *prev(s.end());
s.erase(prev(s.end()));
cand.erase(x);
if (!cand.empty()) {
auto it = cand.lower_bound(x);
int y;
if (it == cand.end()) {
y = *cand.begin();
} else {
y = *it;
}
s.erase({d[y], y});
s.emplace(d[y] += d[x], y);
}
A[x] = i;
if (x > 0) {
update(update, 0, 0, M, max(0, x - (K - 1)), x);
}
if (x - (K - 1) < 0) {
update(update, 0, 0, M, N + x - (K - 1), N);
}
}
}
int compare_plants(int x, int y) {
if (K == 2) {
if (pfs[y] - pfs[x] == 0) {
return 1;
}
if (pfs[y] - pfs[x] == y - x) {
return -1;
}
if (pfs[N] - pfs[y] + pfs[x] == 0) {
return -1;
}
if (pfs[N] - pfs[y] + pfs[x] == N - y + x) {
return 1;
}
return 0;
}
return A[x] > A[y] ? 1 : -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
42 ms |
4048 KB |
Output is correct |
7 |
Correct |
43 ms |
5528 KB |
Output is correct |
8 |
Correct |
57 ms |
8396 KB |
Output is correct |
9 |
Correct |
56 ms |
8480 KB |
Output is correct |
10 |
Correct |
56 ms |
8396 KB |
Output is correct |
11 |
Correct |
57 ms |
8528 KB |
Output is correct |
12 |
Correct |
56 ms |
8388 KB |
Output is correct |
13 |
Correct |
61 ms |
8404 KB |
Output is correct |
14 |
Correct |
57 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
2 ms |
444 KB |
Output is correct |
7 |
Correct |
43 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
44 ms |
3368 KB |
Output is correct |
11 |
Correct |
42 ms |
3288 KB |
Output is correct |
12 |
Correct |
44 ms |
3728 KB |
Output is correct |
13 |
Correct |
43 ms |
3396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
2 ms |
444 KB |
Output is correct |
7 |
Correct |
43 ms |
3420 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
44 ms |
3368 KB |
Output is correct |
11 |
Correct |
42 ms |
3288 KB |
Output is correct |
12 |
Correct |
44 ms |
3728 KB |
Output is correct |
13 |
Correct |
43 ms |
3396 KB |
Output is correct |
14 |
Correct |
66 ms |
3620 KB |
Output is correct |
15 |
Correct |
333 ms |
10948 KB |
Output is correct |
16 |
Correct |
62 ms |
3816 KB |
Output is correct |
17 |
Correct |
294 ms |
10936 KB |
Output is correct |
18 |
Correct |
242 ms |
10984 KB |
Output is correct |
19 |
Correct |
395 ms |
20332 KB |
Output is correct |
20 |
Correct |
282 ms |
14404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
48 ms |
3436 KB |
Output is correct |
4 |
Correct |
265 ms |
17316 KB |
Output is correct |
5 |
Correct |
276 ms |
15068 KB |
Output is correct |
6 |
Correct |
294 ms |
14028 KB |
Output is correct |
7 |
Correct |
336 ms |
14304 KB |
Output is correct |
8 |
Correct |
296 ms |
14360 KB |
Output is correct |
9 |
Correct |
238 ms |
14180 KB |
Output is correct |
10 |
Correct |
301 ms |
15676 KB |
Output is correct |
11 |
Correct |
54 ms |
8668 KB |
Output is correct |
12 |
Correct |
372 ms |
32576 KB |
Output is correct |
13 |
Correct |
228 ms |
13772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
42 ms |
4048 KB |
Output is correct |
7 |
Correct |
43 ms |
5528 KB |
Output is correct |
8 |
Correct |
57 ms |
8396 KB |
Output is correct |
9 |
Correct |
56 ms |
8480 KB |
Output is correct |
10 |
Correct |
56 ms |
8396 KB |
Output is correct |
11 |
Correct |
57 ms |
8528 KB |
Output is correct |
12 |
Correct |
56 ms |
8388 KB |
Output is correct |
13 |
Correct |
61 ms |
8404 KB |
Output is correct |
14 |
Correct |
57 ms |
8404 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
2 ms |
444 KB |
Output is correct |
21 |
Correct |
43 ms |
3420 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
440 KB |
Output is correct |
24 |
Correct |
44 ms |
3368 KB |
Output is correct |
25 |
Correct |
42 ms |
3288 KB |
Output is correct |
26 |
Correct |
44 ms |
3728 KB |
Output is correct |
27 |
Correct |
43 ms |
3396 KB |
Output is correct |
28 |
Correct |
66 ms |
3620 KB |
Output is correct |
29 |
Correct |
333 ms |
10948 KB |
Output is correct |
30 |
Correct |
62 ms |
3816 KB |
Output is correct |
31 |
Correct |
294 ms |
10936 KB |
Output is correct |
32 |
Correct |
242 ms |
10984 KB |
Output is correct |
33 |
Correct |
395 ms |
20332 KB |
Output is correct |
34 |
Correct |
282 ms |
14404 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
296 KB |
Output is correct |
37 |
Correct |
48 ms |
3436 KB |
Output is correct |
38 |
Correct |
265 ms |
17316 KB |
Output is correct |
39 |
Correct |
276 ms |
15068 KB |
Output is correct |
40 |
Correct |
294 ms |
14028 KB |
Output is correct |
41 |
Correct |
336 ms |
14304 KB |
Output is correct |
42 |
Correct |
296 ms |
14360 KB |
Output is correct |
43 |
Correct |
238 ms |
14180 KB |
Output is correct |
44 |
Correct |
301 ms |
15676 KB |
Output is correct |
45 |
Correct |
54 ms |
8668 KB |
Output is correct |
46 |
Correct |
372 ms |
32576 KB |
Output is correct |
47 |
Correct |
228 ms |
13772 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
300 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |