#include<bits/stdc++.h>
using namespace std;
#include "plants.h"
const int N = 2e5 + 10, M = 20;
int p[2 * N], rev_p[N];
int f[M + 1][2 * N], g[M + 1][2 * N];
struct IT1 {
int T[N << 2], lazy[N << 2];;
void push(int s, int l, int r) {
if (!lazy[s]) return;
T[s] += lazy[s];
if (l != r) {
lazy[s << 1] += lazy[s];
lazy[s << 1 | 1] += lazy[s];
}
lazy[s] = 0;
}
void up(int s, int l, int r, int from, int to, int val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[s] = val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, val);
up(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = min(T[s << 1], T[s << 1 | 1]);
}
int get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from || T[s]) return -1;
if (l == r) return l;
int mid = l + r >> 1;
int ret = get(s << 1 | 1, mid + 1, r, from, to);
if (ret >= 0) return ret;
return get(s << 1, l, mid, from, to);
}
} it1;
struct IT2 {
int T[N << 3];
void up(int s, int l, int r, int pos, int val) {
if (l == r) {
T[s] = val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) up(s << 1, l, mid, pos, val);
else up(s << 1 | 1, mid + 1, r, pos, val);
T[s] = max(T[s << 1], T[s << 1 | 1]);
}
int get(int s, int l, int r, int from, int to) {
if (l > to || r < from) return -1;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return max(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}
} it2;
int n;
int move_right (int i, int val) {
for(int j = M; j >= 0; j--) {
if (f[j][i] >= 0 && p[f[j][i]] >= val) i = f[j][i];
}
return i;
}
int move_left (int i, int val) {
for(int j = M; j >= 0; j--) {
if (g[j][i] >= 0 && p[g[j][i]] >= val) i = g[j][i];
}
return i;
}
void init(int k, std::vector<int> r) {
n = r.size();
k--;
for(int i = 0; i < n; i++) it1.up(1, 0, n - 1, i, i, r[i]);
int val = n - 1;
auto calc = [&] () {
int m = n;
vector<int> ret(m);
for(int i = 0; i < m; i++) {
for(int j = i; j <= i + k; j++) ret[i] += p[i] < p[j % m];
}
return ret;
};
auto dfs = [&] (auto self, int i) -> void {
while (1) {
int left = it1.get(1, 0, n - 1, max(0, i - k), i - 1);
if (left >= 0) self(self, left);
else if (i - k < 0) {
left = it1.get(1, 0, n - 1, n + i - k, n - 1);
if (left >= 0) self(self, left);
else break;
}
else break;
}
p[i] = val--;
it1.up(1, 0, n - 1, i, i, 1e9);
it1.up(1, 0, n - 1, max(0, i - k), i - 1, -1);
if (i - k < 0) it1.up(1, 0, n - 1, n + i - k, n - 1, -1);
};
while (1) {
int pos = it1.get(1, 0, n - 1, 0, n - 1);
if (pos < 0) break;
dfs(dfs, pos);
}
for(int i = 0; i < n; i++) rev_p[p[i]] = i;
for(int i = 0; i < n; i++) p[i + n] = p[i];
for(int i = 0; i <= 4 * 2 * n; i++) it2.T[i] = -1;
for(int val = 0; val < n; val++) {
int largest;
int i = rev_p[val];
// cout << i << endl;
largest = it2.get(1, 0, 2 * n - 1, i + 1, min(2 * n - 1, i + k));
f[0][i] = largest >= 0 ? rev_p[largest] : -1;
largest = it2.get(1, 0, 2 * n - 1, max(0, i - k), i - 1);
g[0][i] = largest >= 0 ? rev_p[largest] : -1;
it2.up(1, 0, 2 * n - 1, i, val);
if (f[0][i] != -1 && f[0][i] < i) f[0][i] += n;
if (g[0][i] != -1 && g[0][i] + n < i) g[0][i] += n;
i += n;
largest = it2.get(1, 0, 2 * n - 1, i + 1, min(2 * n - 1, i + k));
f[0][i] = largest >= 0 ? rev_p[largest]: -1;
largest = it2.get(1, 0, 2 * n - 1, max(0, i - k), i - 1);
g[0][i] = largest >= 0 ? rev_p[largest] : -1;
it2.up(1, 0, 2 * n - 1, i, val);
if (f[0][i] != -1 && f[0][i] < i) f[0][i] += n;
if (g[0][i] != -1 && g[0][i] + n < i) g[0][i] += n;
}
for(int j = 1; j <= M; j++) for(int i = 0; i < 2 * n; i++) {
if (f[j - 1][i] < 0) f[j][i] = -1;
else f[j][i] = f[j - 1][f[j - 1][i]];
if (g[j - 1][i] < 0) g[j][i] = -1;
else g[j][i] = g[j - 1][g[j - 1][i]];
}
}
int compare_plants(int x, int y) {
int ret = 1;
if (p[x] < p[y]) ret = -1, swap(x, y);
if (x < y) x += n;
if (move_left(x, p[y]) <= y) return ret;
if (move_right(x, p[y]) >= y + n) return ret;
return 0;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
static int n, k, q;
static std::vector<int> r;
static std:: vector<int> x;
static std:: vector<int> y;
static std:: vector<int> answer;
assert(scanf("%d%d%d", &n, &k, &q) == 3);
r.resize(n);
answer.resize(q);
for (int i = 0; i < n; i++) {
int value;
assert(scanf("%d", &value) == 1);
r[i] = value;
}
x.resize(q);
y.resize(q);
for (int i = 0; i < q; i++) {
assert(scanf("%d%d", &x[i], &y[i]) == 2);
}
fclose(stdin);
init(k, r);
for (int i = 0; i < q; i++) {
answer[i] = compare_plants(x[i], y[i]);
}
for (int i = 0; i < q; i++) {
printf("%d\n", answer[i]);
}
fclose(stdout);
return 0;
}
#endif // ngu
Compilation message
plants.cpp: In member function 'void IT1::up(int, int, int, int, int, int)':
plants.cpp:32:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In member function 'int IT1::get(int, int, int, int, int)':
plants.cpp:42:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In member function 'void IT2::up(int, int, int, int, int)':
plants.cpp:58:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In member function 'int IT2::get(int, int, int, int, int)':
plants.cpp:67:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:95:8: warning: variable 'calc' set but not used [-Wunused-but-set-variable]
95 | auto calc = [&] () {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
67932 KB |
Output is correct |
2 |
Correct |
8 ms |
68108 KB |
Output is correct |
3 |
Correct |
8 ms |
69980 KB |
Output is correct |
4 |
Correct |
8 ms |
69980 KB |
Output is correct |
5 |
Correct |
8 ms |
67932 KB |
Output is correct |
6 |
Correct |
52 ms |
70744 KB |
Output is correct |
7 |
Correct |
103 ms |
75600 KB |
Output is correct |
8 |
Correct |
455 ms |
82212 KB |
Output is correct |
9 |
Correct |
434 ms |
85332 KB |
Output is correct |
10 |
Correct |
445 ms |
85844 KB |
Output is correct |
11 |
Correct |
507 ms |
88932 KB |
Output is correct |
12 |
Correct |
517 ms |
96552 KB |
Output is correct |
13 |
Correct |
627 ms |
107088 KB |
Output is correct |
14 |
Correct |
461 ms |
85332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
68184 KB |
Output is correct |
2 |
Correct |
8 ms |
68184 KB |
Output is correct |
3 |
Correct |
8 ms |
67932 KB |
Output is correct |
4 |
Correct |
8 ms |
67932 KB |
Output is correct |
5 |
Correct |
8 ms |
69980 KB |
Output is correct |
6 |
Correct |
10 ms |
70236 KB |
Output is correct |
7 |
Correct |
71 ms |
75344 KB |
Output is correct |
8 |
Correct |
9 ms |
70236 KB |
Output is correct |
9 |
Correct |
11 ms |
70300 KB |
Output is correct |
10 |
Correct |
67 ms |
75272 KB |
Output is correct |
11 |
Correct |
84 ms |
73300 KB |
Output is correct |
12 |
Correct |
74 ms |
75348 KB |
Output is correct |
13 |
Correct |
65 ms |
75344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
68184 KB |
Output is correct |
2 |
Correct |
8 ms |
68184 KB |
Output is correct |
3 |
Correct |
8 ms |
67932 KB |
Output is correct |
4 |
Correct |
8 ms |
67932 KB |
Output is correct |
5 |
Correct |
8 ms |
69980 KB |
Output is correct |
6 |
Correct |
10 ms |
70236 KB |
Output is correct |
7 |
Correct |
71 ms |
75344 KB |
Output is correct |
8 |
Correct |
9 ms |
70236 KB |
Output is correct |
9 |
Correct |
11 ms |
70300 KB |
Output is correct |
10 |
Correct |
67 ms |
75272 KB |
Output is correct |
11 |
Correct |
84 ms |
73300 KB |
Output is correct |
12 |
Correct |
74 ms |
75348 KB |
Output is correct |
13 |
Correct |
65 ms |
75344 KB |
Output is correct |
14 |
Correct |
103 ms |
77644 KB |
Output is correct |
15 |
Correct |
614 ms |
82888 KB |
Output is correct |
16 |
Correct |
102 ms |
79844 KB |
Output is correct |
17 |
Correct |
608 ms |
86420 KB |
Output is correct |
18 |
Correct |
667 ms |
97108 KB |
Output is correct |
19 |
Correct |
547 ms |
86500 KB |
Output is correct |
20 |
Correct |
571 ms |
86588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
67928 KB |
Output is correct |
2 |
Correct |
8 ms |
69980 KB |
Output is correct |
3 |
Correct |
78 ms |
71168 KB |
Output is correct |
4 |
Correct |
723 ms |
88008 KB |
Output is correct |
5 |
Correct |
737 ms |
87004 KB |
Output is correct |
6 |
Correct |
624 ms |
86356 KB |
Output is correct |
7 |
Correct |
589 ms |
86424 KB |
Output is correct |
8 |
Correct |
653 ms |
86584 KB |
Output is correct |
9 |
Correct |
584 ms |
88660 KB |
Output is correct |
10 |
Correct |
608 ms |
88132 KB |
Output is correct |
11 |
Correct |
613 ms |
106972 KB |
Output is correct |
12 |
Correct |
452 ms |
85884 KB |
Output is correct |
13 |
Correct |
718 ms |
101204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
67928 KB |
Output is correct |
2 |
Correct |
8 ms |
67932 KB |
Output is correct |
3 |
Correct |
8 ms |
68092 KB |
Output is correct |
4 |
Correct |
8 ms |
69980 KB |
Output is correct |
5 |
Correct |
8 ms |
69980 KB |
Output is correct |
6 |
Correct |
9 ms |
70236 KB |
Output is correct |
7 |
Correct |
19 ms |
70616 KB |
Output is correct |
8 |
Correct |
19 ms |
70936 KB |
Output is correct |
9 |
Correct |
20 ms |
68700 KB |
Output is correct |
10 |
Correct |
19 ms |
70748 KB |
Output is correct |
11 |
Correct |
20 ms |
70748 KB |
Output is correct |
12 |
Correct |
20 ms |
70748 KB |
Output is correct |
13 |
Correct |
19 ms |
70748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
67932 KB |
Output is correct |
2 |
Correct |
8 ms |
67932 KB |
Output is correct |
3 |
Correct |
8 ms |
69980 KB |
Output is correct |
4 |
Correct |
8 ms |
67928 KB |
Output is correct |
5 |
Correct |
10 ms |
70236 KB |
Output is correct |
6 |
Correct |
558 ms |
83180 KB |
Output is correct |
7 |
Correct |
576 ms |
85492 KB |
Output is correct |
8 |
Correct |
554 ms |
85572 KB |
Output is correct |
9 |
Correct |
589 ms |
85628 KB |
Output is correct |
10 |
Correct |
519 ms |
87920 KB |
Output is correct |
11 |
Correct |
488 ms |
86680 KB |
Output is correct |
12 |
Correct |
537 ms |
91196 KB |
Output is correct |
13 |
Correct |
549 ms |
86612 KB |
Output is correct |
14 |
Correct |
576 ms |
85428 KB |
Output is correct |
15 |
Correct |
589 ms |
85484 KB |
Output is correct |
16 |
Correct |
430 ms |
88016 KB |
Output is correct |
17 |
Correct |
498 ms |
85612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
67932 KB |
Output is correct |
2 |
Correct |
8 ms |
68108 KB |
Output is correct |
3 |
Correct |
8 ms |
69980 KB |
Output is correct |
4 |
Correct |
8 ms |
69980 KB |
Output is correct |
5 |
Correct |
8 ms |
67932 KB |
Output is correct |
6 |
Correct |
52 ms |
70744 KB |
Output is correct |
7 |
Correct |
103 ms |
75600 KB |
Output is correct |
8 |
Correct |
455 ms |
82212 KB |
Output is correct |
9 |
Correct |
434 ms |
85332 KB |
Output is correct |
10 |
Correct |
445 ms |
85844 KB |
Output is correct |
11 |
Correct |
507 ms |
88932 KB |
Output is correct |
12 |
Correct |
517 ms |
96552 KB |
Output is correct |
13 |
Correct |
627 ms |
107088 KB |
Output is correct |
14 |
Correct |
461 ms |
85332 KB |
Output is correct |
15 |
Correct |
8 ms |
68184 KB |
Output is correct |
16 |
Correct |
8 ms |
68184 KB |
Output is correct |
17 |
Correct |
8 ms |
67932 KB |
Output is correct |
18 |
Correct |
8 ms |
67932 KB |
Output is correct |
19 |
Correct |
8 ms |
69980 KB |
Output is correct |
20 |
Correct |
10 ms |
70236 KB |
Output is correct |
21 |
Correct |
71 ms |
75344 KB |
Output is correct |
22 |
Correct |
9 ms |
70236 KB |
Output is correct |
23 |
Correct |
11 ms |
70300 KB |
Output is correct |
24 |
Correct |
67 ms |
75272 KB |
Output is correct |
25 |
Correct |
84 ms |
73300 KB |
Output is correct |
26 |
Correct |
74 ms |
75348 KB |
Output is correct |
27 |
Correct |
65 ms |
75344 KB |
Output is correct |
28 |
Correct |
103 ms |
77644 KB |
Output is correct |
29 |
Correct |
614 ms |
82888 KB |
Output is correct |
30 |
Correct |
102 ms |
79844 KB |
Output is correct |
31 |
Correct |
608 ms |
86420 KB |
Output is correct |
32 |
Correct |
667 ms |
97108 KB |
Output is correct |
33 |
Correct |
547 ms |
86500 KB |
Output is correct |
34 |
Correct |
571 ms |
86588 KB |
Output is correct |
35 |
Correct |
8 ms |
67928 KB |
Output is correct |
36 |
Correct |
8 ms |
69980 KB |
Output is correct |
37 |
Correct |
78 ms |
71168 KB |
Output is correct |
38 |
Correct |
723 ms |
88008 KB |
Output is correct |
39 |
Correct |
737 ms |
87004 KB |
Output is correct |
40 |
Correct |
624 ms |
86356 KB |
Output is correct |
41 |
Correct |
589 ms |
86424 KB |
Output is correct |
42 |
Correct |
653 ms |
86584 KB |
Output is correct |
43 |
Correct |
584 ms |
88660 KB |
Output is correct |
44 |
Correct |
608 ms |
88132 KB |
Output is correct |
45 |
Correct |
613 ms |
106972 KB |
Output is correct |
46 |
Correct |
452 ms |
85884 KB |
Output is correct |
47 |
Correct |
718 ms |
101204 KB |
Output is correct |
48 |
Correct |
7 ms |
67928 KB |
Output is correct |
49 |
Correct |
8 ms |
67932 KB |
Output is correct |
50 |
Correct |
8 ms |
68092 KB |
Output is correct |
51 |
Correct |
8 ms |
69980 KB |
Output is correct |
52 |
Correct |
8 ms |
69980 KB |
Output is correct |
53 |
Correct |
9 ms |
70236 KB |
Output is correct |
54 |
Correct |
19 ms |
70616 KB |
Output is correct |
55 |
Correct |
19 ms |
70936 KB |
Output is correct |
56 |
Correct |
20 ms |
68700 KB |
Output is correct |
57 |
Correct |
19 ms |
70748 KB |
Output is correct |
58 |
Correct |
20 ms |
70748 KB |
Output is correct |
59 |
Correct |
20 ms |
70748 KB |
Output is correct |
60 |
Correct |
19 ms |
70748 KB |
Output is correct |
61 |
Correct |
72 ms |
74688 KB |
Output is correct |
62 |
Correct |
107 ms |
79624 KB |
Output is correct |
63 |
Correct |
443 ms |
85604 KB |
Output is correct |
64 |
Correct |
558 ms |
85912 KB |
Output is correct |
65 |
Correct |
743 ms |
86100 KB |
Output is correct |
66 |
Correct |
655 ms |
86356 KB |
Output is correct |
67 |
Correct |
626 ms |
86612 KB |
Output is correct |
68 |
Correct |
595 ms |
89396 KB |
Output is correct |
69 |
Correct |
587 ms |
87548 KB |
Output is correct |
70 |
Correct |
744 ms |
91660 KB |
Output is correct |
71 |
Correct |
698 ms |
87160 KB |
Output is correct |
72 |
Correct |
669 ms |
86136 KB |
Output is correct |
73 |
Correct |
600 ms |
86252 KB |
Output is correct |
74 |
Correct |
498 ms |
85844 KB |
Output is correct |
75 |
Correct |
594 ms |
86608 KB |
Output is correct |