#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 20;
const int inf = 1e9 + 20;
int a[maxn];
int tree[maxn * 4];
int lazy[maxn * 4];
int pos[maxn];
int dp_left[maxn];
int dp_right[maxn];
int n, k;
set<int> all_zeroes, good_zeroes;
void build(int id, int lt, int rt) {
if (lt == rt) {
tree[id] = a[lt];
return;
}
int mt = (lt + rt) / 2;
build(id * 2, lt, mt);
build(id * 2 + 1, mt + 1, rt);
tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
}
void update(int id, int lt, int rt, int ql, int qr, int add) {
if (lt == ql && rt == qr) {
tree[id] += add;
lazy[id] += add;
return;
}
tree[id * 2] += lazy[id];
lazy[id * 2] += lazy[id];
tree[id * 2 + 1] += lazy[id];
lazy[id * 2 + 1] += lazy[id];
lazy[id] = 0;
int mt = (lt + rt) / 2;
if (qr <= mt) {
update(id * 2, lt, mt, ql, qr, add);
}
else if (ql >= mt + 1) {
update(id * 2 + 1, mt + 1, rt, ql, qr, add);
}
else {
update(id * 2, lt, mt, ql, mt, add);
update(id * 2 + 1, mt + 1, rt, mt + 1, qr, add);
}
tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
}
bool contains(int x, int y) {
if (x < y) {
return y - x + 1 <= k;
}
else {
return n - (x - y - 1) <= k;
}
}
void add_zero(int x) {
auto it = all_zeroes.insert(x).first;
int prv = (it == all_zeroes.begin() ? *all_zeroes.rbegin() : *prev(it));
if (!contains(prv, x)) {
good_zeroes.insert(x);
}
int nxt = (next(it) == all_zeroes.end() ? *all_zeroes.begin() : *next(it));
if (contains(x, nxt)) {
good_zeroes.erase(nxt);
}
else {
good_zeroes.insert(nxt);
}
}
void rem_zero(int x) {
auto it = all_zeroes.find(x);
int prv = (it == all_zeroes.begin() ? *all_zeroes.rbegin() : *prev(it));
int nxt = (next(it) == all_zeroes.end() ? *all_zeroes.begin() : *next(it));
if (contains(prv, nxt)) {
good_zeroes.erase(nxt);
}
else {
good_zeroes.insert(nxt);
}
int rt = x;
int lt = (rt + n - (k - 1)) % n;
if (lt <= rt) {
update(1, 0, n - 1, lt, rt, -1);
}
else {
update(1, 0, n - 1, lt, n - 1, -1);
update(1, 0, n - 1, 0, rt, -1);
}
all_zeroes.erase(it);
good_zeroes.erase(x);
}
void find_zeroes(int id, int lt, int rt) {
if (tree[id] > 0) {
return;
}
if (lt == rt) {
add_zero(lt);
tree[id] = inf;
lazy[id] = 0;
return;
}
tree[id * 2] += lazy[id];
lazy[id * 2] += lazy[id];
tree[id * 2 + 1] += lazy[id];
lazy[id * 2 + 1] += lazy[id];
lazy[id] = 0;
int mt = (lt + rt) / 2;
find_zeroes(id * 2, lt, mt);
find_zeroes(id * 2 + 1, mt + 1, rt);
tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
}
void init(int _k, vector<int> _a) {
n = _a.size();
k = _k;
for (int i = 0; i < n; i++) {
a[i] = k - 1 - _a[i];
}
if (k == 2) {
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
dp_right[i] = i;
for (int j = (i + n - 1) % n; j != i; j = (j + n - 1) % n) {
dp_right[j] = (a[j] ? dp_right[(j + 1) % n] : j);
}
break;
}
}
for (int i = 0; i < n; i++) {
if (a[(i + n - 1) % n] == 1) {
dp_left[i] = i;
for (int j = (i + 1) % n; j != i; j = (j + 1) % n) {
dp_left[j] = (!a[(j + n - 1) % n] ? dp_left[(j + n - 1) % n] : j);
}
break;
}
}
}
else {
build(1, 0, n - 1);
for (int i = 0; i < n; i++) {
find_zeroes(1, 0, n - 1);
pos[*good_zeroes.begin()] = i;
rem_zero(*good_zeroes.begin());
}
}
return;
}
bool check(int x, int y) {
if (dp_left[x] == dp_right[x]) {
return (dp_left[x] != x);
}
else if (dp_left[x] < dp_right[x]) {
return dp_left[x] <= y && y <= dp_right[x];
}
else {
return (y >= dp_left[x] || y <= dp_right[x]);
}
}
int compare_plants(int x, int y) {
if (k == 2) {
if (check(x, y)) {
return 1;
}
else if (check(y, x)) {
return -1;
}
else {
return 0;
}
}
else {
return (pos[x] > pos[y] ? 1 : -1);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
39 ms |
4044 KB |
Output is correct |
7 |
Correct |
45 ms |
4828 KB |
Output is correct |
8 |
Correct |
63 ms |
7944 KB |
Output is correct |
9 |
Correct |
73 ms |
7968 KB |
Output is correct |
10 |
Correct |
62 ms |
7920 KB |
Output is correct |
11 |
Correct |
65 ms |
8032 KB |
Output is correct |
12 |
Correct |
61 ms |
7868 KB |
Output is correct |
13 |
Correct |
59 ms |
7936 KB |
Output is correct |
14 |
Correct |
59 ms |
7860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
408 KB |
Output is correct |
7 |
Correct |
43 ms |
4572 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
44 ms |
4540 KB |
Output is correct |
11 |
Correct |
42 ms |
4492 KB |
Output is correct |
12 |
Correct |
42 ms |
4800 KB |
Output is correct |
13 |
Correct |
44 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
408 KB |
Output is correct |
7 |
Correct |
43 ms |
4572 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
44 ms |
4540 KB |
Output is correct |
11 |
Correct |
42 ms |
4492 KB |
Output is correct |
12 |
Correct |
42 ms |
4800 KB |
Output is correct |
13 |
Correct |
44 ms |
4692 KB |
Output is correct |
14 |
Correct |
55 ms |
5536 KB |
Output is correct |
15 |
Correct |
198 ms |
11420 KB |
Output is correct |
16 |
Correct |
55 ms |
4848 KB |
Output is correct |
17 |
Correct |
203 ms |
11512 KB |
Output is correct |
18 |
Correct |
159 ms |
11468 KB |
Output is correct |
19 |
Correct |
209 ms |
16216 KB |
Output is correct |
20 |
Correct |
192 ms |
11488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
41 ms |
4260 KB |
Output is correct |
4 |
Correct |
160 ms |
15888 KB |
Output is correct |
5 |
Correct |
168 ms |
13684 KB |
Output is correct |
6 |
Correct |
185 ms |
13260 KB |
Output is correct |
7 |
Correct |
199 ms |
13296 KB |
Output is correct |
8 |
Correct |
202 ms |
13552 KB |
Output is correct |
9 |
Correct |
160 ms |
13228 KB |
Output is correct |
10 |
Correct |
163 ms |
13920 KB |
Output is correct |
11 |
Correct |
64 ms |
9456 KB |
Output is correct |
12 |
Correct |
152 ms |
22288 KB |
Output is correct |
13 |
Correct |
152 ms |
13032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
39 ms |
4044 KB |
Output is correct |
7 |
Correct |
45 ms |
4828 KB |
Output is correct |
8 |
Correct |
63 ms |
7944 KB |
Output is correct |
9 |
Correct |
73 ms |
7968 KB |
Output is correct |
10 |
Correct |
62 ms |
7920 KB |
Output is correct |
11 |
Correct |
65 ms |
8032 KB |
Output is correct |
12 |
Correct |
61 ms |
7868 KB |
Output is correct |
13 |
Correct |
59 ms |
7936 KB |
Output is correct |
14 |
Correct |
59 ms |
7860 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
408 KB |
Output is correct |
21 |
Correct |
43 ms |
4572 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
44 ms |
4540 KB |
Output is correct |
25 |
Correct |
42 ms |
4492 KB |
Output is correct |
26 |
Correct |
42 ms |
4800 KB |
Output is correct |
27 |
Correct |
44 ms |
4692 KB |
Output is correct |
28 |
Correct |
55 ms |
5536 KB |
Output is correct |
29 |
Correct |
198 ms |
11420 KB |
Output is correct |
30 |
Correct |
55 ms |
4848 KB |
Output is correct |
31 |
Correct |
203 ms |
11512 KB |
Output is correct |
32 |
Correct |
159 ms |
11468 KB |
Output is correct |
33 |
Correct |
209 ms |
16216 KB |
Output is correct |
34 |
Correct |
192 ms |
11488 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
41 ms |
4260 KB |
Output is correct |
38 |
Correct |
160 ms |
15888 KB |
Output is correct |
39 |
Correct |
168 ms |
13684 KB |
Output is correct |
40 |
Correct |
185 ms |
13260 KB |
Output is correct |
41 |
Correct |
199 ms |
13296 KB |
Output is correct |
42 |
Correct |
202 ms |
13552 KB |
Output is correct |
43 |
Correct |
160 ms |
13228 KB |
Output is correct |
44 |
Correct |
163 ms |
13920 KB |
Output is correct |
45 |
Correct |
64 ms |
9456 KB |
Output is correct |
46 |
Correct |
152 ms |
22288 KB |
Output is correct |
47 |
Correct |
152 ms |
13032 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
53 |
Halted |
0 ms |
0 KB |
- |