#include "plants.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define ll long long
using namespace std;
namespace
{
int n, k, order[200005], out[200005], in[200005];
ll l[200005][20], r[200005][20];
}
struct segTree
{
pll seg[800005];
ll lazy[800005];
void init(int i = 1, int L = 0, int R = n - 1)
{
seg[i].S = L;
if (L == R)
return;
int mid = (L + R) / 2;
init(i << 1, L, mid);
init(i << 1 | 1, mid + 1, R);
}
void modify(int mL, int mR, ll val, int i = 1, int L = 0, int R = n - 1)
{
if (mL <= L && R <= mR)
{
lazy[i] += val;
seg[i].F += val;
}
else if (R < mL || mR < L)
return;
else
{
int mid = (L + R) / 2;
lazy[i << 1] += lazy[i];
lazy[i << 1 | 1] += lazy[i];
seg[i << 1].F += lazy[i];
seg[i << 1 | 1].F += lazy[i];
lazy[i] = 0;
modify(mL, mR, val, i << 1, L, mid);
modify(mL, mR, val, i << 1 | 1, mid + 1, R);
seg[i] = min(seg[i << 1], seg[i << 1 | 1]);
}
}
pll query(int qL, int qR, int i = 1, int L = 0, int R = n - 1)
{
if (qL <= L && R <= qR)
return seg[i];
else if (R < qL || qR < L)
return pll(1e9, n + 1);
else
{
int mid = (L + R) / 2;
lazy[i << 1] += lazy[i];
lazy[i << 1 | 1] += lazy[i];
seg[i << 1].F += lazy[i];
seg[i << 1 | 1].F += lazy[i];
lazy[i] = 0;
return min(query(qL, qR, i << 1, L, mid),
query(qL, qR, i << 1 | 1, mid + 1, R));
}
}
void safe_modify(int l, int r, ll val)
{
if (l <= r)
modify(l, r, val);
else
{
modify(l, n - 1, val);
modify(0, r, val);
}
}
} seg, zero;
struct changeValseg
{
ll seg[800005];
void init(int i = 1, int L = 0, int R = n - 1)
{
seg[i] = -1;
if (L == R)
return;
int mid = (L + R) / 2;
init(i << 1, L, mid);
init(i << 1 | 1, mid + 1, R);
}
void modify(int mL, int mR, ll val, int i = 1, int L = 0, int R = n - 1)
{
if (mL <= L && R <= mR)
seg[i] = val;
else if (R < mL || mR < L)
return;
else
{
if (seg[i] != -1)
{
seg[i << 1] = seg[i << 1 | 1] = seg[i];
seg[i] = -1;
}
int mid = (L + R) / 2;
modify(mL, mR, val, i << 1, L, mid);
modify(mL, mR, val, i << 1 | 1, mid + 1, R);
}
}
ll query(int pos, int i = 1, int L = 0, int R = n - 1)
{
if (seg[i] != -1 || L == R)
return seg[i];
else
{
int mid = (L + R) / 2;
if (pos <= mid)
return query(pos, i << 1, L, mid);
else
return query(pos, i << 1 | 1, mid + 1, R);
}
}
void safe_modify(int l, int r, ll val)
{
if (l <= r)
modify(l, r, val);
else
{
modify(l, n - 1, val);
modify(0, r, val);
}
}
} open, conceal;
const ll inf = 1e9;
void init(int k, vector<int> r)
{
n = r.size();
::k = k;
seg.init();
zero.init();
open.init();
conceal.init();
for (int i = 0; i < n; i++)
{
r[i] = k - 1 - r[i];
seg.modify(i, i, r[i]);
zero.modify(i, i, r[i]);
}
for (int it = 1; it <= n; it++)
{
{
auto [v, p] = zero.query(0, n - 1);
while (v == 0)
{
seg.safe_modify((p + 1) % n, (p + k - 1) % n, inf);
zero.modify(p, p, inf);
out[p] = open.query(p);
tie(v, p) = zero.query(0, n - 1);
}
}
int cur = seg.query(0, n - 1).S;
in[cur] = conceal.query(cur);
// cerr << cur << "->" << in[cur] << ' ' << out[cur] << '\n';
seg.modify(cur, cur, inf);
seg.safe_modify((cur + 1) % n, (cur + k - 1) % n, -inf);
zero.safe_modify((cur - k + 1 + n) % n, cur, -1);
seg.safe_modify((cur - k + 1 + n) % n, cur, -1);
conceal.safe_modify((cur + 1) % n, (cur + k - 1) % n, cur);
open.safe_modify((cur - k + 1 + n) % n, cur, cur);
assert(order[cur] == 0);
order[cur] = it;
}
vector<int> ord(n);
for (int i = 0; i < n; i++)
ord[order[i] - 1] = i;
for (int i = 0; i < n; i++)
{
int u = ord[i];
if (in[u] >= 0)
l[u][0] = (u - in[u] + n) % n;
else
l[u][0] = 0;
if (out[u] >= 0)
::r[u][0] = (out[u] - u + n) % n;
else
::r[u][0] = 0;
}
for (int p = 0; p + 1 < 20; p++)
for (int i = 0; i < n; i++)
{
l[i][p + 1] = l[i][p] + l[((i - l[i][p]) % n + n) % n][p];
::r[i][p + 1] = ::r[i][p] + ::r[(i + ::r[i][p]) % n][p];
}
// for (int i = 0; i < n; i++)
// cerr << in[i] << ' ' << l[i] << ' ' << out[i] << ' ' << ::r[i] << '\n';
}
bool reach(int x, int y)
{
// check left
int u = x, v = y;
if (u > v)
u -= n;
for (int p = 19; p >= 0; p--)
if (u <= v - l[(v + n) % n][p])
v = v - l[(v + n) % n][p];
if (abs(u - v) < k && order[(u + n) % n] <= order[(v + n) % n])
return 1;
u = x, v = y;
if (u < v)
u += n;
for (int p = 19; p >= 0; p--)
if (v + r[v % n][p] <= u)
v = v + r[v % n][p];
if (abs(u - v) < k)
return order[u % n] <= order[v % n];
return 0;
}
int compare_plants(int x, int y)
{
if (reach(x, y))
return -1;
if (reach(y, x))
return 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25300 KB |
Output is correct |
2 |
Correct |
10 ms |
25364 KB |
Output is correct |
3 |
Correct |
10 ms |
25376 KB |
Output is correct |
4 |
Correct |
10 ms |
25380 KB |
Output is correct |
5 |
Correct |
10 ms |
25300 KB |
Output is correct |
6 |
Correct |
131 ms |
29132 KB |
Output is correct |
7 |
Correct |
199 ms |
39156 KB |
Output is correct |
8 |
Correct |
777 ms |
114264 KB |
Output is correct |
9 |
Correct |
782 ms |
114352 KB |
Output is correct |
10 |
Correct |
793 ms |
114356 KB |
Output is correct |
11 |
Correct |
820 ms |
114392 KB |
Output is correct |
12 |
Correct |
858 ms |
114328 KB |
Output is correct |
13 |
Correct |
918 ms |
114364 KB |
Output is correct |
14 |
Correct |
738 ms |
114364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25300 KB |
Output is correct |
2 |
Correct |
10 ms |
25328 KB |
Output is correct |
3 |
Correct |
11 ms |
25300 KB |
Output is correct |
4 |
Correct |
10 ms |
25380 KB |
Output is correct |
5 |
Correct |
12 ms |
25380 KB |
Output is correct |
6 |
Correct |
16 ms |
25912 KB |
Output is correct |
7 |
Correct |
130 ms |
32344 KB |
Output is correct |
8 |
Correct |
13 ms |
25556 KB |
Output is correct |
9 |
Correct |
16 ms |
25804 KB |
Output is correct |
10 |
Correct |
128 ms |
32304 KB |
Output is correct |
11 |
Correct |
180 ms |
32280 KB |
Output is correct |
12 |
Correct |
98 ms |
32344 KB |
Output is correct |
13 |
Correct |
127 ms |
32332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25300 KB |
Output is correct |
2 |
Correct |
10 ms |
25328 KB |
Output is correct |
3 |
Correct |
11 ms |
25300 KB |
Output is correct |
4 |
Correct |
10 ms |
25380 KB |
Output is correct |
5 |
Correct |
12 ms |
25380 KB |
Output is correct |
6 |
Correct |
16 ms |
25912 KB |
Output is correct |
7 |
Correct |
130 ms |
32344 KB |
Output is correct |
8 |
Correct |
13 ms |
25556 KB |
Output is correct |
9 |
Correct |
16 ms |
25804 KB |
Output is correct |
10 |
Correct |
128 ms |
32304 KB |
Output is correct |
11 |
Correct |
180 ms |
32280 KB |
Output is correct |
12 |
Correct |
98 ms |
32344 KB |
Output is correct |
13 |
Correct |
127 ms |
32332 KB |
Output is correct |
14 |
Correct |
192 ms |
39136 KB |
Output is correct |
15 |
Correct |
1387 ms |
115148 KB |
Output is correct |
16 |
Correct |
198 ms |
39216 KB |
Output is correct |
17 |
Correct |
1392 ms |
115188 KB |
Output is correct |
18 |
Correct |
1064 ms |
114792 KB |
Output is correct |
19 |
Correct |
953 ms |
115252 KB |
Output is correct |
20 |
Correct |
1234 ms |
115200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25300 KB |
Output is correct |
2 |
Correct |
10 ms |
25428 KB |
Output is correct |
3 |
Correct |
139 ms |
30664 KB |
Output is correct |
4 |
Correct |
982 ms |
114384 KB |
Output is correct |
5 |
Correct |
1006 ms |
114476 KB |
Output is correct |
6 |
Correct |
1227 ms |
114920 KB |
Output is correct |
7 |
Correct |
1342 ms |
114904 KB |
Output is correct |
8 |
Correct |
1356 ms |
115060 KB |
Output is correct |
9 |
Correct |
1022 ms |
114520 KB |
Output is correct |
10 |
Correct |
906 ms |
114380 KB |
Output is correct |
11 |
Correct |
890 ms |
114392 KB |
Output is correct |
12 |
Correct |
842 ms |
114580 KB |
Output is correct |
13 |
Correct |
1148 ms |
114668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25328 KB |
Output is correct |
2 |
Correct |
11 ms |
25388 KB |
Output is correct |
3 |
Correct |
10 ms |
25368 KB |
Output is correct |
4 |
Correct |
10 ms |
25428 KB |
Output is correct |
5 |
Correct |
10 ms |
25380 KB |
Output is correct |
6 |
Correct |
14 ms |
25556 KB |
Output is correct |
7 |
Correct |
42 ms |
26388 KB |
Output is correct |
8 |
Correct |
33 ms |
26396 KB |
Output is correct |
9 |
Correct |
40 ms |
26392 KB |
Output is correct |
10 |
Correct |
33 ms |
26400 KB |
Output is correct |
11 |
Correct |
40 ms |
26400 KB |
Output is correct |
12 |
Correct |
37 ms |
26392 KB |
Output is correct |
13 |
Correct |
32 ms |
26400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25300 KB |
Output is correct |
2 |
Correct |
10 ms |
25376 KB |
Output is correct |
3 |
Correct |
10 ms |
25348 KB |
Output is correct |
4 |
Correct |
11 ms |
25304 KB |
Output is correct |
5 |
Correct |
13 ms |
25812 KB |
Output is correct |
6 |
Correct |
935 ms |
113776 KB |
Output is correct |
7 |
Correct |
1106 ms |
113848 KB |
Output is correct |
8 |
Correct |
1345 ms |
114076 KB |
Output is correct |
9 |
Correct |
1317 ms |
114224 KB |
Output is correct |
10 |
Correct |
871 ms |
113732 KB |
Output is correct |
11 |
Correct |
1135 ms |
114196 KB |
Output is correct |
12 |
Correct |
821 ms |
113632 KB |
Output is correct |
13 |
Correct |
963 ms |
113728 KB |
Output is correct |
14 |
Correct |
1196 ms |
113884 KB |
Output is correct |
15 |
Correct |
1243 ms |
114124 KB |
Output is correct |
16 |
Correct |
841 ms |
113556 KB |
Output is correct |
17 |
Correct |
888 ms |
113680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25300 KB |
Output is correct |
2 |
Correct |
10 ms |
25364 KB |
Output is correct |
3 |
Correct |
10 ms |
25376 KB |
Output is correct |
4 |
Correct |
10 ms |
25380 KB |
Output is correct |
5 |
Correct |
10 ms |
25300 KB |
Output is correct |
6 |
Correct |
131 ms |
29132 KB |
Output is correct |
7 |
Correct |
199 ms |
39156 KB |
Output is correct |
8 |
Correct |
777 ms |
114264 KB |
Output is correct |
9 |
Correct |
782 ms |
114352 KB |
Output is correct |
10 |
Correct |
793 ms |
114356 KB |
Output is correct |
11 |
Correct |
820 ms |
114392 KB |
Output is correct |
12 |
Correct |
858 ms |
114328 KB |
Output is correct |
13 |
Correct |
918 ms |
114364 KB |
Output is correct |
14 |
Correct |
738 ms |
114364 KB |
Output is correct |
15 |
Correct |
10 ms |
25300 KB |
Output is correct |
16 |
Correct |
10 ms |
25328 KB |
Output is correct |
17 |
Correct |
11 ms |
25300 KB |
Output is correct |
18 |
Correct |
10 ms |
25380 KB |
Output is correct |
19 |
Correct |
12 ms |
25380 KB |
Output is correct |
20 |
Correct |
16 ms |
25912 KB |
Output is correct |
21 |
Correct |
130 ms |
32344 KB |
Output is correct |
22 |
Correct |
13 ms |
25556 KB |
Output is correct |
23 |
Correct |
16 ms |
25804 KB |
Output is correct |
24 |
Correct |
128 ms |
32304 KB |
Output is correct |
25 |
Correct |
180 ms |
32280 KB |
Output is correct |
26 |
Correct |
98 ms |
32344 KB |
Output is correct |
27 |
Correct |
127 ms |
32332 KB |
Output is correct |
28 |
Correct |
192 ms |
39136 KB |
Output is correct |
29 |
Correct |
1387 ms |
115148 KB |
Output is correct |
30 |
Correct |
198 ms |
39216 KB |
Output is correct |
31 |
Correct |
1392 ms |
115188 KB |
Output is correct |
32 |
Correct |
1064 ms |
114792 KB |
Output is correct |
33 |
Correct |
953 ms |
115252 KB |
Output is correct |
34 |
Correct |
1234 ms |
115200 KB |
Output is correct |
35 |
Correct |
10 ms |
25300 KB |
Output is correct |
36 |
Correct |
10 ms |
25428 KB |
Output is correct |
37 |
Correct |
139 ms |
30664 KB |
Output is correct |
38 |
Correct |
982 ms |
114384 KB |
Output is correct |
39 |
Correct |
1006 ms |
114476 KB |
Output is correct |
40 |
Correct |
1227 ms |
114920 KB |
Output is correct |
41 |
Correct |
1342 ms |
114904 KB |
Output is correct |
42 |
Correct |
1356 ms |
115060 KB |
Output is correct |
43 |
Correct |
1022 ms |
114520 KB |
Output is correct |
44 |
Correct |
906 ms |
114380 KB |
Output is correct |
45 |
Correct |
890 ms |
114392 KB |
Output is correct |
46 |
Correct |
842 ms |
114580 KB |
Output is correct |
47 |
Correct |
1148 ms |
114668 KB |
Output is correct |
48 |
Correct |
10 ms |
25328 KB |
Output is correct |
49 |
Correct |
11 ms |
25388 KB |
Output is correct |
50 |
Correct |
10 ms |
25368 KB |
Output is correct |
51 |
Correct |
10 ms |
25428 KB |
Output is correct |
52 |
Correct |
10 ms |
25380 KB |
Output is correct |
53 |
Correct |
14 ms |
25556 KB |
Output is correct |
54 |
Correct |
42 ms |
26388 KB |
Output is correct |
55 |
Correct |
33 ms |
26396 KB |
Output is correct |
56 |
Correct |
40 ms |
26392 KB |
Output is correct |
57 |
Correct |
33 ms |
26400 KB |
Output is correct |
58 |
Correct |
40 ms |
26400 KB |
Output is correct |
59 |
Correct |
37 ms |
26392 KB |
Output is correct |
60 |
Correct |
32 ms |
26400 KB |
Output is correct |
61 |
Correct |
161 ms |
30664 KB |
Output is correct |
62 |
Correct |
221 ms |
39076 KB |
Output is correct |
63 |
Correct |
883 ms |
114380 KB |
Output is correct |
64 |
Correct |
977 ms |
114560 KB |
Output is correct |
65 |
Correct |
1197 ms |
114784 KB |
Output is correct |
66 |
Correct |
1322 ms |
114844 KB |
Output is correct |
67 |
Correct |
1352 ms |
115196 KB |
Output is correct |
68 |
Correct |
967 ms |
114488 KB |
Output is correct |
69 |
Correct |
1199 ms |
115056 KB |
Output is correct |
70 |
Correct |
1037 ms |
114340 KB |
Output is correct |
71 |
Correct |
1109 ms |
114556 KB |
Output is correct |
72 |
Correct |
1415 ms |
114704 KB |
Output is correct |
73 |
Correct |
1470 ms |
114896 KB |
Output is correct |
74 |
Correct |
996 ms |
114332 KB |
Output is correct |
75 |
Correct |
947 ms |
114560 KB |
Output is correct |