#include "plants.h"
#include <cassert>
#include <iostream>
// regret ca mi a fost lene sa scriu 2 linii de define uri si am stat sa scriu printf uri ca prostu
using namespace std;
struct SegmTree {
int n;
vector<int> lazy;
vector<pair<int, int>> t;
SegmTree() {}
SegmTree(const vector<int> &r) : n(r.size()) {
int sz = 1; while (sz < n) sz *= 2;
lazy.resize(2 * sz);
t.resize(2 * sz);
Build(0, 0, n - 1, r);
}
void Build(int node, int left, int right, const vector<int> &r) {
if (left == right) {
t[node] = {r[left], left};
return;
}
int mid = (left + right) / 2;
Build(2 * node + 1, left, mid, r);
Build(2 * node + 2, mid + 1, right, r);
t[node] = max(t[2 * node + 1], t[2 * node + 2]);
}
void Push(int node) {
if (lazy[node] == 0) return;
for (int son : {2 * node + 1, 2 * node + 2}) {
t[son].first += lazy[node];
lazy[son] += lazy[node];
}
lazy[node] = 0;
}
pair<int, int> Max(int node, int left, int right, int x, int y) {
if (x <= left && right <= y) {
return t[node];
}
Push(node);
int mid = (left + right) / 2;
if (y <= mid)
return Max(2 * node + 1, left, mid, x, y);
if (mid < x)
return Max(2 * node + 2, mid + 1, right, x, y);
return max(
Max(2 * node + 1, left, mid, x, y),
Max(2 * node + 2, mid + 1, right, x, y)
);
}
void Add(int node, int left, int right, int x, int y, int val) {
// printf("node = %d, left = %d, right = %d\n", node, left, right);
// printf("x = %d, y = %d\n", x, y);
// printf("%d\n", x <= left && right <= y);
if (x <= left && right <= y) {
t[node].first += val;
lazy[node] += val;
// printf("left = %d, val = %d\n", left, val);
return;
}
Push(node);
int mid = (left + right) / 2;
if (x <= mid)
Add(2 * node + 1, left, mid, x, y, val);
if (mid < y)
Add(2 * node + 2, mid + 1, right, x, y, val);
t[node] = max(t[2 * node + 1], t[2 * node + 2]);
}
pair<int, int> Max(int l, int r) {
if (l >= 0) {
return Max(0, 0, n - 1, l, r);
}
l += n;
if (r >= 0)
return max(Max(0, r), Max(l, n - 1));
return Max(l, r + n);
}
void Add(int l, int r, int val) {
// printf("l = %d, r = %d, val = %d\n", l, r, val);
// printf("n = %d\n", n);
if (l >= 0) {
Add(0, 0, n - 1, l, r, val);
return;
}
l += n;
if (r >= 0) {
Add(0, 0, n - 1, 0, r, val);
Add(0, 0, n - 1, l, n - 1, val);
return;
}
Add(0, 0, n - 1, l, r + n, val);
}
};
int n, k;
int curr_h = 0;
vector<int> h;
SegmTree st;
void SetH(int pos) {
pair<int, int> p;
while ((p = st.Max(pos - k + 1, pos - 1)).first == k - 1) {
SetH(p.second);
}
h[pos] = ++curr_h;
// printf("pos = %d\n", pos);
st.Add(pos - k + 1, pos - 1, +1);
st.Add(pos, pos, -n);
// printf("(%d, %d)\n", st.Max(pos - k + 1, pos - 1).first, st.Max(pos - k + 1, pos - 1).second);
}
void init(int k_, vector<int> r) {
n = r.size();
k = k_;
st = SegmTree(r);
h.resize(n);
pair<int, int> p;
while ((p = st.Max(0, n - 1)).first == k - 1) {
// printf("pp = %d\n", p.second);
// printf("val = %d\n", p.first);
SetH(p.second);
}
// printf("p = (%d, %d)\n", p.first, p.second);
}
int compare_plants(int x, int y) {
return 2 * (h[x] > h[y]) - 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
68 ms |
5188 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
71 ms |
5316 KB |
Output is correct |
11 |
Correct |
65 ms |
5188 KB |
Output is correct |
12 |
Correct |
65 ms |
5600 KB |
Output is correct |
13 |
Correct |
67 ms |
5216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
68 ms |
5188 KB |
Output is correct |
8 |
Correct |
2 ms |
380 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
71 ms |
5316 KB |
Output is correct |
11 |
Correct |
65 ms |
5188 KB |
Output is correct |
12 |
Correct |
65 ms |
5600 KB |
Output is correct |
13 |
Correct |
67 ms |
5216 KB |
Output is correct |
14 |
Correct |
95 ms |
6300 KB |
Output is correct |
15 |
Correct |
517 ms |
14900 KB |
Output is correct |
16 |
Correct |
101 ms |
6340 KB |
Output is correct |
17 |
Correct |
547 ms |
14968 KB |
Output is correct |
18 |
Correct |
317 ms |
14380 KB |
Output is correct |
19 |
Correct |
373 ms |
27460 KB |
Output is correct |
20 |
Correct |
454 ms |
15044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
62 ms |
4948 KB |
Output is correct |
4 |
Correct |
265 ms |
20548 KB |
Output is correct |
5 |
Correct |
355 ms |
15828 KB |
Output is correct |
6 |
Correct |
445 ms |
14624 KB |
Output is correct |
7 |
Correct |
502 ms |
14660 KB |
Output is correct |
8 |
Correct |
542 ms |
14832 KB |
Output is correct |
9 |
Correct |
296 ms |
14168 KB |
Output is correct |
10 |
Correct |
310 ms |
16852 KB |
Output is correct |
11 |
Correct |
213 ms |
14020 KB |
Output is correct |
12 |
Correct |
293 ms |
39304 KB |
Output is correct |
13 |
Correct |
308 ms |
14268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
292 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |