#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int mod(int a, int n) {
if ((a %= n) < 0) a += n;
return a;
}
int n, k;
int dist(int i, int j) {
return mod(j - i - 1, n) + 1;
}
struct MinTree {
int i, j;
int m, a = 0;
MinTree *l, *r;
MinTree(const vector<int>& v, int i, int j): i(i), j(j) {
if (j - i == 1) {
m = v[i];
l = r = nullptr;
} else {
int k = i + j >> 1;
l = new MinTree(v, i, k);
r = new MinTree(v, k, j);
m = min(l->m, r->m);
}
}
void visit() {
if (a) {
m += a;
if (l) l->a += a, r->a += a;
a = 0;
}
}
void inc(int I, int J, int A) {
if (I <= i && j <= J) {
a += A;
visit();
} else {
visit();
if (!(J <= i || j <= I)) {
l->inc(I, J, A);
r->inc(I, J, A);
m = min(l->m, r->m);
}
}
}
void get0s(int I, int J, vector<int>& t) {
visit();
if (m > 0 || J <= i || j <= I) return;
if (l) {
l->get0s(I, J, t);
r->get0s(I, J, t);
} else {
t.push_back(i);
}
}
void rinc(int I, int J, int A) {
while (I < j) {
I += j - i;
J += j - i;
}
while (i < I) {
I -= j - i;
J -= j - i;
inc(I, J, A);
}
}
void rget0s(int I, int J, vector<int>& t) {
while (I < j) {
I += j - i;
J += j - i;
}
while (i < I) {
I -= j - i;
J -= j - i;
get0s(I, J, t);
}
}
};
struct MaxTree {
int i, j;
pair<int,int> m;
MaxTree *l, *r;
MaxTree(int v, int i, int j): i(i), j(j), m(v, i) {
if (j - i == 1) {
l = r = nullptr;
} else {
int k = i + j >> 1;
l = new MaxTree(v, i, k);
r = new MaxTree(v, k, j);
}
}
void write(int I, int v) {
if (i <= I && I < j) {
if (l) {
l->write(I, v);
r->write(I, v);
m = std::max(l->m, r->m);
} else {
m = {v, i};
}
}
}
pair<int,int> max(int I, int J) {
if (I <= i && j <= J) {
return m;
} else if (J <= i || j <= I) {
return {-2, -2};
} else {
return std::max(l->max(I, J), r->max(I, J));
}
}
pair<int,int> rmax(int I, int J) {
while (I < j) {
I += j - i;
J += j - i;
}
pair<int,int> ans(-2, -2);
while (i < I) {
I -= j - i;
J -= j - i;
ans = std::max(ans, max(I, J));
}
return ans;
}
};
struct TreeTree {
int k;
vector<vector<int>> anc;
vector<vector<ll>> ancd;
TreeTree() {}
TreeTree(const vector<int>& par, const vector<ll>& pard) {
int n = par.size();
for (k = 0; 1 << k <= n; k++);
anc = vector<vector<int>>(k, vector<int>(n, -1));
ancd = vector<vector<ll>>(k, vector<ll>(n, -1));
anc[0] = par;
ancd[0] = pard;
for (int kk = 0; kk < k - 1; kk++) {
for (int i = 0; i < n; i++) {
int j = anc[kk][i];
anc[kk + 1][i] = anc[kk][j];
ancd[kk + 1][i] = ancd[kk][i] + ancd[kk][j];
}
}
}
pair<int,ll> walk_up(int i, ll d) {
for (int kk = k; kk--;) {
if (d >= ancd[kk][i]) {
d -= ancd[kk][i];
i = anc[kk][i];
}
}
return {i, d};
}
};
vector<int> vals;
TreeTree rttree, lftree;
void init(int k_, vector<int> r) {
k = k_;
n = r.size();
vals = vector<int>(n);
MinTree curr(r, 0, n);
vector<int> likod(n), harap(n);
unordered_set<int> oks;
vector<int> cands;
for (int i = 0; i < n; i++) if (!r[i]) cands.push_back(i);
for (int x2 = 0, x1 = cands.size() - 1; x2 < cands.size(); x1 = x2++) {
int j1 = cands[x1], j2 = cands[x2];
harap[j1] = j2;
likod[j2] = j1;
if (dist(j1, j2) >= k) oks.insert(j2);
}
for (int v = n; v; v--) {
int j = *oks.begin();
int j1 = likod[j], j2 = harap[j];
oks.erase(j);
vals[j] = v;
curr.inc(j, j + 1, n + 1);
curr.rinc(j - k + 1, j, -1);
vector<int> bagos;
curr.rget0s(j - k + 1, j, bagos);
if (v > 1) {
if (j == j1 && j == j2) {
j1 = bagos.back();
j2 = bagos.front();
}
bagos.insert(bagos.begin(), j1);
bagos.push_back(j2);
for (int x1 = 0, x2 = 1; x2 < bagos.size(); x1++, x2++) {
int j1 = bagos[x1], j2 = bagos[x2];
harap[j1] = j2;
likod[j2] = j1;
if (dist(j1, j2) >= k) {
oks.insert(j2);
} else if (oks.count(j2)) {
oks.erase(j2);
}
}
}
}
vector<int> lfs(n, -1), rts(n, -1);
MaxTree wals(-1, 0, n);
vector<int> inds(n);
iota(inds.begin(), inds.end(), 0);
sort(inds.begin(), inds.end(), [&](int i, int j) { return vals[i] < vals[j]; });
for (int x : inds) {
wals.write(x, 0);
int m;
tie(m, lfs[x]) = wals.rmax(x - k + 1, x + 1);
tie(m, rts[x]) = wals.rmax(x, x + k);
wals.write(x, vals[x]);
}
vector<ll> lfdists(n), rtdists(n);
for (int i = 0; i < n; i++) {
lfdists[i] = dist(lfs[i], i);
rtdists[i] = dist(i, rts[i]);
}
lftree = TreeTree(lfs, lfdists);
rttree = TreeTree(rts, rtdists);
}
bool is_fixed_right(int x, int y) {
auto [X, rem] = rttree.walk_up(x, dist(x, y));
return rem < k && vals[X] >= vals[y];
}
bool is_fixed_left(int x, int y) {
auto [X, rem] = lftree.walk_up(x, dist(y, x));
return rem < k && vals[X] >= vals[y];
}
int compare_plants(int x, int y) {
int mul = 1;
if (vals[x] < vals[y]) {
swap(x, y);
mul = -1;
}
return (is_fixed_right(x, y) || is_fixed_left(x, y)) * mul;
}
Compilation message
plants.cpp: In constructor 'MinTree::MinTree(const std::vector<int>&, int, int)':
plants.cpp:25:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int k = i + j >> 1;
| ~~^~~
plants.cpp: In constructor 'MaxTree::MaxTree(int, int, int)':
plants.cpp:98:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
98 | int k = i + j >> 1;
| ~~^~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:186:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
186 | for (int x2 = 0, x1 = cands.size() - 1; x2 < cands.size(); x1 = x2++) {
| ~~~^~~~~~~~~~~~~~
plants.cpp:212:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
212 | for (int x1 = 0, x2 = 1; x2 < bagos.size(); x1++, x2++) {
| ~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
70 ms |
3192 KB |
Output is correct |
7 |
Correct |
196 ms |
15108 KB |
Output is correct |
8 |
Correct |
823 ms |
137156 KB |
Output is correct |
9 |
Correct |
833 ms |
137096 KB |
Output is correct |
10 |
Correct |
868 ms |
137092 KB |
Output is correct |
11 |
Correct |
874 ms |
137028 KB |
Output is correct |
12 |
Correct |
864 ms |
137028 KB |
Output is correct |
13 |
Correct |
903 ms |
137536 KB |
Output is correct |
14 |
Correct |
1010 ms |
136652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
97 ms |
6056 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
99 ms |
6008 KB |
Output is correct |
11 |
Correct |
120 ms |
6008 KB |
Output is correct |
12 |
Correct |
119 ms |
6008 KB |
Output is correct |
13 |
Correct |
108 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
97 ms |
6056 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
896 KB |
Output is correct |
10 |
Correct |
99 ms |
6008 KB |
Output is correct |
11 |
Correct |
120 ms |
6008 KB |
Output is correct |
12 |
Correct |
119 ms |
6008 KB |
Output is correct |
13 |
Correct |
108 ms |
5880 KB |
Output is correct |
14 |
Correct |
176 ms |
15128 KB |
Output is correct |
15 |
Correct |
1577 ms |
136708 KB |
Output is correct |
16 |
Correct |
171 ms |
15000 KB |
Output is correct |
17 |
Correct |
1565 ms |
136652 KB |
Output is correct |
18 |
Correct |
1038 ms |
137028 KB |
Output is correct |
19 |
Correct |
1043 ms |
136752 KB |
Output is correct |
20 |
Correct |
1265 ms |
136720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
104 ms |
4216 KB |
Output is correct |
4 |
Correct |
1047 ms |
136900 KB |
Output is correct |
5 |
Correct |
1162 ms |
136752 KB |
Output is correct |
6 |
Correct |
1432 ms |
136780 KB |
Output is correct |
7 |
Correct |
1544 ms |
136740 KB |
Output is correct |
8 |
Correct |
1567 ms |
136840 KB |
Output is correct |
9 |
Correct |
979 ms |
136904 KB |
Output is correct |
10 |
Correct |
1087 ms |
136780 KB |
Output is correct |
11 |
Correct |
914 ms |
137408 KB |
Output is correct |
12 |
Correct |
1073 ms |
136780 KB |
Output is correct |
13 |
Correct |
1035 ms |
137156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
19 ms |
1024 KB |
Output is correct |
8 |
Correct |
17 ms |
1152 KB |
Output is correct |
9 |
Correct |
21 ms |
1024 KB |
Output is correct |
10 |
Correct |
18 ms |
1152 KB |
Output is correct |
11 |
Correct |
19 ms |
1024 KB |
Output is correct |
12 |
Correct |
20 ms |
1152 KB |
Output is correct |
13 |
Correct |
17 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
3 ms |
768 KB |
Output is correct |
6 |
Correct |
924 ms |
136780 KB |
Output is correct |
7 |
Correct |
1211 ms |
136780 KB |
Output is correct |
8 |
Correct |
1367 ms |
136712 KB |
Output is correct |
9 |
Correct |
1561 ms |
136812 KB |
Output is correct |
10 |
Correct |
728 ms |
137032 KB |
Output is correct |
11 |
Correct |
1104 ms |
136908 KB |
Output is correct |
12 |
Correct |
827 ms |
136900 KB |
Output is correct |
13 |
Correct |
866 ms |
136780 KB |
Output is correct |
14 |
Correct |
1186 ms |
136652 KB |
Output is correct |
15 |
Correct |
1456 ms |
136780 KB |
Output is correct |
16 |
Correct |
862 ms |
136940 KB |
Output is correct |
17 |
Correct |
818 ms |
136780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
70 ms |
3192 KB |
Output is correct |
7 |
Correct |
196 ms |
15108 KB |
Output is correct |
8 |
Correct |
823 ms |
137156 KB |
Output is correct |
9 |
Correct |
833 ms |
137096 KB |
Output is correct |
10 |
Correct |
868 ms |
137092 KB |
Output is correct |
11 |
Correct |
874 ms |
137028 KB |
Output is correct |
12 |
Correct |
864 ms |
137028 KB |
Output is correct |
13 |
Correct |
903 ms |
137536 KB |
Output is correct |
14 |
Correct |
1010 ms |
136652 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
896 KB |
Output is correct |
21 |
Correct |
97 ms |
6056 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
896 KB |
Output is correct |
24 |
Correct |
99 ms |
6008 KB |
Output is correct |
25 |
Correct |
120 ms |
6008 KB |
Output is correct |
26 |
Correct |
119 ms |
6008 KB |
Output is correct |
27 |
Correct |
108 ms |
5880 KB |
Output is correct |
28 |
Correct |
176 ms |
15128 KB |
Output is correct |
29 |
Correct |
1577 ms |
136708 KB |
Output is correct |
30 |
Correct |
171 ms |
15000 KB |
Output is correct |
31 |
Correct |
1565 ms |
136652 KB |
Output is correct |
32 |
Correct |
1038 ms |
137028 KB |
Output is correct |
33 |
Correct |
1043 ms |
136752 KB |
Output is correct |
34 |
Correct |
1265 ms |
136720 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
256 KB |
Output is correct |
37 |
Correct |
104 ms |
4216 KB |
Output is correct |
38 |
Correct |
1047 ms |
136900 KB |
Output is correct |
39 |
Correct |
1162 ms |
136752 KB |
Output is correct |
40 |
Correct |
1432 ms |
136780 KB |
Output is correct |
41 |
Correct |
1544 ms |
136740 KB |
Output is correct |
42 |
Correct |
1567 ms |
136840 KB |
Output is correct |
43 |
Correct |
979 ms |
136904 KB |
Output is correct |
44 |
Correct |
1087 ms |
136780 KB |
Output is correct |
45 |
Correct |
914 ms |
137408 KB |
Output is correct |
46 |
Correct |
1073 ms |
136780 KB |
Output is correct |
47 |
Correct |
1035 ms |
137156 KB |
Output is correct |
48 |
Correct |
1 ms |
256 KB |
Output is correct |
49 |
Correct |
1 ms |
256 KB |
Output is correct |
50 |
Correct |
1 ms |
256 KB |
Output is correct |
51 |
Correct |
1 ms |
256 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
3 ms |
384 KB |
Output is correct |
54 |
Correct |
19 ms |
1024 KB |
Output is correct |
55 |
Correct |
17 ms |
1152 KB |
Output is correct |
56 |
Correct |
21 ms |
1024 KB |
Output is correct |
57 |
Correct |
18 ms |
1152 KB |
Output is correct |
58 |
Correct |
19 ms |
1024 KB |
Output is correct |
59 |
Correct |
20 ms |
1152 KB |
Output is correct |
60 |
Correct |
17 ms |
1152 KB |
Output is correct |
61 |
Correct |
100 ms |
4216 KB |
Output is correct |
62 |
Correct |
192 ms |
15000 KB |
Output is correct |
63 |
Correct |
931 ms |
136908 KB |
Output is correct |
64 |
Correct |
1078 ms |
136908 KB |
Output is correct |
65 |
Correct |
1397 ms |
136908 KB |
Output is correct |
66 |
Correct |
1516 ms |
136816 KB |
Output is correct |
67 |
Correct |
1593 ms |
136780 KB |
Output is correct |
68 |
Correct |
968 ms |
136904 KB |
Output is correct |
69 |
Correct |
1313 ms |
136852 KB |
Output is correct |
70 |
Correct |
1020 ms |
136900 KB |
Output is correct |
71 |
Correct |
1221 ms |
136780 KB |
Output is correct |
72 |
Correct |
1464 ms |
136648 KB |
Output is correct |
73 |
Correct |
1565 ms |
136780 KB |
Output is correct |
74 |
Correct |
931 ms |
136908 KB |
Output is correct |
75 |
Correct |
1066 ms |
136780 KB |
Output is correct |