#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const long long MXN = 2e5 + 5;
const long long LOG = 20;
#define inf 0x3F3F3F3F
long long n;
array<long long, 2> p[2][LOG][MXN];
long long rr[MXN], f[MXN];
long long cnt[MXN];
array<long long, 2> st[MXN << 2];
long long lz[MXN << 2];
void build(long long l, long long r, long long x)
{
if (l == r)
{
st[x] = {cnt[l] - rr[l], l};
return;
}
long long mid = (l + r) >> 1;
build(l, mid, 2*x);
build(mid + 1, r, 2*x + 1);
st[x] = min(st[2*x], st[2*x + 1]);
}
void relax(long long l, long long r, long long x)
{
if (!lz[x]) return;
st[x][0] += lz[x];
if (l == r)
{
lz[x] = 0;
return;
}
lz[2*x] += lz[x], lz[2*x + 1] += lz[x];
lz[x] = 0;
}
void upd(long long l, long long r, long long x, long long lx, long long rx, long long val)
{
if (lx > rx) return;
relax(l, r, x);
if (l > rx || r < lx) return;
if (l >= lx && r <= rx)
{
lz[x] += val;
relax(l, r, x);
return;
}
long long mid = (l + r) >> 1;
upd(l, mid, 2*x, lx, rx, val);
upd(mid + 1, r, 2*x + 1, lx, rx, val);
st[x] = min(st[2*x], st[2*x + 1]);
}
set<long long> idx;
set<array<long long, 2>> dif;
void ins(long long x)
{
idx.insert(x);
if (idx.size() == 1)
{
dif.insert({0, x});
return;
}
auto it = idx.find(x);
auto itl = it, itr = it;
if (it == idx.begin()) itl = prev(idx.end());
else itl = prev(it);
if (next(it) == idx.end()) itr = idx.begin();
else itr = next(it);
dif.erase({(*itr - *itl + n) % n, *itr});
dif.insert({(x - *itl + n) % n, x});
dif.insert({(*itr - x + n) % n, *itr});
}
void ers(long long x)
{
if (idx.size() == 1)
{
idx.clear();
dif.clear();
return;
}
auto it = idx.find(x);
auto itl = it, itr = it;
if (it == idx.begin()) itl = prev(idx.end());
else itl = prev(it);
if (next(it) == idx.end()) itr = idx.begin();
else itr = next(it);
dif.erase({(x - *itl + n) % n, x});
dif.erase({(*itr - x + n) % n, *itr});
dif.insert({(*itr - *itl + n) % n, *itr});
idx.erase(it);
}
// 4 3 2
// 0 1 1 2
// 0 2
// 1 2
void init(int k, vector<int> r)
{
n = r.size();
f[n] = inf;
p[0][0][n] = {n, 0};
p[1][0][n] = {n, 0};
for (long long i = 1; i <= (n * 4); i++) st[i] = {inf, -1};
for (long long i = 0; i < n; i++) rr[i] = r[i];
for (long long i = 0; i < n; i++) cnt[i] = k - 1;
build(0, n - 1, 1);
while (1)
{
array<long long, 2> x = st[1];
if (!x[0])
{
ins(x[1]);
upd(0, n - 1, 1, x[1], x[1], inf);
}
else break;
}
long long cur = 0;
while (!idx.empty())
{
long long ind = (*dif.rbegin())[1];
ers(ind);
f[ind] = ++cur;
if (ind - 1 >= 0) upd(0, n - 1, 1, max(ind - k + 1, 0LL), ind - 1, -1);
if (ind - k + 1 < 0) upd(0, n - 1, 1, (ind - k + 1 + n) % n, n - 1, -1);
// for (long long i = (idx[ind] - 1 + n) % n; i != idx[(ind - 1 + sz) % sz]; i = (i - 1 + n) % n)
// {
// if (!f[i]) adj[idx[ind]].push_back(i);
// }
// for (long long i = (idx[ind] + 1) % n; i != (idx[ind] + k) % n; i = (i + 1) % n)
// {
// if (!f[i]) adj[idx[ind]].push_back(i);
// }
// for (long long i = (idx[ind] - 1 + n) % n; i != (idx[ind] - k + n) % n; i = (i - 1 + n) % n)
// {
// if (!f[i]) adj[idx[ind]].push_back(i);
// cnt[i]--;
// }
// f[idx[ind]] = 1;
while (1)
{
array<long long, 2> x = st[1];
if (!x[0])
{
ins(x[1]);
upd(0, n - 1, 1, x[1], x[1], inf);
}
else break;
}
}
set<array<long long, 2>> s;
for (long long i = 0; i < k; i++) s.insert({f[i], i});
for (long long i = n - 1; i >= 0; i--)
{
s.erase({f[(i + k) % n], (i + k) % n});
auto it = s.lower_bound({f[i], i});
if (it != s.end())
{
p[0][0][i] = {(*it)[1], ((*it)[1] - i + n) % n};
}
else p[0][0][i] = {n, inf};
s.insert({f[i], i});
}
s.clear();
for (long long i = n - k; i < n; i++) s.insert({f[i], i});
for (long long i = 0; i < n; i++)
{
s.erase({f[(i - k + n) % n], (i - k + n) % n});
auto it = s.lower_bound({f[i], i});
if (it != s.end())
{
p[1][0][i] = {(*it)[1], (i - (*it)[1] + n) % n};
}
else p[1][0][i] = {n, inf};
s.insert({f[i], i});
}
// for (long long i = 0; i < n; i++)
// {
// cout << f[i] << ' ';
// }
// cout << '\n';
// for (long long i = 0; i < n; i++)
// {
// cout << p[0][0][i][0] << ' ';
// }
// cout << '\n';
// for (long long i = 0; i < n; i++)
// {
// cout << p[1][0][i][0] << ' ';
// }
// cout << '\n';
for (long long j = 0; j < 2; j++)
{
for (long long i = 1; i < LOG; i++)
{
for (long long k = 0; k <= n; k++)
{
long long par = p[j][i - 1][k][0];
p[j][i][k] = {p[j][i - 1][par][0], p[j][i - 1][par][1] + p[j][i - 1][k][1]};
}
}
}
}
long long check(long long x, long long y)
{
long long x1 = x;
long long d = (y - x + n) % n;
long long res = 0;
for (long long i = LOG - 1; i >= 0; i--)
{
if (f[p[0][i][x][0]] <= f[y])
{
res += 1LL * p[0][i][x][1];
x = p[0][i][x][0];
}
}
if (f[x] <= f[y] && res >= d) return 1;
res = 0, x = x1;
d = (x - y + n) % n;
for (long long i = LOG - 1; i >= 0; i--)
{
if (f[p[1][i][x][0]] <= f[y])
{
res += 1LL * p[1][i][x][1];
x = p[1][i][x][0];
}
}
return (f[x] <= f[y] && res >= d);
}
int compare_plants(int x, int y)
{
// ? x < y
if (check(x, y)) return -1;
if (check(y, x)) return 1;
return 0;
// return (a[x] > a[y] ? 1 : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
35420 KB |
Output is correct |
2 |
Correct |
2 ms |
31452 KB |
Output is correct |
3 |
Correct |
2 ms |
31324 KB |
Output is correct |
4 |
Correct |
3 ms |
35420 KB |
Output is correct |
5 |
Correct |
2 ms |
31324 KB |
Output is correct |
6 |
Correct |
36 ms |
36096 KB |
Output is correct |
7 |
Correct |
84 ms |
47448 KB |
Output is correct |
8 |
Correct |
360 ms |
162644 KB |
Output is correct |
9 |
Correct |
388 ms |
163156 KB |
Output is correct |
10 |
Correct |
452 ms |
162900 KB |
Output is correct |
11 |
Correct |
453 ms |
161760 KB |
Output is correct |
12 |
Correct |
456 ms |
162128 KB |
Output is correct |
13 |
Correct |
452 ms |
151888 KB |
Output is correct |
14 |
Correct |
488 ms |
173604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
35420 KB |
Output is correct |
2 |
Correct |
2 ms |
31324 KB |
Output is correct |
3 |
Correct |
2 ms |
31324 KB |
Output is correct |
4 |
Correct |
3 ms |
33372 KB |
Output is correct |
5 |
Correct |
4 ms |
33372 KB |
Output is correct |
6 |
Correct |
5 ms |
33884 KB |
Output is correct |
7 |
Correct |
68 ms |
36876 KB |
Output is correct |
8 |
Correct |
4 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
33880 KB |
Output is correct |
10 |
Correct |
71 ms |
38920 KB |
Output is correct |
11 |
Correct |
68 ms |
38740 KB |
Output is correct |
12 |
Correct |
61 ms |
41068 KB |
Output is correct |
13 |
Correct |
79 ms |
38996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
35420 KB |
Output is correct |
2 |
Correct |
2 ms |
31324 KB |
Output is correct |
3 |
Correct |
2 ms |
31324 KB |
Output is correct |
4 |
Correct |
3 ms |
33372 KB |
Output is correct |
5 |
Correct |
4 ms |
33372 KB |
Output is correct |
6 |
Correct |
5 ms |
33884 KB |
Output is correct |
7 |
Correct |
68 ms |
36876 KB |
Output is correct |
8 |
Correct |
4 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
33880 KB |
Output is correct |
10 |
Correct |
71 ms |
38920 KB |
Output is correct |
11 |
Correct |
68 ms |
38740 KB |
Output is correct |
12 |
Correct |
61 ms |
41068 KB |
Output is correct |
13 |
Correct |
79 ms |
38996 KB |
Output is correct |
14 |
Correct |
130 ms |
47532 KB |
Output is correct |
15 |
Correct |
778 ms |
160596 KB |
Output is correct |
16 |
Correct |
112 ms |
49688 KB |
Output is correct |
17 |
Correct |
750 ms |
164332 KB |
Output is correct |
18 |
Correct |
522 ms |
162556 KB |
Output is correct |
19 |
Correct |
598 ms |
167640 KB |
Output is correct |
20 |
Correct |
770 ms |
169196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
33372 KB |
Output is correct |
2 |
Correct |
3 ms |
31324 KB |
Output is correct |
3 |
Correct |
58 ms |
35340 KB |
Output is correct |
4 |
Correct |
470 ms |
159824 KB |
Output is correct |
5 |
Correct |
478 ms |
154504 KB |
Output is correct |
6 |
Correct |
523 ms |
152912 KB |
Output is correct |
7 |
Correct |
551 ms |
153428 KB |
Output is correct |
8 |
Correct |
699 ms |
158544 KB |
Output is correct |
9 |
Correct |
466 ms |
156376 KB |
Output is correct |
10 |
Correct |
499 ms |
158036 KB |
Output is correct |
11 |
Correct |
437 ms |
154964 KB |
Output is correct |
12 |
Correct |
512 ms |
177672 KB |
Output is correct |
13 |
Correct |
546 ms |
159824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
31324 KB |
Output is correct |
2 |
Correct |
2 ms |
29276 KB |
Output is correct |
3 |
Correct |
3 ms |
29272 KB |
Output is correct |
4 |
Correct |
3 ms |
29276 KB |
Output is correct |
5 |
Correct |
2 ms |
29276 KB |
Output is correct |
6 |
Correct |
3 ms |
29276 KB |
Output is correct |
7 |
Correct |
11 ms |
28116 KB |
Output is correct |
8 |
Correct |
11 ms |
30040 KB |
Output is correct |
9 |
Correct |
12 ms |
30040 KB |
Output is correct |
10 |
Correct |
11 ms |
30044 KB |
Output is correct |
11 |
Correct |
11 ms |
30044 KB |
Output is correct |
12 |
Correct |
11 ms |
32136 KB |
Output is correct |
13 |
Correct |
11 ms |
27996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
29276 KB |
Output is correct |
2 |
Correct |
3 ms |
29272 KB |
Output is correct |
3 |
Correct |
2 ms |
29272 KB |
Output is correct |
4 |
Correct |
2 ms |
27228 KB |
Output is correct |
5 |
Correct |
4 ms |
29788 KB |
Output is correct |
6 |
Correct |
410 ms |
153172 KB |
Output is correct |
7 |
Correct |
429 ms |
153168 KB |
Output is correct |
8 |
Correct |
530 ms |
153420 KB |
Output is correct |
9 |
Correct |
604 ms |
158544 KB |
Output is correct |
10 |
Correct |
366 ms |
155600 KB |
Output is correct |
11 |
Correct |
416 ms |
160592 KB |
Output is correct |
12 |
Correct |
351 ms |
161872 KB |
Output is correct |
13 |
Correct |
456 ms |
156496 KB |
Output is correct |
14 |
Correct |
467 ms |
155476 KB |
Output is correct |
15 |
Correct |
499 ms |
156028 KB |
Output is correct |
16 |
Correct |
390 ms |
158944 KB |
Output is correct |
17 |
Correct |
425 ms |
155440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
35420 KB |
Output is correct |
2 |
Correct |
2 ms |
31452 KB |
Output is correct |
3 |
Correct |
2 ms |
31324 KB |
Output is correct |
4 |
Correct |
3 ms |
35420 KB |
Output is correct |
5 |
Correct |
2 ms |
31324 KB |
Output is correct |
6 |
Correct |
36 ms |
36096 KB |
Output is correct |
7 |
Correct |
84 ms |
47448 KB |
Output is correct |
8 |
Correct |
360 ms |
162644 KB |
Output is correct |
9 |
Correct |
388 ms |
163156 KB |
Output is correct |
10 |
Correct |
452 ms |
162900 KB |
Output is correct |
11 |
Correct |
453 ms |
161760 KB |
Output is correct |
12 |
Correct |
456 ms |
162128 KB |
Output is correct |
13 |
Correct |
452 ms |
151888 KB |
Output is correct |
14 |
Correct |
488 ms |
173604 KB |
Output is correct |
15 |
Correct |
3 ms |
35420 KB |
Output is correct |
16 |
Correct |
2 ms |
31324 KB |
Output is correct |
17 |
Correct |
2 ms |
31324 KB |
Output is correct |
18 |
Correct |
3 ms |
33372 KB |
Output is correct |
19 |
Correct |
4 ms |
33372 KB |
Output is correct |
20 |
Correct |
5 ms |
33884 KB |
Output is correct |
21 |
Correct |
68 ms |
36876 KB |
Output is correct |
22 |
Correct |
4 ms |
31324 KB |
Output is correct |
23 |
Correct |
6 ms |
33880 KB |
Output is correct |
24 |
Correct |
71 ms |
38920 KB |
Output is correct |
25 |
Correct |
68 ms |
38740 KB |
Output is correct |
26 |
Correct |
61 ms |
41068 KB |
Output is correct |
27 |
Correct |
79 ms |
38996 KB |
Output is correct |
28 |
Correct |
130 ms |
47532 KB |
Output is correct |
29 |
Correct |
778 ms |
160596 KB |
Output is correct |
30 |
Correct |
112 ms |
49688 KB |
Output is correct |
31 |
Correct |
750 ms |
164332 KB |
Output is correct |
32 |
Correct |
522 ms |
162556 KB |
Output is correct |
33 |
Correct |
598 ms |
167640 KB |
Output is correct |
34 |
Correct |
770 ms |
169196 KB |
Output is correct |
35 |
Correct |
3 ms |
33372 KB |
Output is correct |
36 |
Correct |
3 ms |
31324 KB |
Output is correct |
37 |
Correct |
58 ms |
35340 KB |
Output is correct |
38 |
Correct |
470 ms |
159824 KB |
Output is correct |
39 |
Correct |
478 ms |
154504 KB |
Output is correct |
40 |
Correct |
523 ms |
152912 KB |
Output is correct |
41 |
Correct |
551 ms |
153428 KB |
Output is correct |
42 |
Correct |
699 ms |
158544 KB |
Output is correct |
43 |
Correct |
466 ms |
156376 KB |
Output is correct |
44 |
Correct |
499 ms |
158036 KB |
Output is correct |
45 |
Correct |
437 ms |
154964 KB |
Output is correct |
46 |
Correct |
512 ms |
177672 KB |
Output is correct |
47 |
Correct |
546 ms |
159824 KB |
Output is correct |
48 |
Correct |
2 ms |
31324 KB |
Output is correct |
49 |
Correct |
2 ms |
29276 KB |
Output is correct |
50 |
Correct |
3 ms |
29272 KB |
Output is correct |
51 |
Correct |
3 ms |
29276 KB |
Output is correct |
52 |
Correct |
2 ms |
29276 KB |
Output is correct |
53 |
Correct |
3 ms |
29276 KB |
Output is correct |
54 |
Correct |
11 ms |
28116 KB |
Output is correct |
55 |
Correct |
11 ms |
30040 KB |
Output is correct |
56 |
Correct |
12 ms |
30040 KB |
Output is correct |
57 |
Correct |
11 ms |
30044 KB |
Output is correct |
58 |
Correct |
11 ms |
30044 KB |
Output is correct |
59 |
Correct |
11 ms |
32136 KB |
Output is correct |
60 |
Correct |
11 ms |
27996 KB |
Output is correct |
61 |
Correct |
57 ms |
32852 KB |
Output is correct |
62 |
Correct |
91 ms |
42068 KB |
Output is correct |
63 |
Correct |
394 ms |
158292 KB |
Output is correct |
64 |
Correct |
489 ms |
156260 KB |
Output is correct |
65 |
Correct |
512 ms |
156240 KB |
Output is correct |
66 |
Correct |
566 ms |
156880 KB |
Output is correct |
67 |
Correct |
731 ms |
162388 KB |
Output is correct |
68 |
Correct |
454 ms |
156496 KB |
Output is correct |
69 |
Correct |
520 ms |
161512 KB |
Output is correct |
70 |
Correct |
536 ms |
162808 KB |
Output is correct |
71 |
Correct |
487 ms |
157428 KB |
Output is correct |
72 |
Correct |
525 ms |
156340 KB |
Output is correct |
73 |
Correct |
587 ms |
156892 KB |
Output is correct |
74 |
Correct |
401 ms |
157320 KB |
Output is correct |
75 |
Correct |
503 ms |
156524 KB |
Output is correct |