#include <bits/stdc++.h>
#include "plants.h"
using namespace std;
#define MAXN 200005
#define debug(...) fprintf(stderr, __VA_ARGS__); fflush(stderr);
const int TS = 1<<20, ST = TS/2-1;
int N, K, R[MAXN*2];
int A[MAXN*2], B[MAXN*2];
struct SegTree{
template <typename T>
struct Value{
T value, lazysum;
};
Value<int> tree[TS];
void build(int arr[]){
memset(tree, 0, sizeof(tree));
for (int i=ST+1;i<TS;i++) tree[i].value = i-ST <= N+N ? arr[i-ST] : 1e9;
for (int i=ST;i;i--) tree[i].value = min(tree[i*2].value, tree[i*2+1].value);
}
void propagate(int n){
if (n <= ST){
tree[n*2].value += tree[n].lazysum, tree[n*2+1].value += tree[n].lazysum;
tree[n*2].lazysum += tree[n].lazysum, tree[n*2+1].lazysum += tree[n].lazysum;
}
tree[n].lazysum = 0;
}
void update(int n, int s, int e, int l, int r, const int &v){
if (l > r || e < l || r < s) return;
if (l <= s && e <= r){
tree[n].value += v, tree[n].lazysum += v;
return;
}
propagate(n);
int m = s+e >> 1;
update(n*2, s, m, l, r, v);
update(n*2+1, m+1, e, l, r, v);
tree[n].value = min(tree[n*2].value, tree[n*2+1].value);
}
int findzero(int n){
if (tree[n].value != 0) return 0;
propagate(n);
if (n > ST) return n-ST;
if (!tree[n*2].value) return findzero(n*2);
else if (!tree[n*2+1].value) return findzero(n*2+1);
else assert(0);
}
} segtree;
void get(int perm[])
{
segtree.build(R);
stack <int> wait;
for (int ord=1;ord<=N+N;ord++){
int x = segtree.findzero(1);
while (!x){
int q = wait.top(); wait.pop();
segtree.update(1, 1, TS/2, q, N+N, -1);
x = segtree.findzero(1);
}
perm[x] = ord;
segtree.update(1, 1, TS/2, x, x, 1e9);
segtree.update(1, 1, TS/2, max(1, x-K+1), x-1, -1);
if (x-K+1 < 1) wait.push(N+N-(1-(x-K+1))+1);
}
}
void init(int K, vector<int> r)
{
::K = K;
N = r.size();
for (int i=1;i<=N;i++) R[N+i] = R[i] = r[i-1];
get(A);
for (int i=1;i<=N+N;i++) R[i] = K-R[i]-1;
get(B);
}
int compare_plants(int x, int y)
{
if (x > y) return -compare_plants(y, x);
x++; y++;
if (A[x] > A[y] || B[y] > B[x+N]) return -1;
if (B[x] > B[y] || A[y] > A[x+N]) return 1;
return 0;
}
Compilation message
plants.cpp: In member function 'void SegTree::update(int, int, int, int, int, const int&)':
plants.cpp:37:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int m = s+e >> 1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8576 KB |
Output is correct |
4 |
Correct |
10 ms |
8576 KB |
Output is correct |
5 |
Correct |
10 ms |
8576 KB |
Output is correct |
6 |
Correct |
74 ms |
11384 KB |
Output is correct |
7 |
Correct |
147 ms |
12024 KB |
Output is correct |
8 |
Correct |
723 ms |
17144 KB |
Output is correct |
9 |
Correct |
737 ms |
17144 KB |
Output is correct |
10 |
Correct |
722 ms |
17144 KB |
Output is correct |
11 |
Correct |
735 ms |
17144 KB |
Output is correct |
12 |
Correct |
722 ms |
17196 KB |
Output is correct |
13 |
Correct |
715 ms |
17272 KB |
Output is correct |
14 |
Correct |
730 ms |
17144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8576 KB |
Output is correct |
4 |
Correct |
9 ms |
8576 KB |
Output is correct |
5 |
Correct |
10 ms |
8576 KB |
Output is correct |
6 |
Correct |
15 ms |
8704 KB |
Output is correct |
7 |
Correct |
105 ms |
11588 KB |
Output is correct |
8 |
Correct |
11 ms |
8576 KB |
Output is correct |
9 |
Correct |
17 ms |
8704 KB |
Output is correct |
10 |
Correct |
110 ms |
11512 KB |
Output is correct |
11 |
Correct |
103 ms |
11512 KB |
Output is correct |
12 |
Correct |
98 ms |
11640 KB |
Output is correct |
13 |
Correct |
108 ms |
11512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8576 KB |
Output is correct |
4 |
Correct |
9 ms |
8576 KB |
Output is correct |
5 |
Correct |
10 ms |
8576 KB |
Output is correct |
6 |
Correct |
15 ms |
8704 KB |
Output is correct |
7 |
Correct |
105 ms |
11588 KB |
Output is correct |
8 |
Correct |
11 ms |
8576 KB |
Output is correct |
9 |
Correct |
17 ms |
8704 KB |
Output is correct |
10 |
Correct |
110 ms |
11512 KB |
Output is correct |
11 |
Correct |
103 ms |
11512 KB |
Output is correct |
12 |
Correct |
98 ms |
11640 KB |
Output is correct |
13 |
Correct |
108 ms |
11512 KB |
Output is correct |
14 |
Correct |
185 ms |
12024 KB |
Output is correct |
15 |
Correct |
1451 ms |
17296 KB |
Output is correct |
16 |
Correct |
185 ms |
12152 KB |
Output is correct |
17 |
Correct |
1445 ms |
17276 KB |
Output is correct |
18 |
Correct |
944 ms |
17752 KB |
Output is correct |
19 |
Correct |
959 ms |
17568 KB |
Output is correct |
20 |
Correct |
1382 ms |
17400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Correct |
87 ms |
11384 KB |
Output is correct |
4 |
Correct |
787 ms |
17272 KB |
Output is correct |
5 |
Correct |
904 ms |
17140 KB |
Output is correct |
6 |
Correct |
1141 ms |
17300 KB |
Output is correct |
7 |
Correct |
1378 ms |
17212 KB |
Output is correct |
8 |
Correct |
1466 ms |
17144 KB |
Output is correct |
9 |
Correct |
821 ms |
17148 KB |
Output is correct |
10 |
Correct |
832 ms |
17096 KB |
Output is correct |
11 |
Correct |
736 ms |
17272 KB |
Output is correct |
12 |
Correct |
827 ms |
17172 KB |
Output is correct |
13 |
Correct |
969 ms |
17472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
10 ms |
8576 KB |
Output is correct |
3 |
Correct |
12 ms |
8552 KB |
Output is correct |
4 |
Correct |
9 ms |
8576 KB |
Output is correct |
5 |
Correct |
12 ms |
8576 KB |
Output is correct |
6 |
Correct |
12 ms |
8576 KB |
Output is correct |
7 |
Correct |
26 ms |
9216 KB |
Output is correct |
8 |
Correct |
26 ms |
9216 KB |
Output is correct |
9 |
Correct |
28 ms |
9216 KB |
Output is correct |
10 |
Correct |
27 ms |
9208 KB |
Output is correct |
11 |
Correct |
27 ms |
9208 KB |
Output is correct |
12 |
Correct |
27 ms |
9208 KB |
Output is correct |
13 |
Correct |
26 ms |
9216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8576 KB |
Output is correct |
2 |
Correct |
10 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8576 KB |
Output is correct |
4 |
Correct |
11 ms |
8576 KB |
Output is correct |
5 |
Correct |
14 ms |
8616 KB |
Output is correct |
6 |
Correct |
868 ms |
17092 KB |
Output is correct |
7 |
Correct |
1132 ms |
17168 KB |
Output is correct |
8 |
Correct |
1283 ms |
17144 KB |
Output is correct |
9 |
Correct |
1391 ms |
17272 KB |
Output is correct |
10 |
Correct |
821 ms |
17400 KB |
Output is correct |
11 |
Correct |
1055 ms |
17400 KB |
Output is correct |
12 |
Correct |
786 ms |
17272 KB |
Output is correct |
13 |
Correct |
892 ms |
17400 KB |
Output is correct |
14 |
Correct |
1111 ms |
17172 KB |
Output is correct |
15 |
Correct |
1425 ms |
17272 KB |
Output is correct |
16 |
Correct |
812 ms |
17124 KB |
Output is correct |
17 |
Correct |
861 ms |
17144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8576 KB |
Output is correct |
4 |
Correct |
10 ms |
8576 KB |
Output is correct |
5 |
Correct |
10 ms |
8576 KB |
Output is correct |
6 |
Correct |
74 ms |
11384 KB |
Output is correct |
7 |
Correct |
147 ms |
12024 KB |
Output is correct |
8 |
Correct |
723 ms |
17144 KB |
Output is correct |
9 |
Correct |
737 ms |
17144 KB |
Output is correct |
10 |
Correct |
722 ms |
17144 KB |
Output is correct |
11 |
Correct |
735 ms |
17144 KB |
Output is correct |
12 |
Correct |
722 ms |
17196 KB |
Output is correct |
13 |
Correct |
715 ms |
17272 KB |
Output is correct |
14 |
Correct |
730 ms |
17144 KB |
Output is correct |
15 |
Correct |
10 ms |
8576 KB |
Output is correct |
16 |
Correct |
9 ms |
8576 KB |
Output is correct |
17 |
Correct |
9 ms |
8576 KB |
Output is correct |
18 |
Correct |
9 ms |
8576 KB |
Output is correct |
19 |
Correct |
10 ms |
8576 KB |
Output is correct |
20 |
Correct |
15 ms |
8704 KB |
Output is correct |
21 |
Correct |
105 ms |
11588 KB |
Output is correct |
22 |
Correct |
11 ms |
8576 KB |
Output is correct |
23 |
Correct |
17 ms |
8704 KB |
Output is correct |
24 |
Correct |
110 ms |
11512 KB |
Output is correct |
25 |
Correct |
103 ms |
11512 KB |
Output is correct |
26 |
Correct |
98 ms |
11640 KB |
Output is correct |
27 |
Correct |
108 ms |
11512 KB |
Output is correct |
28 |
Correct |
185 ms |
12024 KB |
Output is correct |
29 |
Correct |
1451 ms |
17296 KB |
Output is correct |
30 |
Correct |
185 ms |
12152 KB |
Output is correct |
31 |
Correct |
1445 ms |
17276 KB |
Output is correct |
32 |
Correct |
944 ms |
17752 KB |
Output is correct |
33 |
Correct |
959 ms |
17568 KB |
Output is correct |
34 |
Correct |
1382 ms |
17400 KB |
Output is correct |
35 |
Correct |
10 ms |
8576 KB |
Output is correct |
36 |
Correct |
9 ms |
8576 KB |
Output is correct |
37 |
Correct |
87 ms |
11384 KB |
Output is correct |
38 |
Correct |
787 ms |
17272 KB |
Output is correct |
39 |
Correct |
904 ms |
17140 KB |
Output is correct |
40 |
Correct |
1141 ms |
17300 KB |
Output is correct |
41 |
Correct |
1378 ms |
17212 KB |
Output is correct |
42 |
Correct |
1466 ms |
17144 KB |
Output is correct |
43 |
Correct |
821 ms |
17148 KB |
Output is correct |
44 |
Correct |
832 ms |
17096 KB |
Output is correct |
45 |
Correct |
736 ms |
17272 KB |
Output is correct |
46 |
Correct |
827 ms |
17172 KB |
Output is correct |
47 |
Correct |
969 ms |
17472 KB |
Output is correct |
48 |
Correct |
10 ms |
8576 KB |
Output is correct |
49 |
Correct |
10 ms |
8576 KB |
Output is correct |
50 |
Correct |
12 ms |
8552 KB |
Output is correct |
51 |
Correct |
9 ms |
8576 KB |
Output is correct |
52 |
Correct |
12 ms |
8576 KB |
Output is correct |
53 |
Correct |
12 ms |
8576 KB |
Output is correct |
54 |
Correct |
26 ms |
9216 KB |
Output is correct |
55 |
Correct |
26 ms |
9216 KB |
Output is correct |
56 |
Correct |
28 ms |
9216 KB |
Output is correct |
57 |
Correct |
27 ms |
9208 KB |
Output is correct |
58 |
Correct |
27 ms |
9208 KB |
Output is correct |
59 |
Correct |
27 ms |
9208 KB |
Output is correct |
60 |
Correct |
26 ms |
9216 KB |
Output is correct |
61 |
Correct |
126 ms |
11384 KB |
Output is correct |
62 |
Correct |
161 ms |
11896 KB |
Output is correct |
63 |
Correct |
811 ms |
17212 KB |
Output is correct |
64 |
Correct |
912 ms |
17208 KB |
Output is correct |
65 |
Correct |
1260 ms |
17228 KB |
Output is correct |
66 |
Correct |
1433 ms |
17212 KB |
Output is correct |
67 |
Correct |
1529 ms |
17204 KB |
Output is correct |
68 |
Correct |
939 ms |
17184 KB |
Output is correct |
69 |
Correct |
1091 ms |
17360 KB |
Output is correct |
70 |
Correct |
787 ms |
17144 KB |
Output is correct |
71 |
Correct |
896 ms |
17144 KB |
Output is correct |
72 |
Correct |
1124 ms |
17348 KB |
Output is correct |
73 |
Correct |
1370 ms |
17204 KB |
Output is correct |
74 |
Correct |
799 ms |
17092 KB |
Output is correct |
75 |
Correct |
895 ms |
17144 KB |
Output is correct |