#include "plants.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <map>
typedef long long llong;
const int MAXN = 200000 + 10;
const int MAXLOG = 18;
const int INF = 2e9;
int n, k;
struct SegmentTree
{
struct Node
{
int min;
int idx;
int lazy;
Node()
{
min = INF;
idx = -1;
lazy = 0;
}
friend Node operator + (const Node &left, const Node &right)
{
if (left.min < right.min)
{
return left;
} else
{
return right;
}
}
};
Node tree[4*MAXN];
void build(int l, int r, int node, int vals[])
{
if (l == r)
{
tree[node].min = vals[l];
tree[node].idx = l;
return;
}
int mid = l + r >> 1;
build(l, mid, 2*node, vals);
build(mid + 1, r, 2*node + 1, vals);
tree[node] = tree[2*node] + tree[2*node + 1];
}
void push(int node, int l, int r)
{
if (tree[node].lazy == 0)
{
return;
}
tree[node].min += tree[node].lazy;
if (l < r)
{
tree[2*node].lazy += tree[node].lazy;
tree[2*node + 1].lazy += tree[node].lazy;
}
tree[node].lazy = 0;
}
void update(int l, int r, int node, int queryL, int queryR, int queryVal)
{
push(node, l, r);
if (queryR < l || r < queryL)
{
return;
}
if (queryL <= l && r <= queryR)
{
tree[node].lazy = queryVal;
push(node, l, r);
return;
}
int mid = l + r >> 1;
update(l, mid, 2*node, queryL, queryR, queryVal);
update(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
tree[node] = tree[2*node] + tree[2*node + 1];
}
Node query(int l, int r, int node, int queryL, int queryR)
{
push(node, l, r);
if (queryL <= l && r <= queryR)
{
return tree[node];
}
Node result;
int mid = l + r >> 1;
if (queryL <= mid) result = result + query(l, mid, 2*node, queryL, queryR);
if (mid + 1 <= queryR) result = result + query(mid + 1, r, 2*node + 1, queryL, queryR);
return result;
}
void build(int r[])
{
build(1, n, 1, r);
}
void update(int l, int r, int val)
{
update(1, n, 1, l, r, val);
}
Node query(int l, int r)
{
return query(1, n, 1, l, r);
}
};
struct Fenwick
{
int tree[MAXN];
void reset()
{
std::fill(tree + 1, tree + 1 + n, 0);
}
void update(int idx, int val)
{
for (; idx <= n ; idx += idx & (-idx))
{
tree[idx] += val;
}
}
int query(int idx)
{
int res = 0;
for (; idx ; idx -= idx & (-idx))
{
res += tree[idx];
}
return res;
}
int kth(int k)
{
int idx = 0;
for (int lg = MAXLOG - 1 ; lg >= 0 ; --lg)
{
if (idx + (1 << lg) <= n && tree[idx + (1 << lg)] < k)
{
idx += (1 << lg);
k -= tree[idx];
}
}
return idx + 1;
}
};
int h[2 * MAXN];
struct Sparse
{
int sparse[MAXLOG + 1][2 * MAXN];
void build(int jump[])
{
for (int i = 1 ; i <= 2 * n ; ++i)
{
sparse[0][i] = jump[i];
}
for (int lg = 1 ; (1 << lg) <= 2 * n ; ++lg)
{
for (int u = 1 ; u <= 2 * n ; ++u)
{
sparse[lg][u] = sparse[lg - 1][sparse[lg - 1][u]];
}
}
}
bool jumpLeft(int from, int to)
{
for (int lg = MAXLOG ; lg >= 0 ; --lg)
{
if (sparse[lg][from] != 0 && sparse[lg][from] >= to)
{
from = sparse[lg][from];
}
}
if (from - k + 1 <= to && h[from] >= h[to]) return true;
return false;
}
bool jumpRight(int from, int to)
{
for (int lg = MAXLOG ; lg >= 0 ; --lg)
{
if (sparse[lg][from] != 0 && sparse[lg][from] <= to)
{
from = sparse[lg][from];
}
}
if (from + k - 1 >= to && h[from] >= h[to]) return true;
return false;
}
};
Fenwick fenwick;
SegmentTree tree;
Sparse sparsePrev;
Sparse sparseNext;
int r[MAXN];
int prev[2 * MAXN];
int next[2 * MAXN];
int ptrH;
void rec(int idx)
{
while (true)
{
SegmentTree::Node res;
if (idx > 1) res = res + tree.query(std::max(1, idx - k + 1), idx - 1);
if (idx < k) res = res + tree.query(n - (k - idx) + 1, n);
if (res.min == 0)
{
rec(res.idx);
} else
{
break;
}
}
h[idx] = ptrH--;
tree.update(idx, idx, 2 * n);
if (idx > 1) tree.update(std::max(1, idx - k + 1), idx, -1);
if (idx < k) tree.update(n - (k - idx) + 1, n, -1);
}
std::map <int,int> toIndex;
void init(int K, std::vector <int> R)
{
k = K;
n = R.size();
for (int i = 1 ; i <= n ; ++i)
{
r[i] = R[i - 1];
}
ptrH = n;
tree.build(r);
while (ptrH > 0)
{
SegmentTree::Node res = tree.query(1, n);
assert(res.min == 0);
rec(res.idx);
}
for (int i = n + 1 ; i <= 2 * n ; ++i)
{
h[i] = h[i - n];
}
for (int i = 1 ; i < k ; ++i)
{
toIndex[h[i]] = i;
fenwick.update(h[i], 1);
}
for (int i = k ; i <= 2 * n ; ++i)
{
if (fenwick.query(h[i]) == 0)
{
prev[i] = 0;
} else
{
prev[i] = toIndex[fenwick.kth(fenwick.query(h[i]))];
}
toIndex[h[i - k + 1]] = 0;
toIndex[h[i]] = i;
fenwick.update(h[i - k + 1], -1);
fenwick.update(h[i], 1);
}
toIndex.clear();
fenwick.reset();
for (int i = 2 * n ; i > 2 * n - k + 1 ; --i)
{
toIndex[h[i]] = i;
fenwick.update(h[i], 1);
}
for (int i = 2 * n - k + 1 ; i >= 1 ; --i)
{
if (fenwick.query(h[i]) == 0)
{
next[i] = 0;
} else
{
next[i] = toIndex[fenwick.kth(fenwick.query(h[i]))];
}
toIndex[h[i + k - 1]] = 0;
toIndex[h[i]] = i;
fenwick.update(h[i + k - 1], -1);
fenwick.update(h[i], 1);
}
sparsePrev.build(prev);
sparseNext.build(next);
}
int compare_plants(int x, int y)
{
x++; y++;
if (sparsePrev.jumpLeft(x + n, y) || sparseNext.jumpRight(x, y)) return 1;
if (sparsePrev.jumpLeft(y, x) || sparseNext.jumpRight(y, n + x)) return -1;
return 0;
}
Compilation message
plants.cpp: In member function 'void SegmentTree::build(int, int, int, int*)':
plants.cpp:52:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In member function 'void SegmentTree::update(int, int, int, int, int, int)':
plants.cpp:90:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int mid = l + r >> 1;
| ~~^~~
plants.cpp: In member function 'SegmentTree::Node SegmentTree::query(int, int, int, int, int)':
plants.cpp:105:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
105 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
54 ms |
13596 KB |
Output is correct |
7 |
Correct |
116 ms |
21716 KB |
Output is correct |
8 |
Correct |
442 ms |
91220 KB |
Output is correct |
9 |
Correct |
449 ms |
91728 KB |
Output is correct |
10 |
Correct |
456 ms |
91900 KB |
Output is correct |
11 |
Correct |
488 ms |
95704 KB |
Output is correct |
12 |
Correct |
590 ms |
104276 KB |
Output is correct |
13 |
Correct |
484 ms |
116304 KB |
Output is correct |
14 |
Correct |
561 ms |
91436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
6 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
7 ms |
10332 KB |
Output is correct |
7 |
Correct |
96 ms |
15440 KB |
Output is correct |
8 |
Correct |
5 ms |
10072 KB |
Output is correct |
9 |
Correct |
6 ms |
10332 KB |
Output is correct |
10 |
Correct |
65 ms |
15308 KB |
Output is correct |
11 |
Correct |
74 ms |
15608 KB |
Output is correct |
12 |
Correct |
94 ms |
15636 KB |
Output is correct |
13 |
Correct |
66 ms |
15444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
6 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
7 ms |
10332 KB |
Output is correct |
7 |
Correct |
96 ms |
15440 KB |
Output is correct |
8 |
Correct |
5 ms |
10072 KB |
Output is correct |
9 |
Correct |
6 ms |
10332 KB |
Output is correct |
10 |
Correct |
65 ms |
15308 KB |
Output is correct |
11 |
Correct |
74 ms |
15608 KB |
Output is correct |
12 |
Correct |
94 ms |
15636 KB |
Output is correct |
13 |
Correct |
66 ms |
15444 KB |
Output is correct |
14 |
Correct |
122 ms |
20816 KB |
Output is correct |
15 |
Correct |
919 ms |
89988 KB |
Output is correct |
16 |
Correct |
115 ms |
20884 KB |
Output is correct |
17 |
Correct |
922 ms |
90044 KB |
Output is correct |
18 |
Correct |
558 ms |
102740 KB |
Output is correct |
19 |
Correct |
575 ms |
90400 KB |
Output is correct |
20 |
Correct |
777 ms |
89504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
6 ms |
9820 KB |
Output is correct |
3 |
Correct |
69 ms |
14512 KB |
Output is correct |
4 |
Correct |
620 ms |
96488 KB |
Output is correct |
5 |
Correct |
710 ms |
92120 KB |
Output is correct |
6 |
Correct |
962 ms |
90788 KB |
Output is correct |
7 |
Correct |
1103 ms |
91824 KB |
Output is correct |
8 |
Correct |
1035 ms |
91220 KB |
Output is correct |
9 |
Correct |
603 ms |
94800 KB |
Output is correct |
10 |
Correct |
635 ms |
94032 KB |
Output is correct |
11 |
Correct |
530 ms |
116472 KB |
Output is correct |
12 |
Correct |
584 ms |
91656 KB |
Output is correct |
13 |
Correct |
536 ms |
108620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
10072 KB |
Output is correct |
3 |
Correct |
4 ms |
9880 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9852 KB |
Output is correct |
6 |
Correct |
6 ms |
9932 KB |
Output is correct |
7 |
Correct |
15 ms |
11008 KB |
Output is correct |
8 |
Correct |
13 ms |
10844 KB |
Output is correct |
9 |
Correct |
15 ms |
10840 KB |
Output is correct |
10 |
Correct |
16 ms |
10844 KB |
Output is correct |
11 |
Correct |
24 ms |
10844 KB |
Output is correct |
12 |
Correct |
19 ms |
11100 KB |
Output is correct |
13 |
Correct |
12 ms |
11008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9872 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
6 ms |
9820 KB |
Output is correct |
5 |
Correct |
6 ms |
10332 KB |
Output is correct |
6 |
Correct |
593 ms |
90964 KB |
Output is correct |
7 |
Correct |
904 ms |
91216 KB |
Output is correct |
8 |
Correct |
1090 ms |
91424 KB |
Output is correct |
9 |
Correct |
989 ms |
90960 KB |
Output is correct |
10 |
Correct |
573 ms |
94096 KB |
Output is correct |
11 |
Correct |
600 ms |
92240 KB |
Output is correct |
12 |
Correct |
501 ms |
98388 KB |
Output is correct |
13 |
Correct |
635 ms |
92788 KB |
Output is correct |
14 |
Correct |
871 ms |
91588 KB |
Output is correct |
15 |
Correct |
1024 ms |
91444 KB |
Output is correct |
16 |
Correct |
456 ms |
94348 KB |
Output is correct |
17 |
Correct |
577 ms |
91684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9816 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
54 ms |
13596 KB |
Output is correct |
7 |
Correct |
116 ms |
21716 KB |
Output is correct |
8 |
Correct |
442 ms |
91220 KB |
Output is correct |
9 |
Correct |
449 ms |
91728 KB |
Output is correct |
10 |
Correct |
456 ms |
91900 KB |
Output is correct |
11 |
Correct |
488 ms |
95704 KB |
Output is correct |
12 |
Correct |
590 ms |
104276 KB |
Output is correct |
13 |
Correct |
484 ms |
116304 KB |
Output is correct |
14 |
Correct |
561 ms |
91436 KB |
Output is correct |
15 |
Correct |
4 ms |
9816 KB |
Output is correct |
16 |
Correct |
4 ms |
9820 KB |
Output is correct |
17 |
Correct |
6 ms |
9820 KB |
Output is correct |
18 |
Correct |
4 ms |
9820 KB |
Output is correct |
19 |
Correct |
4 ms |
9820 KB |
Output is correct |
20 |
Correct |
7 ms |
10332 KB |
Output is correct |
21 |
Correct |
96 ms |
15440 KB |
Output is correct |
22 |
Correct |
5 ms |
10072 KB |
Output is correct |
23 |
Correct |
6 ms |
10332 KB |
Output is correct |
24 |
Correct |
65 ms |
15308 KB |
Output is correct |
25 |
Correct |
74 ms |
15608 KB |
Output is correct |
26 |
Correct |
94 ms |
15636 KB |
Output is correct |
27 |
Correct |
66 ms |
15444 KB |
Output is correct |
28 |
Correct |
122 ms |
20816 KB |
Output is correct |
29 |
Correct |
919 ms |
89988 KB |
Output is correct |
30 |
Correct |
115 ms |
20884 KB |
Output is correct |
31 |
Correct |
922 ms |
90044 KB |
Output is correct |
32 |
Correct |
558 ms |
102740 KB |
Output is correct |
33 |
Correct |
575 ms |
90400 KB |
Output is correct |
34 |
Correct |
777 ms |
89504 KB |
Output is correct |
35 |
Correct |
4 ms |
9820 KB |
Output is correct |
36 |
Correct |
6 ms |
9820 KB |
Output is correct |
37 |
Correct |
69 ms |
14512 KB |
Output is correct |
38 |
Correct |
620 ms |
96488 KB |
Output is correct |
39 |
Correct |
710 ms |
92120 KB |
Output is correct |
40 |
Correct |
962 ms |
90788 KB |
Output is correct |
41 |
Correct |
1103 ms |
91824 KB |
Output is correct |
42 |
Correct |
1035 ms |
91220 KB |
Output is correct |
43 |
Correct |
603 ms |
94800 KB |
Output is correct |
44 |
Correct |
635 ms |
94032 KB |
Output is correct |
45 |
Correct |
530 ms |
116472 KB |
Output is correct |
46 |
Correct |
584 ms |
91656 KB |
Output is correct |
47 |
Correct |
536 ms |
108620 KB |
Output is correct |
48 |
Correct |
5 ms |
9816 KB |
Output is correct |
49 |
Correct |
3 ms |
10072 KB |
Output is correct |
50 |
Correct |
4 ms |
9880 KB |
Output is correct |
51 |
Correct |
4 ms |
9820 KB |
Output is correct |
52 |
Correct |
4 ms |
9852 KB |
Output is correct |
53 |
Correct |
6 ms |
9932 KB |
Output is correct |
54 |
Correct |
15 ms |
11008 KB |
Output is correct |
55 |
Correct |
13 ms |
10844 KB |
Output is correct |
56 |
Correct |
15 ms |
10840 KB |
Output is correct |
57 |
Correct |
16 ms |
10844 KB |
Output is correct |
58 |
Correct |
24 ms |
10844 KB |
Output is correct |
59 |
Correct |
19 ms |
11100 KB |
Output is correct |
60 |
Correct |
12 ms |
11008 KB |
Output is correct |
61 |
Correct |
83 ms |
14928 KB |
Output is correct |
62 |
Correct |
124 ms |
21612 KB |
Output is correct |
63 |
Correct |
576 ms |
91728 KB |
Output is correct |
64 |
Correct |
735 ms |
91996 KB |
Output is correct |
65 |
Correct |
876 ms |
91984 KB |
Output is correct |
66 |
Correct |
1128 ms |
92136 KB |
Output is correct |
67 |
Correct |
1076 ms |
91712 KB |
Output is correct |
68 |
Correct |
710 ms |
95828 KB |
Output is correct |
69 |
Correct |
778 ms |
93012 KB |
Output is correct |
70 |
Correct |
625 ms |
98608 KB |
Output is correct |
71 |
Correct |
840 ms |
93444 KB |
Output is correct |
72 |
Correct |
1053 ms |
92248 KB |
Output is correct |
73 |
Correct |
1159 ms |
92464 KB |
Output is correct |
74 |
Correct |
586 ms |
91728 KB |
Output is correct |
75 |
Correct |
617 ms |
92756 KB |
Output is correct |