#include "plants.h"
#ifdef NYAOWO
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 200'000;
const int MAXLG = 20;
int n, k;
int dist(int s, int t) {
if(s <= t) return t - s;
else return t - s + n;
}
struct Ayaya {
set<int> pos;
set<pii> d;
pii find(int x) {
auto itr = pos.upper_bound(x);
if(itr == pos.end()) {
itr = pos.begin();
}
auto itl = pos.lower_bound(x);
if(itl == pos.begin()) {
itl = prev(pos.end());
} else {
itl = prev(itl);
}
return pii(*itl, *itr);
}
void insert(int x) {
if(sz(pos) == 0) {
pos.insert(x);
d.emplace(0, x);
return;
}
int l, r; tie(l, r) = find(x);
d.erase(pii(dist(l, r), r));
d.insert(pii(dist(l, x), x));
d.insert(pii(dist(x, r), r));
pos.insert(x);
}
void erase(int x) {
if(sz(pos) == 1) {
pos.clear(); d.clear();
return;
}
int l, r; tie(l, r) = find(x);
d.erase(pii(dist(l, x), x));
d.erase(pii(dist(x, r), r));
d.insert(pii(dist(l, r), r));
pos.erase(x);
}
int pop() {
int i = prev(d.end())->S;
erase(i);
return i;
}
void out() {
for(auto &i:pos) cerr << i << " ";
cerr << "\n";
for(auto &i:d) cerr << "(" << i.F << ", " << i.S << ") ";
cerr << "\n";
}
} aya;
#define L(id) ((id) * 2 + 1)
#define R(id) ((id) * 2 + 2)
struct SegTree {
int add[MAXN * 4 + 10];
pii mn[MAXN * 4 + 10]; // {value, index}
void pull(int id) {
mn[id] = min(mn[L(id)], mn[R(id)]);
mn[id].F += add[id];
}
void build(int id, int l, int r, int *owo) {
add[id] = 0;
if(l == r) {
add[id] = owo[l];
mn[id] = pii(owo[l], l);
return;
}
int m = (l + r) / 2;
build(L(id), l, m, owo);
build(R(id), m + 1, r, owo);
pull(id);
}
void range_add(int id, int l, int r, int L, int R, int val) {
if(L > R) {
range_add(0, 0, n - 1, L, n - 1, val);
range_add(0, 0, n - 1, 0, R, val);
return;
}
if(l >= L && r <= R) {
add[id] += val; mn[id].F += val;
return;
}
int m = (l + r) / 2;
if(L <= m) range_add(L(id), l, m, L, R, val);
if(R > m) range_add(R(id), m + 1, r, L, R, val);
pull(id);
}
pii find_min(int id, int l, int r, int L, int R) {
if(L > R) {
return min(
find_min(0, 0, n - 1, L, n - 1),
find_min(0, 0, n - 1, 0, R)
);
}
if(l >= L && r <= R) return mn[id];
int m = (l + r) / 2;
pii res(n * 8, -1);
if(L <= m) res = min(res, find_min(L(id), l, m, L, R));
if(R > m) res = min(res, find_min(R(id), m + 1, r, L, R));
res.F += add[id];
return res;
}
void kill_all() {
pii res = find_min(0, 0, n - 1, 0, n - 1);
while(res.F == 0) {
aya.insert(res.S);
range_add(0, 0, n - 1, res.S, res.S, n * 888);
res = find_min(0, 0, n - 1, 0, n - 1);
}
}
} seg;
int r[MAXN + 10];
int rk[MAXN + 10];
int pos[MAXN + 10];
int pl[MAXLG + 10][MAXN + 10];
int pr[MAXLG + 10][MAXN + 10];
int sl[MAXLG + 10][MAXN + 10];
int sr[MAXLG + 10][MAXN + 10];
void init(int32_t __k, vector<int32_t> __r) {
k = __k;
n = sz(__r);
For(i, 0, n - 1) r[i] = __r[i];
seg.build(0, 0, n - 1, r);
seg.kill_all();
For(rank, 1, n) {
int idx = aya.pop();
seg.range_add(0, 0, n - 1, (idx + n - k + 1) % n, (idx + n - 1) % n, -1);
seg.kill_all();
rk[idx] = rank;
pos[rank] = idx;
}
seg.build(0, 0, n - 1, rk);
For(rank, 1, n) {
int idx = pos[rank];
pii owo;
owo = seg.find_min(0, 0, n - 1, (idx + n - k + 1) % n, (idx + n - 1) % n);
pl[0][idx] = (owo.F <= n ? owo.S : idx);
owo = seg.find_min(0, 0, n - 1, (idx + 1) % n, (idx + k - 1) % n);
pr[0][idx] = (owo.F <= n ? owo.S : idx);
sl[0][idx] = dist(pl[0][idx], idx);
sr[0][idx] = dist(idx, pr[0][idx]);
seg.range_add(0, 0, n - 1, idx, idx, n * 888);
}
// For(i, 0, n - 1) cerr << rk[i] << " \n"[i == n - 1];
// For(i, 0, n - 1) cerr << pl[i] << " \n"[i == n - 1];
// For(i, 0, n - 1) cerr << sl[i] << " \n"[i == n - 1];
// For(i, 0, n - 1) cerr << pr[i] << " \n"[i == n - 1];
// For(i, 0, n - 1) cerr << sr[i] << " \n"[i == n - 1];
For(j, 1, MAXLG - 1) {
For(i, 0, n - 1) {
pl[j][i] = pl[j - 1][pl[j - 1][i]];
pr[j][i] = pr[j - 1][pr[j - 1][i]];
sl[j][i] = sl[j - 1][i] + sl[j - 1][pl[j - 1][i]];
sr[j][i] = sr[j - 1][i] + sr[j - 1][pr[j - 1][i]];
}
// For(i, 0, n - 1) cerr << sr[i] << " \n"[i == n - 1];
}
}
int32_t compare_plants(int32_t x, int32_t y) {
if(rk[x] > rk[y]) return -compare_plants(y, x);
int d, x2;
// left
d = dist(y, x);
x2 = x;
Forr(i, MAXLG - 1, 0) {
if(d - sl[i][x2] > 0) {
d -= sl[i][x2];
x2 = pl[i][x2];
}
}
if(d <= sl[0][x2] && rk[x2] <= rk[y]) return 1;
// right
d = dist(x, y);
x2 = x;
Forr(i, MAXLG - 1, 0) {
if(d - sr[i][x2] > 0) {
d -= sr[i][x2];
x2 = pr[i][x2];
}
}
if(d <= sr[0][x2] && rk[x2] <= rk[y]) return 1;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13396 KB |
Output is correct |
2 |
Correct |
5 ms |
13368 KB |
Output is correct |
3 |
Correct |
5 ms |
13396 KB |
Output is correct |
4 |
Correct |
4 ms |
13396 KB |
Output is correct |
5 |
Correct |
6 ms |
13396 KB |
Output is correct |
6 |
Correct |
70 ms |
16208 KB |
Output is correct |
7 |
Correct |
233 ms |
30496 KB |
Output is correct |
8 |
Correct |
1043 ms |
161696 KB |
Output is correct |
9 |
Correct |
989 ms |
161324 KB |
Output is correct |
10 |
Correct |
946 ms |
161464 KB |
Output is correct |
11 |
Correct |
1014 ms |
162648 KB |
Output is correct |
12 |
Correct |
991 ms |
162272 KB |
Output is correct |
13 |
Correct |
1239 ms |
172636 KB |
Output is correct |
14 |
Correct |
548 ms |
150832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13396 KB |
Output is correct |
2 |
Correct |
6 ms |
13396 KB |
Output is correct |
3 |
Correct |
7 ms |
13396 KB |
Output is correct |
4 |
Correct |
5 ms |
13396 KB |
Output is correct |
5 |
Correct |
8 ms |
13404 KB |
Output is correct |
6 |
Correct |
8 ms |
14036 KB |
Output is correct |
7 |
Correct |
77 ms |
19680 KB |
Output is correct |
8 |
Correct |
9 ms |
13556 KB |
Output is correct |
9 |
Correct |
8 ms |
14036 KB |
Output is correct |
10 |
Correct |
71 ms |
19680 KB |
Output is correct |
11 |
Correct |
123 ms |
19836 KB |
Output is correct |
12 |
Correct |
95 ms |
19700 KB |
Output is correct |
13 |
Correct |
65 ms |
19640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13396 KB |
Output is correct |
2 |
Correct |
6 ms |
13396 KB |
Output is correct |
3 |
Correct |
7 ms |
13396 KB |
Output is correct |
4 |
Correct |
5 ms |
13396 KB |
Output is correct |
5 |
Correct |
8 ms |
13404 KB |
Output is correct |
6 |
Correct |
8 ms |
14036 KB |
Output is correct |
7 |
Correct |
77 ms |
19680 KB |
Output is correct |
8 |
Correct |
9 ms |
13556 KB |
Output is correct |
9 |
Correct |
8 ms |
14036 KB |
Output is correct |
10 |
Correct |
71 ms |
19680 KB |
Output is correct |
11 |
Correct |
123 ms |
19836 KB |
Output is correct |
12 |
Correct |
95 ms |
19700 KB |
Output is correct |
13 |
Correct |
65 ms |
19640 KB |
Output is correct |
14 |
Correct |
122 ms |
29896 KB |
Output is correct |
15 |
Correct |
1045 ms |
150716 KB |
Output is correct |
16 |
Correct |
122 ms |
32064 KB |
Output is correct |
17 |
Correct |
957 ms |
154472 KB |
Output is correct |
18 |
Correct |
1128 ms |
165004 KB |
Output is correct |
19 |
Correct |
767 ms |
154572 KB |
Output is correct |
20 |
Correct |
786 ms |
154560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13396 KB |
Output is correct |
2 |
Correct |
6 ms |
13396 KB |
Output is correct |
3 |
Correct |
100 ms |
17556 KB |
Output is correct |
4 |
Correct |
835 ms |
158128 KB |
Output is correct |
5 |
Correct |
961 ms |
152408 KB |
Output is correct |
6 |
Correct |
973 ms |
150916 KB |
Output is correct |
7 |
Correct |
1105 ms |
150764 KB |
Output is correct |
8 |
Correct |
883 ms |
150748 KB |
Output is correct |
9 |
Correct |
986 ms |
159152 KB |
Output is correct |
10 |
Correct |
1014 ms |
155620 KB |
Output is correct |
11 |
Correct |
1111 ms |
175644 KB |
Output is correct |
12 |
Correct |
572 ms |
153928 KB |
Output is correct |
13 |
Correct |
1176 ms |
169256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13396 KB |
Output is correct |
2 |
Correct |
6 ms |
13376 KB |
Output is correct |
3 |
Correct |
7 ms |
13396 KB |
Output is correct |
4 |
Correct |
5 ms |
13396 KB |
Output is correct |
5 |
Correct |
7 ms |
13424 KB |
Output is correct |
6 |
Correct |
9 ms |
13524 KB |
Output is correct |
7 |
Correct |
26 ms |
14164 KB |
Output is correct |
8 |
Correct |
20 ms |
14136 KB |
Output is correct |
9 |
Correct |
21 ms |
14184 KB |
Output is correct |
10 |
Correct |
16 ms |
14220 KB |
Output is correct |
11 |
Correct |
25 ms |
14156 KB |
Output is correct |
12 |
Correct |
22 ms |
14132 KB |
Output is correct |
13 |
Correct |
17 ms |
14208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13312 KB |
Output is correct |
2 |
Correct |
5 ms |
13396 KB |
Output is correct |
3 |
Correct |
5 ms |
13396 KB |
Output is correct |
4 |
Correct |
6 ms |
13396 KB |
Output is correct |
5 |
Correct |
8 ms |
14036 KB |
Output is correct |
6 |
Correct |
675 ms |
151108 KB |
Output is correct |
7 |
Correct |
918 ms |
150776 KB |
Output is correct |
8 |
Correct |
859 ms |
150828 KB |
Output is correct |
9 |
Correct |
876 ms |
150724 KB |
Output is correct |
10 |
Correct |
536 ms |
158324 KB |
Output is correct |
11 |
Correct |
809 ms |
154632 KB |
Output is correct |
12 |
Correct |
701 ms |
160052 KB |
Output is correct |
13 |
Correct |
705 ms |
154564 KB |
Output is correct |
14 |
Correct |
738 ms |
153360 KB |
Output is correct |
15 |
Correct |
879 ms |
153428 KB |
Output is correct |
16 |
Correct |
584 ms |
156552 KB |
Output is correct |
17 |
Correct |
610 ms |
153604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
13396 KB |
Output is correct |
2 |
Correct |
5 ms |
13368 KB |
Output is correct |
3 |
Correct |
5 ms |
13396 KB |
Output is correct |
4 |
Correct |
4 ms |
13396 KB |
Output is correct |
5 |
Correct |
6 ms |
13396 KB |
Output is correct |
6 |
Correct |
70 ms |
16208 KB |
Output is correct |
7 |
Correct |
233 ms |
30496 KB |
Output is correct |
8 |
Correct |
1043 ms |
161696 KB |
Output is correct |
9 |
Correct |
989 ms |
161324 KB |
Output is correct |
10 |
Correct |
946 ms |
161464 KB |
Output is correct |
11 |
Correct |
1014 ms |
162648 KB |
Output is correct |
12 |
Correct |
991 ms |
162272 KB |
Output is correct |
13 |
Correct |
1239 ms |
172636 KB |
Output is correct |
14 |
Correct |
548 ms |
150832 KB |
Output is correct |
15 |
Correct |
6 ms |
13396 KB |
Output is correct |
16 |
Correct |
6 ms |
13396 KB |
Output is correct |
17 |
Correct |
7 ms |
13396 KB |
Output is correct |
18 |
Correct |
5 ms |
13396 KB |
Output is correct |
19 |
Correct |
8 ms |
13404 KB |
Output is correct |
20 |
Correct |
8 ms |
14036 KB |
Output is correct |
21 |
Correct |
77 ms |
19680 KB |
Output is correct |
22 |
Correct |
9 ms |
13556 KB |
Output is correct |
23 |
Correct |
8 ms |
14036 KB |
Output is correct |
24 |
Correct |
71 ms |
19680 KB |
Output is correct |
25 |
Correct |
123 ms |
19836 KB |
Output is correct |
26 |
Correct |
95 ms |
19700 KB |
Output is correct |
27 |
Correct |
65 ms |
19640 KB |
Output is correct |
28 |
Correct |
122 ms |
29896 KB |
Output is correct |
29 |
Correct |
1045 ms |
150716 KB |
Output is correct |
30 |
Correct |
122 ms |
32064 KB |
Output is correct |
31 |
Correct |
957 ms |
154472 KB |
Output is correct |
32 |
Correct |
1128 ms |
165004 KB |
Output is correct |
33 |
Correct |
767 ms |
154572 KB |
Output is correct |
34 |
Correct |
786 ms |
154560 KB |
Output is correct |
35 |
Correct |
5 ms |
13396 KB |
Output is correct |
36 |
Correct |
6 ms |
13396 KB |
Output is correct |
37 |
Correct |
100 ms |
17556 KB |
Output is correct |
38 |
Correct |
835 ms |
158128 KB |
Output is correct |
39 |
Correct |
961 ms |
152408 KB |
Output is correct |
40 |
Correct |
973 ms |
150916 KB |
Output is correct |
41 |
Correct |
1105 ms |
150764 KB |
Output is correct |
42 |
Correct |
883 ms |
150748 KB |
Output is correct |
43 |
Correct |
986 ms |
159152 KB |
Output is correct |
44 |
Correct |
1014 ms |
155620 KB |
Output is correct |
45 |
Correct |
1111 ms |
175644 KB |
Output is correct |
46 |
Correct |
572 ms |
153928 KB |
Output is correct |
47 |
Correct |
1176 ms |
169256 KB |
Output is correct |
48 |
Correct |
6 ms |
13396 KB |
Output is correct |
49 |
Correct |
6 ms |
13376 KB |
Output is correct |
50 |
Correct |
7 ms |
13396 KB |
Output is correct |
51 |
Correct |
5 ms |
13396 KB |
Output is correct |
52 |
Correct |
7 ms |
13424 KB |
Output is correct |
53 |
Correct |
9 ms |
13524 KB |
Output is correct |
54 |
Correct |
26 ms |
14164 KB |
Output is correct |
55 |
Correct |
20 ms |
14136 KB |
Output is correct |
56 |
Correct |
21 ms |
14184 KB |
Output is correct |
57 |
Correct |
16 ms |
14220 KB |
Output is correct |
58 |
Correct |
25 ms |
14156 KB |
Output is correct |
59 |
Correct |
22 ms |
14132 KB |
Output is correct |
60 |
Correct |
17 ms |
14208 KB |
Output is correct |
61 |
Correct |
92 ms |
19256 KB |
Output is correct |
62 |
Correct |
247 ms |
32152 KB |
Output is correct |
63 |
Correct |
1051 ms |
156192 KB |
Output is correct |
64 |
Correct |
764 ms |
154148 KB |
Output is correct |
65 |
Correct |
1012 ms |
154072 KB |
Output is correct |
66 |
Correct |
918 ms |
154304 KB |
Output is correct |
67 |
Correct |
882 ms |
154400 KB |
Output is correct |
68 |
Correct |
955 ms |
159164 KB |
Output is correct |
69 |
Correct |
824 ms |
155476 KB |
Output is correct |
70 |
Correct |
962 ms |
160988 KB |
Output is correct |
71 |
Correct |
992 ms |
155448 KB |
Output is correct |
72 |
Correct |
1034 ms |
154196 KB |
Output is correct |
73 |
Correct |
890 ms |
154328 KB |
Output is correct |
74 |
Correct |
954 ms |
155344 KB |
Output is correct |
75 |
Correct |
851 ms |
154524 KB |
Output is correct |