#include "plants.h"
#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
using namespace std;
using pii = pair<int, int>;
const int MAXN = 2e5+1, base = (1<<18);
int n, k, when[MAXN], nr;
int ptr[MAXN], ptrr[MAXN];
vector<int> R;
pii tree_max[2*base+1];
void upd(int x) {
pii val = mp(when[x], x);
x+=base;
while(x) {
tree_max[x] = max(tree_max[x], val);
x/=2;
}
}
pii find_max(int l, int r) {
if(l>r) {
return max(find_max(l, n-1), find_max(0, r));
}
l+=base, r+=base;
pii res = tree_max[l];
if(l!=r) res = max(res, tree_max[r]);
while(l/2 != r/2) {
if(l%2==0) res = max(res, tree_max[l+1]);
if(r%2==1) res = max(res, tree_max[r-1]);
l /= 2;
r /= 2;
}
return res;
}
int tree[2*base+1], push[2*base+1];
void ADD(int id, int val) {
tree[id] += val;
push[id] += val;
}
void add(int l, int r, int val, int id=1, int rl=0, int rr=base-1) {
if(l>r) {
add(l, n-1, val), add(0, r, val);
return;
}
if(l>rr || r<rl) return;
if(l<=rl && r>=rr) {
ADD(id, val);
return;
}
ADD(id*2, push[id]), ADD(id*2+1, push[id]);
push[id] = 0;
add(l, r, val, id*2, rl, (rl+rr)/2), add(l, r, val, id*2+1, (rl+rr)/2+1, rr);
tree[id] = min(tree[id*2], tree[id*2+1]);
}
int find_closest_zero(int l, int r, int id=1, int rl=0, int rr=base-1) {
if(l>rr || r<rl || tree[id]>0) return -1;
if(rl==rr) {
return rl;
}
ADD(id*2, push[id]), ADD(id*2+1, push[id]);
push[id] = 0;
int rs = find_closest_zero(l, r, id*2+1, (rl+rr)/2+1, rr);
if(rs!=-1) return rs;
return find_closest_zero(l, r, id*2, rl, (rl+rr)/2);
}
int clw_dist(int x, int y) {
return (x<=y)? y-x: n-(x-y);
}
void solve(int x) {
//cerr<<"solve("<<x<<")\n";
while(true) {
int prv = -1;
if(x>0) {
prv = find_closest_zero(0, x-1);
}
if(x<n-1 && prv==-1) {
prv = find_closest_zero(x+1, n-1);
}
if(prv != -1 && clw_dist(prv, x) < k) {
//cerr<<"->";
solve(prv);
}
else {
break;
}
}
when[x] = ++nr;
ptr[x] = find_max((x+1)%n, (x+k-1)%n).second;
ptrr[x] = find_max((x-(k-1)+n)%n, (x-1+n)%n).second;
add((x-(k-1)+n)%n, (x-1+n)%n, -1);
add(x, x, n);
upd(x);
}
void prep_jumps();
void init(int _k, vector<int> _r) {
k = _k, R = _r, n = R.size();
for(int i=0; i<2*base; ++i) {
tree_max[i] = mp(-1, -1);
}
for(int i=0; i<n; ++i) {
tree[i+base] = R[i];
}
for(int i=base-1; i>=1; --i) {
tree[i] = min(tree[i*2], tree[i*2+1]);
}
while(nr < n) {
int cur = find_closest_zero(0, n-1);
assert(cur != -1);
solve(cur);
}
prep_jumps();
}
const int L = 19;
pii jump[L][MAXN], jumpr[L][MAXN];
void prep_jumps() {
for(int i=0; i<n; ++i) {
jump[0][i] = mp(ptr[i], clw_dist(i, ptr[i]));
jumpr[0][i] = mp(ptrr[i], clw_dist(ptrr[i], i));
}
for(int l=1; l<L; ++l) {
for(int i=0; i<n; ++i) {
if(jump[l-1][i].st != -1) {
jump[l][i] = jump[l-1][jump[l-1][i].st];
jump[l][i].nd += jump[l-1][i].nd;
jump[l][i].nd = min(n, jump[l][i].nd);
}
else {
jump[l][i] = mp(-1, 1);
}
if(jumpr[l-1][i].st != -1) {
jumpr[l][i] = jumpr[l-1][jumpr[l-1][i].st];
jumpr[l][i].nd += jumpr[l-1][i].nd;
jumpr[l][i].nd = min(n, jumpr[l][i].nd);
}
else {
jumpr[l][i] = mp(-1, 1);
}
}
}
}
bool is_bigger(int _x, int y) {
int x = _x;
for(int l=L-1; l>=0; --l) {
if(jump[l][x].st!=-1 && jump[l][x].nd <= clw_dist(x, y)) {
x = jump[l][x].st;
}
}
if(clw_dist(x, y)<k && when[x] >= when[y]) return true;
x = _x;
for(int l=L-1; l>=0; --l) {
if(jumpr[l][x].st!=-1 && jumpr[l][x].nd <= clw_dist(y, x)) {
x = jumpr[l][x].st;
}
}
if(clw_dist(y, x)<k && when[x] >= when[y]) return true;
return false;
}
int compare_plants(int x, int y) {
if(is_bigger(x, y)) return -1;
if(is_bigger(y, x)) return 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
4 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5740 KB |
Output is correct |
5 |
Correct |
4 ms |
5740 KB |
Output is correct |
6 |
Correct |
87 ms |
8684 KB |
Output is correct |
7 |
Correct |
241 ms |
15468 KB |
Output is correct |
8 |
Correct |
615 ms |
74604 KB |
Output is correct |
9 |
Correct |
678 ms |
74732 KB |
Output is correct |
10 |
Correct |
744 ms |
74988 KB |
Output is correct |
11 |
Correct |
820 ms |
76780 KB |
Output is correct |
12 |
Correct |
844 ms |
83816 KB |
Output is correct |
13 |
Correct |
1020 ms |
89964 KB |
Output is correct |
14 |
Correct |
784 ms |
77292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
4 ms |
5740 KB |
Output is correct |
4 |
Correct |
4 ms |
5740 KB |
Output is correct |
5 |
Correct |
5 ms |
5868 KB |
Output is correct |
6 |
Correct |
11 ms |
6124 KB |
Output is correct |
7 |
Correct |
162 ms |
10348 KB |
Output is correct |
8 |
Correct |
7 ms |
5868 KB |
Output is correct |
9 |
Correct |
9 ms |
6124 KB |
Output is correct |
10 |
Correct |
162 ms |
10396 KB |
Output is correct |
11 |
Correct |
169 ms |
10368 KB |
Output is correct |
12 |
Correct |
110 ms |
10476 KB |
Output is correct |
13 |
Correct |
162 ms |
10348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
4 ms |
5740 KB |
Output is correct |
4 |
Correct |
4 ms |
5740 KB |
Output is correct |
5 |
Correct |
5 ms |
5868 KB |
Output is correct |
6 |
Correct |
11 ms |
6124 KB |
Output is correct |
7 |
Correct |
162 ms |
10348 KB |
Output is correct |
8 |
Correct |
7 ms |
5868 KB |
Output is correct |
9 |
Correct |
9 ms |
6124 KB |
Output is correct |
10 |
Correct |
162 ms |
10396 KB |
Output is correct |
11 |
Correct |
169 ms |
10368 KB |
Output is correct |
12 |
Correct |
110 ms |
10476 KB |
Output is correct |
13 |
Correct |
162 ms |
10348 KB |
Output is correct |
14 |
Correct |
301 ms |
15340 KB |
Output is correct |
15 |
Correct |
1661 ms |
74348 KB |
Output is correct |
16 |
Correct |
285 ms |
15340 KB |
Output is correct |
17 |
Correct |
1629 ms |
74436 KB |
Output is correct |
18 |
Correct |
1101 ms |
80748 KB |
Output is correct |
19 |
Correct |
973 ms |
74348 KB |
Output is correct |
20 |
Correct |
1682 ms |
74348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
127 ms |
9324 KB |
Output is correct |
4 |
Correct |
954 ms |
77508 KB |
Output is correct |
5 |
Correct |
966 ms |
75372 KB |
Output is correct |
6 |
Correct |
1211 ms |
74732 KB |
Output is correct |
7 |
Correct |
1384 ms |
74604 KB |
Output is correct |
8 |
Correct |
1583 ms |
74604 KB |
Output is correct |
9 |
Correct |
991 ms |
76352 KB |
Output is correct |
10 |
Correct |
1004 ms |
76140 KB |
Output is correct |
11 |
Correct |
1012 ms |
87276 KB |
Output is correct |
12 |
Correct |
846 ms |
74604 KB |
Output is correct |
13 |
Correct |
1122 ms |
83436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
4 ms |
5740 KB |
Output is correct |
4 |
Correct |
4 ms |
5740 KB |
Output is correct |
5 |
Correct |
4 ms |
5740 KB |
Output is correct |
6 |
Correct |
7 ms |
5868 KB |
Output is correct |
7 |
Correct |
29 ms |
6892 KB |
Output is correct |
8 |
Correct |
25 ms |
6508 KB |
Output is correct |
9 |
Correct |
28 ms |
6764 KB |
Output is correct |
10 |
Correct |
25 ms |
6764 KB |
Output is correct |
11 |
Correct |
28 ms |
6764 KB |
Output is correct |
12 |
Correct |
28 ms |
6764 KB |
Output is correct |
13 |
Correct |
27 ms |
6784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
4 ms |
5740 KB |
Output is correct |
4 |
Correct |
4 ms |
5752 KB |
Output is correct |
5 |
Correct |
8 ms |
6124 KB |
Output is correct |
6 |
Correct |
757 ms |
74680 KB |
Output is correct |
7 |
Correct |
750 ms |
74608 KB |
Output is correct |
8 |
Correct |
1095 ms |
74604 KB |
Output is correct |
9 |
Correct |
969 ms |
74732 KB |
Output is correct |
10 |
Correct |
653 ms |
76140 KB |
Output is correct |
11 |
Correct |
828 ms |
77804 KB |
Output is correct |
12 |
Correct |
677 ms |
80236 KB |
Output is correct |
13 |
Correct |
809 ms |
77548 KB |
Output is correct |
14 |
Correct |
883 ms |
76908 KB |
Output is correct |
15 |
Correct |
899 ms |
77036 KB |
Output is correct |
16 |
Correct |
684 ms |
78316 KB |
Output is correct |
17 |
Correct |
687 ms |
76908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5740 KB |
Output is correct |
2 |
Correct |
4 ms |
5740 KB |
Output is correct |
3 |
Correct |
4 ms |
5760 KB |
Output is correct |
4 |
Correct |
4 ms |
5740 KB |
Output is correct |
5 |
Correct |
4 ms |
5740 KB |
Output is correct |
6 |
Correct |
87 ms |
8684 KB |
Output is correct |
7 |
Correct |
241 ms |
15468 KB |
Output is correct |
8 |
Correct |
615 ms |
74604 KB |
Output is correct |
9 |
Correct |
678 ms |
74732 KB |
Output is correct |
10 |
Correct |
744 ms |
74988 KB |
Output is correct |
11 |
Correct |
820 ms |
76780 KB |
Output is correct |
12 |
Correct |
844 ms |
83816 KB |
Output is correct |
13 |
Correct |
1020 ms |
89964 KB |
Output is correct |
14 |
Correct |
784 ms |
77292 KB |
Output is correct |
15 |
Correct |
4 ms |
5740 KB |
Output is correct |
16 |
Correct |
4 ms |
5740 KB |
Output is correct |
17 |
Correct |
4 ms |
5740 KB |
Output is correct |
18 |
Correct |
4 ms |
5740 KB |
Output is correct |
19 |
Correct |
5 ms |
5868 KB |
Output is correct |
20 |
Correct |
11 ms |
6124 KB |
Output is correct |
21 |
Correct |
162 ms |
10348 KB |
Output is correct |
22 |
Correct |
7 ms |
5868 KB |
Output is correct |
23 |
Correct |
9 ms |
6124 KB |
Output is correct |
24 |
Correct |
162 ms |
10396 KB |
Output is correct |
25 |
Correct |
169 ms |
10368 KB |
Output is correct |
26 |
Correct |
110 ms |
10476 KB |
Output is correct |
27 |
Correct |
162 ms |
10348 KB |
Output is correct |
28 |
Correct |
301 ms |
15340 KB |
Output is correct |
29 |
Correct |
1661 ms |
74348 KB |
Output is correct |
30 |
Correct |
285 ms |
15340 KB |
Output is correct |
31 |
Correct |
1629 ms |
74436 KB |
Output is correct |
32 |
Correct |
1101 ms |
80748 KB |
Output is correct |
33 |
Correct |
973 ms |
74348 KB |
Output is correct |
34 |
Correct |
1682 ms |
74348 KB |
Output is correct |
35 |
Correct |
4 ms |
5740 KB |
Output is correct |
36 |
Correct |
4 ms |
5740 KB |
Output is correct |
37 |
Correct |
127 ms |
9324 KB |
Output is correct |
38 |
Correct |
954 ms |
77508 KB |
Output is correct |
39 |
Correct |
966 ms |
75372 KB |
Output is correct |
40 |
Correct |
1211 ms |
74732 KB |
Output is correct |
41 |
Correct |
1384 ms |
74604 KB |
Output is correct |
42 |
Correct |
1583 ms |
74604 KB |
Output is correct |
43 |
Correct |
991 ms |
76352 KB |
Output is correct |
44 |
Correct |
1004 ms |
76140 KB |
Output is correct |
45 |
Correct |
1012 ms |
87276 KB |
Output is correct |
46 |
Correct |
846 ms |
74604 KB |
Output is correct |
47 |
Correct |
1122 ms |
83436 KB |
Output is correct |
48 |
Correct |
4 ms |
5740 KB |
Output is correct |
49 |
Correct |
4 ms |
5740 KB |
Output is correct |
50 |
Correct |
4 ms |
5740 KB |
Output is correct |
51 |
Correct |
4 ms |
5740 KB |
Output is correct |
52 |
Correct |
4 ms |
5740 KB |
Output is correct |
53 |
Correct |
7 ms |
5868 KB |
Output is correct |
54 |
Correct |
29 ms |
6892 KB |
Output is correct |
55 |
Correct |
25 ms |
6508 KB |
Output is correct |
56 |
Correct |
28 ms |
6764 KB |
Output is correct |
57 |
Correct |
25 ms |
6764 KB |
Output is correct |
58 |
Correct |
28 ms |
6764 KB |
Output is correct |
59 |
Correct |
28 ms |
6764 KB |
Output is correct |
60 |
Correct |
27 ms |
6784 KB |
Output is correct |
61 |
Correct |
131 ms |
10988 KB |
Output is correct |
62 |
Correct |
270 ms |
17388 KB |
Output is correct |
63 |
Correct |
783 ms |
77292 KB |
Output is correct |
64 |
Correct |
976 ms |
77676 KB |
Output is correct |
65 |
Correct |
1195 ms |
77932 KB |
Output is correct |
66 |
Correct |
1391 ms |
77932 KB |
Output is correct |
67 |
Correct |
1608 ms |
78060 KB |
Output is correct |
68 |
Correct |
961 ms |
79448 KB |
Output is correct |
69 |
Correct |
977 ms |
78700 KB |
Output is correct |
70 |
Correct |
1002 ms |
80748 KB |
Output is correct |
71 |
Correct |
1247 ms |
78444 KB |
Output is correct |
72 |
Correct |
1275 ms |
77804 KB |
Output is correct |
73 |
Correct |
1341 ms |
77804 KB |
Output is correct |
74 |
Correct |
803 ms |
77440 KB |
Output is correct |
75 |
Correct |
984 ms |
77948 KB |
Output is correct |