#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
struct segment_tree {
int l, r, m, v, t;
segment_tree *c[2];
segment_tree(int l, int r,
const vector <int> &a):
l(l), r(r), m((l + r) / 2), t(0) {
if (l + 1 == r) {
v = a[l]; return;
}
c[0] = new segment_tree(l, m, a);
c[1] = new segment_tree(m, r, a);
pull();
}
void apply(int x) {
v -= x; t += x;
}
void pull() {
v = min(c[0]->v, c[1]->v);
}
void push() {
c[0]->apply(t);
c[1]->apply(t); t = 0;
}
void dec(int x, int y) {
if (x == y) return;
if (l >= x && r <= y)
return apply(1);
push();
if (m > x) c[0]->dec(x, y);
if (m < y) c[1]->dec(x, y);
pull();
}
void del(int p) {
if (l + 1 == r) v = 1e9;
else {
push();
if (m > p) c[0]->del(p);
else c[1]->del(p);
pull();
}
}
int zero() {
if (l + 1 == r) return l;
push();
if (!c[0]->v)
return c[0]->zero();
return c[1]->zero();
}
};
void build(int k, vector <int> r,
vector <int> &idx, vector <int> &ptr) {
int n = r.size();
ptr.resize(n + 1);
idx.resize(n + 1);
vector <int> que(n + 1);
que[0] = n; idx[n] = -1;
int first = 1, last = 1;
segment_tree seg(0, n, r);
for (int i = 0, p; i < n; i++) {
while (seg.v) {
p = que[first++];
seg.dec(p - k + n + 1, n);
}
p = seg.zero(); idx[p] = i;
seg.del(p);
if (p < k - 1)
seg.dec(0, que[last++] = p);
else seg.dec(p - k + 1, p);
ptr[p] = idx[que[first - 1]];
}
}
vector <int> idxg, idxl;
vector <int> ptrg, ptrl;
void init(int k, vector <int> r) {
build(k, r, idxg, ptrg);
for (int &x : r) x = k - 1 - x;
build(k, r, idxl, ptrl);
return;
}
int compare_plants(int x, int y) {
if (x > y)
return -compare_plants(y, x);
if (idxg[x] > idxg[y] ||
ptrl[y] >= idxl[x]) return -1;
if (idxl[x] > idxl[y] ||
ptrg[y] >= idxg[x]) return 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
56 ms |
3028 KB |
Output is correct |
7 |
Correct |
88 ms |
7452 KB |
Output is correct |
8 |
Correct |
319 ms |
46624 KB |
Output is correct |
9 |
Correct |
317 ms |
46528 KB |
Output is correct |
10 |
Correct |
324 ms |
46508 KB |
Output is correct |
11 |
Correct |
338 ms |
46420 KB |
Output is correct |
12 |
Correct |
319 ms |
46520 KB |
Output is correct |
13 |
Correct |
312 ms |
46476 KB |
Output is correct |
14 |
Correct |
327 ms |
46472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
5 ms |
556 KB |
Output is correct |
7 |
Correct |
86 ms |
6004 KB |
Output is correct |
8 |
Correct |
3 ms |
456 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
68 ms |
6084 KB |
Output is correct |
11 |
Correct |
66 ms |
5972 KB |
Output is correct |
12 |
Correct |
64 ms |
6108 KB |
Output is correct |
13 |
Correct |
70 ms |
6104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
5 ms |
556 KB |
Output is correct |
7 |
Correct |
86 ms |
6004 KB |
Output is correct |
8 |
Correct |
3 ms |
456 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
68 ms |
6084 KB |
Output is correct |
11 |
Correct |
66 ms |
5972 KB |
Output is correct |
12 |
Correct |
64 ms |
6108 KB |
Output is correct |
13 |
Correct |
70 ms |
6104 KB |
Output is correct |
14 |
Correct |
102 ms |
9628 KB |
Output is correct |
15 |
Correct |
733 ms |
50412 KB |
Output is correct |
16 |
Correct |
97 ms |
9724 KB |
Output is correct |
17 |
Correct |
726 ms |
50296 KB |
Output is correct |
18 |
Correct |
372 ms |
49776 KB |
Output is correct |
19 |
Correct |
378 ms |
50268 KB |
Output is correct |
20 |
Correct |
637 ms |
50208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
70 ms |
4052 KB |
Output is correct |
4 |
Correct |
333 ms |
49308 KB |
Output is correct |
5 |
Correct |
395 ms |
49544 KB |
Output is correct |
6 |
Correct |
570 ms |
49764 KB |
Output is correct |
7 |
Correct |
697 ms |
49956 KB |
Output is correct |
8 |
Correct |
728 ms |
50160 KB |
Output is correct |
9 |
Correct |
357 ms |
49528 KB |
Output is correct |
10 |
Correct |
355 ms |
49444 KB |
Output is correct |
11 |
Correct |
310 ms |
49288 KB |
Output is correct |
12 |
Correct |
340 ms |
49592 KB |
Output is correct |
13 |
Correct |
372 ms |
49596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
292 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
14 ms |
1264 KB |
Output is correct |
8 |
Correct |
14 ms |
1276 KB |
Output is correct |
9 |
Correct |
14 ms |
1220 KB |
Output is correct |
10 |
Correct |
14 ms |
1228 KB |
Output is correct |
11 |
Correct |
13 ms |
1236 KB |
Output is correct |
12 |
Correct |
14 ms |
1272 KB |
Output is correct |
13 |
Correct |
17 ms |
1204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
372 ms |
48820 KB |
Output is correct |
7 |
Correct |
573 ms |
48776 KB |
Output is correct |
8 |
Correct |
680 ms |
49032 KB |
Output is correct |
9 |
Correct |
689 ms |
49296 KB |
Output is correct |
10 |
Correct |
344 ms |
48656 KB |
Output is correct |
11 |
Correct |
473 ms |
49188 KB |
Output is correct |
12 |
Correct |
329 ms |
48420 KB |
Output is correct |
13 |
Correct |
380 ms |
48696 KB |
Output is correct |
14 |
Correct |
572 ms |
48892 KB |
Output is correct |
15 |
Correct |
698 ms |
49100 KB |
Output is correct |
16 |
Correct |
340 ms |
48668 KB |
Output is correct |
17 |
Correct |
372 ms |
48744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
56 ms |
3028 KB |
Output is correct |
7 |
Correct |
88 ms |
7452 KB |
Output is correct |
8 |
Correct |
319 ms |
46624 KB |
Output is correct |
9 |
Correct |
317 ms |
46528 KB |
Output is correct |
10 |
Correct |
324 ms |
46508 KB |
Output is correct |
11 |
Correct |
338 ms |
46420 KB |
Output is correct |
12 |
Correct |
319 ms |
46520 KB |
Output is correct |
13 |
Correct |
312 ms |
46476 KB |
Output is correct |
14 |
Correct |
327 ms |
46472 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
204 KB |
Output is correct |
20 |
Correct |
5 ms |
556 KB |
Output is correct |
21 |
Correct |
86 ms |
6004 KB |
Output is correct |
22 |
Correct |
3 ms |
456 KB |
Output is correct |
23 |
Correct |
4 ms |
588 KB |
Output is correct |
24 |
Correct |
68 ms |
6084 KB |
Output is correct |
25 |
Correct |
66 ms |
5972 KB |
Output is correct |
26 |
Correct |
64 ms |
6108 KB |
Output is correct |
27 |
Correct |
70 ms |
6104 KB |
Output is correct |
28 |
Correct |
102 ms |
9628 KB |
Output is correct |
29 |
Correct |
733 ms |
50412 KB |
Output is correct |
30 |
Correct |
97 ms |
9724 KB |
Output is correct |
31 |
Correct |
726 ms |
50296 KB |
Output is correct |
32 |
Correct |
372 ms |
49776 KB |
Output is correct |
33 |
Correct |
378 ms |
50268 KB |
Output is correct |
34 |
Correct |
637 ms |
50208 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
70 ms |
4052 KB |
Output is correct |
38 |
Correct |
333 ms |
49308 KB |
Output is correct |
39 |
Correct |
395 ms |
49544 KB |
Output is correct |
40 |
Correct |
570 ms |
49764 KB |
Output is correct |
41 |
Correct |
697 ms |
49956 KB |
Output is correct |
42 |
Correct |
728 ms |
50160 KB |
Output is correct |
43 |
Correct |
357 ms |
49528 KB |
Output is correct |
44 |
Correct |
355 ms |
49444 KB |
Output is correct |
45 |
Correct |
310 ms |
49288 KB |
Output is correct |
46 |
Correct |
340 ms |
49592 KB |
Output is correct |
47 |
Correct |
372 ms |
49596 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
292 KB |
Output is correct |
53 |
Correct |
2 ms |
332 KB |
Output is correct |
54 |
Correct |
14 ms |
1264 KB |
Output is correct |
55 |
Correct |
14 ms |
1276 KB |
Output is correct |
56 |
Correct |
14 ms |
1220 KB |
Output is correct |
57 |
Correct |
14 ms |
1228 KB |
Output is correct |
58 |
Correct |
13 ms |
1236 KB |
Output is correct |
59 |
Correct |
14 ms |
1272 KB |
Output is correct |
60 |
Correct |
17 ms |
1204 KB |
Output is correct |
61 |
Correct |
60 ms |
5332 KB |
Output is correct |
62 |
Correct |
88 ms |
9492 KB |
Output is correct |
63 |
Correct |
333 ms |
49312 KB |
Output is correct |
64 |
Correct |
380 ms |
49560 KB |
Output is correct |
65 |
Correct |
581 ms |
49852 KB |
Output is correct |
66 |
Correct |
686 ms |
50080 KB |
Output is correct |
67 |
Correct |
735 ms |
50264 KB |
Output is correct |
68 |
Correct |
379 ms |
49704 KB |
Output is correct |
69 |
Correct |
485 ms |
50120 KB |
Output is correct |
70 |
Correct |
340 ms |
49316 KB |
Output is correct |
71 |
Correct |
377 ms |
49572 KB |
Output is correct |
72 |
Correct |
584 ms |
49728 KB |
Output is correct |
73 |
Correct |
702 ms |
50076 KB |
Output is correct |
74 |
Correct |
339 ms |
49444 KB |
Output is correct |
75 |
Correct |
372 ms |
49624 KB |
Output is correct |