#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const int NS = (int)2e5 + 4;
int mn[NS * 4 + 4], id[NS * 4 + 4], lazy[NS * 4 + 4];
int k;
vector<int> r;
int ans[NS];
int n;
int L[NS][20], R[NS][20], LD[NS][20], RD[NS][20], bk[NS];
void update(int x, int s, int e){
if(!lazy[x] || s == e) return;
lazy[x * 2] += lazy[x], mn[x * 2] += lazy[x];
lazy[x * 2 + 1] += lazy[x], mn[x * 2 + 1] += lazy[x];
lazy[x] = 0;
}
void push(int x, int s, int e, int ps, int pe, int val){
if(pe < s || ps > e || ps > pe){
return;
}
if(ps <= s && pe >= e){
if(s == e) id[x] = s;
mn[x] += val;
lazy[x] += val;
return;
}
update(x, s, e);
int m = s + e >> 1;
push(x * 2, s, m, ps, pe, val);
push(x * 2 + 1, m + 1, e, ps, pe, val);
if(mn[x * 2] <= mn[x * 2 + 1]){
mn[x] = mn[x * 2], id[x] = id[x * 2];
}
else{
mn[x] = mn[x * 2 + 1], id[x] = id[x * 2 + 1];
}
}
pair<int, int> get(int x, int s, int e, int fs, int fe){
if(fe < s || fs > e || fs > fe){
return {(int)1e9, (int)1e9};
}
if(fs <= s && fe >= e){
return {mn[x], id[x]};
}
update(x, s, e);
int m = s + e >> 1;
return min(get(x * 2, s, m, fs, fe), get(x * 2 + 1, m + 1, e, fs, fe));
}
void init(int K, std::vector<int> RR) {
k = K;
r = RR;
n = (int)r.size();
for(int i = 0; i < n; ++i){
push(1, 0, n - 1, i, i, r[i]);
}
for(int i = n - 1; i >= 0;){
auto rv = get(1, 0, n - 1, 0, n - 1);
auto sol = [&](auto&&self, int now)->void{
while(true){
rv = get(1, 0, n - 1, now - k + 1, now - 1);
rv = min(rv, get(1, 0, n - 1, n - (k - now - 1), n - 1));
if(rv.first == 0){
self(self, rv.second);
}
else{
break;
}
}
ans[now] = i--;
bk[ans[now]] = now;
push(1, 0, n - 1, now - k + 1, now, -1);
push(1, 0, n - 1, n - (k - now) + 1, n - 1, -1);
push(1, 0, n - 1, now, now, (int)1e9);
};
sol(sol, rv.second);
}
memset(mn, 0, sizeof(mn));
memset(lazy, 0, sizeof(lazy));
for(int i = 0; i < n; ++i){
int now = bk[i];
auto rv = min(get(1, 0, n - 1, now - k + 1, now - 1), get(1, 0, n - 1, n - (k - now - 1), n - 1));
L[now][0] = (rv.first ? rv.second : -1);
if(L[now][0] != -1) LD[now][0] = (now - L[now][0] + n) % n;
rv = min(get(1, 0, n - 1, now + 1, now + k - 1), get(1, 0, n - 1, 0, k - (n - now) - 1));
R[now][0] = (rv.first ? rv.second : -1);
if(R[now][0] != -1) RD[now][0] = (R[now][0] - now + n) % n;
push(1, 0, n - 1, now, now, -i - 1);
}
for(int j = 1; j < 20; ++j){
for(int i = 0; i < n; ++i){
R[i][j] = (R[i][j - 1] == -1 ? -1 : R[R[i][j - 1]][j - 1]);
if(R[i][j] != -1) RD[i][j] = min((int)1e9, RD[i][j - 1] + RD[R[i][j - 1]][j - 1]);
L[i][j] = (L[i][j - 1] == -1 ? -1 : L[L[i][j - 1]][j - 1]);
if(L[i][j] != -1) LD[i][j] = min((int)1e9, LD[i][j - 1] + LD[L[i][j - 1]][j - 1]);
}
}
}
int compare_plants(int x, int y) {
for(int i = 0; i < 2; ++i){
int now = x;
for(int j = 19; j >= 0; --j){
if(R[now][j] != -1 && RD[now][j] < (y - now + n) % n - (k - 1)){
now = R[now][j];
}
}
if(R[now][0] != -1 && (y - now + n) % n >= k) now = R[now][0];
if((y - now + n) % n < k && ans[now] > ans[y]){
return (i ? -1 : 1);
}
now = x;
for(int j = 19; j >= 0; --j){
if(L[now][j] != -1 && LD[now][j] < (now - y + n) % n - (k - 1)){
now = L[now][j];
}
}
if(L[now][0] != -1 && (now - y + n) % n >= k) now = L[now][0];
if((now - y + n) % n < k && ans[now] > ans[y]){
return (i ? -1 : 1);
}
swap(x, y);
}
return 0;
}
Compilation message
plants.cpp: In function 'void push(int, int, int, int, int, int)':
plants.cpp:31:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int m = s + e >> 1;
| ~~^~~
plants.cpp: In function 'std::pair<int, int> get(int, int, int, int, int)':
plants.cpp:50:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int m = s + e >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
5 ms |
6612 KB |
Output is correct |
6 |
Correct |
120 ms |
9328 KB |
Output is correct |
7 |
Correct |
192 ms |
15284 KB |
Output is correct |
8 |
Correct |
537 ms |
77488 KB |
Output is correct |
9 |
Correct |
571 ms |
67792 KB |
Output is correct |
10 |
Correct |
600 ms |
62620 KB |
Output is correct |
11 |
Correct |
631 ms |
61976 KB |
Output is correct |
12 |
Correct |
623 ms |
61900 KB |
Output is correct |
13 |
Correct |
615 ms |
61812 KB |
Output is correct |
14 |
Correct |
704 ms |
61788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
7 ms |
7012 KB |
Output is correct |
7 |
Correct |
101 ms |
11072 KB |
Output is correct |
8 |
Correct |
5 ms |
6612 KB |
Output is correct |
9 |
Correct |
7 ms |
6996 KB |
Output is correct |
10 |
Correct |
103 ms |
11136 KB |
Output is correct |
11 |
Correct |
76 ms |
10884 KB |
Output is correct |
12 |
Correct |
151 ms |
11036 KB |
Output is correct |
13 |
Correct |
101 ms |
11136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
7 ms |
7012 KB |
Output is correct |
7 |
Correct |
101 ms |
11072 KB |
Output is correct |
8 |
Correct |
5 ms |
6612 KB |
Output is correct |
9 |
Correct |
7 ms |
6996 KB |
Output is correct |
10 |
Correct |
103 ms |
11136 KB |
Output is correct |
11 |
Correct |
76 ms |
10884 KB |
Output is correct |
12 |
Correct |
151 ms |
11036 KB |
Output is correct |
13 |
Correct |
101 ms |
11136 KB |
Output is correct |
14 |
Correct |
158 ms |
16256 KB |
Output is correct |
15 |
Correct |
1189 ms |
77556 KB |
Output is correct |
16 |
Correct |
162 ms |
18476 KB |
Output is correct |
17 |
Correct |
1117 ms |
81300 KB |
Output is correct |
18 |
Correct |
654 ms |
72936 KB |
Output is correct |
19 |
Correct |
797 ms |
73456 KB |
Output is correct |
20 |
Correct |
1061 ms |
81288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
138 ms |
10020 KB |
Output is correct |
4 |
Correct |
941 ms |
78456 KB |
Output is correct |
5 |
Correct |
862 ms |
77508 KB |
Output is correct |
6 |
Correct |
1091 ms |
77528 KB |
Output is correct |
7 |
Correct |
1160 ms |
77572 KB |
Output is correct |
8 |
Correct |
1186 ms |
77452 KB |
Output is correct |
9 |
Correct |
686 ms |
80636 KB |
Output is correct |
10 |
Correct |
814 ms |
80656 KB |
Output is correct |
11 |
Correct |
629 ms |
64720 KB |
Output is correct |
12 |
Correct |
817 ms |
64876 KB |
Output is correct |
13 |
Correct |
670 ms |
69736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
6 ms |
6612 KB |
Output is correct |
7 |
Correct |
31 ms |
7244 KB |
Output is correct |
8 |
Correct |
23 ms |
7244 KB |
Output is correct |
9 |
Correct |
29 ms |
7248 KB |
Output is correct |
10 |
Correct |
23 ms |
7240 KB |
Output is correct |
11 |
Correct |
32 ms |
7244 KB |
Output is correct |
12 |
Correct |
31 ms |
7244 KB |
Output is correct |
13 |
Correct |
24 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
2 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6520 KB |
Output is correct |
5 |
Correct |
6 ms |
6908 KB |
Output is correct |
6 |
Correct |
687 ms |
77564 KB |
Output is correct |
7 |
Correct |
860 ms |
77564 KB |
Output is correct |
8 |
Correct |
928 ms |
77516 KB |
Output is correct |
9 |
Correct |
1104 ms |
77428 KB |
Output is correct |
10 |
Correct |
703 ms |
79648 KB |
Output is correct |
11 |
Correct |
827 ms |
80244 KB |
Output is correct |
12 |
Correct |
694 ms |
80760 KB |
Output is correct |
13 |
Correct |
771 ms |
79948 KB |
Output is correct |
14 |
Correct |
860 ms |
80024 KB |
Output is correct |
15 |
Correct |
1085 ms |
80240 KB |
Output is correct |
16 |
Correct |
664 ms |
79568 KB |
Output is correct |
17 |
Correct |
721 ms |
79708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
5 ms |
6612 KB |
Output is correct |
6 |
Correct |
120 ms |
9328 KB |
Output is correct |
7 |
Correct |
192 ms |
15284 KB |
Output is correct |
8 |
Correct |
537 ms |
77488 KB |
Output is correct |
9 |
Correct |
571 ms |
67792 KB |
Output is correct |
10 |
Correct |
600 ms |
62620 KB |
Output is correct |
11 |
Correct |
631 ms |
61976 KB |
Output is correct |
12 |
Correct |
623 ms |
61900 KB |
Output is correct |
13 |
Correct |
615 ms |
61812 KB |
Output is correct |
14 |
Correct |
704 ms |
61788 KB |
Output is correct |
15 |
Correct |
3 ms |
6612 KB |
Output is correct |
16 |
Correct |
3 ms |
6612 KB |
Output is correct |
17 |
Correct |
3 ms |
6612 KB |
Output is correct |
18 |
Correct |
3 ms |
6612 KB |
Output is correct |
19 |
Correct |
3 ms |
6612 KB |
Output is correct |
20 |
Correct |
7 ms |
7012 KB |
Output is correct |
21 |
Correct |
101 ms |
11072 KB |
Output is correct |
22 |
Correct |
5 ms |
6612 KB |
Output is correct |
23 |
Correct |
7 ms |
6996 KB |
Output is correct |
24 |
Correct |
103 ms |
11136 KB |
Output is correct |
25 |
Correct |
76 ms |
10884 KB |
Output is correct |
26 |
Correct |
151 ms |
11036 KB |
Output is correct |
27 |
Correct |
101 ms |
11136 KB |
Output is correct |
28 |
Correct |
158 ms |
16256 KB |
Output is correct |
29 |
Correct |
1189 ms |
77556 KB |
Output is correct |
30 |
Correct |
162 ms |
18476 KB |
Output is correct |
31 |
Correct |
1117 ms |
81300 KB |
Output is correct |
32 |
Correct |
654 ms |
72936 KB |
Output is correct |
33 |
Correct |
797 ms |
73456 KB |
Output is correct |
34 |
Correct |
1061 ms |
81288 KB |
Output is correct |
35 |
Correct |
3 ms |
6612 KB |
Output is correct |
36 |
Correct |
3 ms |
6612 KB |
Output is correct |
37 |
Correct |
138 ms |
10020 KB |
Output is correct |
38 |
Correct |
941 ms |
78456 KB |
Output is correct |
39 |
Correct |
862 ms |
77508 KB |
Output is correct |
40 |
Correct |
1091 ms |
77528 KB |
Output is correct |
41 |
Correct |
1160 ms |
77572 KB |
Output is correct |
42 |
Correct |
1186 ms |
77452 KB |
Output is correct |
43 |
Correct |
686 ms |
80636 KB |
Output is correct |
44 |
Correct |
814 ms |
80656 KB |
Output is correct |
45 |
Correct |
629 ms |
64720 KB |
Output is correct |
46 |
Correct |
817 ms |
64876 KB |
Output is correct |
47 |
Correct |
670 ms |
69736 KB |
Output is correct |
48 |
Correct |
3 ms |
6612 KB |
Output is correct |
49 |
Correct |
3 ms |
6612 KB |
Output is correct |
50 |
Correct |
3 ms |
6612 KB |
Output is correct |
51 |
Correct |
3 ms |
6612 KB |
Output is correct |
52 |
Correct |
3 ms |
6612 KB |
Output is correct |
53 |
Correct |
6 ms |
6612 KB |
Output is correct |
54 |
Correct |
31 ms |
7244 KB |
Output is correct |
55 |
Correct |
23 ms |
7244 KB |
Output is correct |
56 |
Correct |
29 ms |
7248 KB |
Output is correct |
57 |
Correct |
23 ms |
7240 KB |
Output is correct |
58 |
Correct |
32 ms |
7244 KB |
Output is correct |
59 |
Correct |
31 ms |
7244 KB |
Output is correct |
60 |
Correct |
24 ms |
7252 KB |
Output is correct |
61 |
Correct |
132 ms |
11772 KB |
Output is correct |
62 |
Correct |
179 ms |
18340 KB |
Output is correct |
63 |
Correct |
644 ms |
80352 KB |
Output is correct |
64 |
Correct |
776 ms |
80576 KB |
Output is correct |
65 |
Correct |
1018 ms |
80908 KB |
Output is correct |
66 |
Correct |
999 ms |
81088 KB |
Output is correct |
67 |
Correct |
1076 ms |
81176 KB |
Output is correct |
68 |
Correct |
720 ms |
80648 KB |
Output is correct |
69 |
Correct |
945 ms |
81252 KB |
Output is correct |
70 |
Correct |
802 ms |
81516 KB |
Output is correct |
71 |
Correct |
943 ms |
80732 KB |
Output is correct |
72 |
Correct |
1016 ms |
80900 KB |
Output is correct |
73 |
Correct |
1066 ms |
81128 KB |
Output is correct |
74 |
Correct |
636 ms |
80484 KB |
Output is correct |
75 |
Correct |
730 ms |
80588 KB |
Output is correct |