#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
const int n_bits=18;
const int inf = 1e9;
const int n_max = 2e5+5;
int arr[1<<(n_bits+1)];
int lazyadd[1<<(n_bits+1)];
int p_lt[n_max];
int p_rg[n_max];
int stash[n_max]; // a manual queue
int tallest[n_max];
int shortest[n_max];
struct segtree {
int node = 1;
int lazy = 0;
segtree(){}
void build(vector<int> &v) {
assert(v.size()<(1<<n_bits));
for(int i=0; i<(1<<n_bits); i++) {
arr[i+(1<<n_bits)] = (i<(int)v.size() ? v[i] : inf);
}
for(int i=(1<<n_bits)-1; i>=1; i--) {
arr[i] = min(arr[2*i], arr[2*i+1]);
}
for(int i=0; i<(1<<(n_bits+1)); i++) {
lazyadd[i] = 0;
}
}
void prefix_update(int x, int change) {
if(x==-1) return;
x += (1<<n_bits);
lazyadd[x] += change;
for(int i=0; i<n_bits; i++) {
if(x&1) {
lazyadd[x-1] += change;
}
arr[x/2] = min(arr[x]+lazyadd[x], arr[x^1]+lazyadd[x^1]);
x=x/2;
}
}
void decr(int left, int right) {
prefix_update(left-1, 1);
prefix_update(right,-1);
}
int find_and_remove_zero() {
node = 1;
lazy = 0;
for(int i=0; i<n_bits; i++) {
lazy += lazyadd[node];
node *= 2;
if(lazy + lazyadd[node] + arr[node]) node++;
}
int ans = node - (1<<n_bits);
lazyadd[node] = 1e9;
for(int i=0; i<n_bits; i++) {
arr[node/2] = min(arr[node]+lazyadd[node], arr[node^1]+lazyadd[node^1]);
node = node/2;
}
return ans;
}
};
void lexi_smallest(int k, vector<int> &r, int* ptr, int* ord) {
segtree s;
s.build(r);
int n = r.size();
stash[0] = n;
ord[n] = -1;
int* fr = stash+1;
int* bk = stash+1;
for(int i=0; i<(int)r.size(); i++) {
while(lazyadd[1] + arr[1]) {
s.decr((*fr++)-k+1+n, n-1);
}
int p = s.find_and_remove_zero();
ord[p] = i;
if(p<k-1) {
s.prefix_update(p, -1);
*bk++ = p;
} else {
s.decr(p-k+1, p);
}
ptr[p] = ord[*(fr-1)];
}
}
void init(int k, std::vector<int> r) {
lexi_smallest(k,r,p_lt, tallest);
for(int &i: r) i = k-1-i;
lexi_smallest(k,r,p_rg, shortest);
}
int compare_plants(int x, int y) {
if(x>y) return -compare_plants(y,x);
if(tallest[x]>tallest[y] || p_rg[y]>=shortest[x]) return -1;
if(shortest[x]>shortest[y] || p_lt[y]>=tallest[x]) return 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4480 KB |
Output is correct |
2 |
Correct |
4 ms |
4480 KB |
Output is correct |
3 |
Correct |
5 ms |
4480 KB |
Output is correct |
4 |
Correct |
4 ms |
4480 KB |
Output is correct |
5 |
Correct |
4 ms |
4480 KB |
Output is correct |
6 |
Correct |
73 ms |
7292 KB |
Output is correct |
7 |
Correct |
96 ms |
7804 KB |
Output is correct |
8 |
Correct |
224 ms |
11640 KB |
Output is correct |
9 |
Correct |
219 ms |
11512 KB |
Output is correct |
10 |
Correct |
225 ms |
11512 KB |
Output is correct |
11 |
Correct |
220 ms |
11512 KB |
Output is correct |
12 |
Correct |
230 ms |
11512 KB |
Output is correct |
13 |
Correct |
218 ms |
11512 KB |
Output is correct |
14 |
Correct |
240 ms |
11512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4480 KB |
Output is correct |
2 |
Correct |
5 ms |
4480 KB |
Output is correct |
3 |
Correct |
4 ms |
4480 KB |
Output is correct |
4 |
Correct |
4 ms |
4480 KB |
Output is correct |
5 |
Correct |
5 ms |
4480 KB |
Output is correct |
6 |
Correct |
8 ms |
4480 KB |
Output is correct |
7 |
Correct |
79 ms |
7496 KB |
Output is correct |
8 |
Correct |
6 ms |
4480 KB |
Output is correct |
9 |
Correct |
7 ms |
4480 KB |
Output is correct |
10 |
Correct |
79 ms |
7416 KB |
Output is correct |
11 |
Correct |
73 ms |
7288 KB |
Output is correct |
12 |
Correct |
83 ms |
7544 KB |
Output is correct |
13 |
Correct |
78 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4480 KB |
Output is correct |
2 |
Correct |
5 ms |
4480 KB |
Output is correct |
3 |
Correct |
4 ms |
4480 KB |
Output is correct |
4 |
Correct |
4 ms |
4480 KB |
Output is correct |
5 |
Correct |
5 ms |
4480 KB |
Output is correct |
6 |
Correct |
8 ms |
4480 KB |
Output is correct |
7 |
Correct |
79 ms |
7496 KB |
Output is correct |
8 |
Correct |
6 ms |
4480 KB |
Output is correct |
9 |
Correct |
7 ms |
4480 KB |
Output is correct |
10 |
Correct |
79 ms |
7416 KB |
Output is correct |
11 |
Correct |
73 ms |
7288 KB |
Output is correct |
12 |
Correct |
83 ms |
7544 KB |
Output is correct |
13 |
Correct |
78 ms |
7416 KB |
Output is correct |
14 |
Correct |
105 ms |
7800 KB |
Output is correct |
15 |
Correct |
358 ms |
11896 KB |
Output is correct |
16 |
Correct |
99 ms |
7808 KB |
Output is correct |
17 |
Correct |
352 ms |
12024 KB |
Output is correct |
18 |
Correct |
216 ms |
11896 KB |
Output is correct |
19 |
Correct |
210 ms |
11896 KB |
Output is correct |
20 |
Correct |
366 ms |
12316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4480 KB |
Output is correct |
2 |
Correct |
4 ms |
4480 KB |
Output is correct |
3 |
Correct |
76 ms |
7288 KB |
Output is correct |
4 |
Correct |
221 ms |
11512 KB |
Output is correct |
5 |
Correct |
248 ms |
11512 KB |
Output is correct |
6 |
Correct |
333 ms |
11516 KB |
Output is correct |
7 |
Correct |
335 ms |
11512 KB |
Output is correct |
8 |
Correct |
339 ms |
11768 KB |
Output is correct |
9 |
Correct |
225 ms |
11512 KB |
Output is correct |
10 |
Correct |
216 ms |
11516 KB |
Output is correct |
11 |
Correct |
209 ms |
11464 KB |
Output is correct |
12 |
Correct |
218 ms |
11512 KB |
Output is correct |
13 |
Correct |
275 ms |
11768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4480 KB |
Output is correct |
2 |
Correct |
5 ms |
4480 KB |
Output is correct |
3 |
Correct |
6 ms |
4480 KB |
Output is correct |
4 |
Correct |
4 ms |
4480 KB |
Output is correct |
5 |
Correct |
4 ms |
4480 KB |
Output is correct |
6 |
Correct |
6 ms |
4480 KB |
Output is correct |
7 |
Correct |
19 ms |
5120 KB |
Output is correct |
8 |
Correct |
25 ms |
5112 KB |
Output is correct |
9 |
Correct |
20 ms |
5120 KB |
Output is correct |
10 |
Correct |
19 ms |
5120 KB |
Output is correct |
11 |
Correct |
19 ms |
5120 KB |
Output is correct |
12 |
Correct |
20 ms |
5120 KB |
Output is correct |
13 |
Correct |
19 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4480 KB |
Output is correct |
2 |
Correct |
4 ms |
4480 KB |
Output is correct |
3 |
Correct |
4 ms |
4480 KB |
Output is correct |
4 |
Correct |
6 ms |
4480 KB |
Output is correct |
5 |
Correct |
6 ms |
4480 KB |
Output is correct |
6 |
Correct |
221 ms |
11512 KB |
Output is correct |
7 |
Correct |
280 ms |
11512 KB |
Output is correct |
8 |
Correct |
359 ms |
11512 KB |
Output is correct |
9 |
Correct |
338 ms |
11896 KB |
Output is correct |
10 |
Correct |
231 ms |
11488 KB |
Output is correct |
11 |
Correct |
241 ms |
11768 KB |
Output is correct |
12 |
Correct |
209 ms |
11512 KB |
Output is correct |
13 |
Correct |
224 ms |
11512 KB |
Output is correct |
14 |
Correct |
282 ms |
11588 KB |
Output is correct |
15 |
Correct |
303 ms |
11640 KB |
Output is correct |
16 |
Correct |
199 ms |
11512 KB |
Output is correct |
17 |
Correct |
236 ms |
11640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4480 KB |
Output is correct |
2 |
Correct |
4 ms |
4480 KB |
Output is correct |
3 |
Correct |
5 ms |
4480 KB |
Output is correct |
4 |
Correct |
4 ms |
4480 KB |
Output is correct |
5 |
Correct |
4 ms |
4480 KB |
Output is correct |
6 |
Correct |
73 ms |
7292 KB |
Output is correct |
7 |
Correct |
96 ms |
7804 KB |
Output is correct |
8 |
Correct |
224 ms |
11640 KB |
Output is correct |
9 |
Correct |
219 ms |
11512 KB |
Output is correct |
10 |
Correct |
225 ms |
11512 KB |
Output is correct |
11 |
Correct |
220 ms |
11512 KB |
Output is correct |
12 |
Correct |
230 ms |
11512 KB |
Output is correct |
13 |
Correct |
218 ms |
11512 KB |
Output is correct |
14 |
Correct |
240 ms |
11512 KB |
Output is correct |
15 |
Correct |
5 ms |
4480 KB |
Output is correct |
16 |
Correct |
5 ms |
4480 KB |
Output is correct |
17 |
Correct |
4 ms |
4480 KB |
Output is correct |
18 |
Correct |
4 ms |
4480 KB |
Output is correct |
19 |
Correct |
5 ms |
4480 KB |
Output is correct |
20 |
Correct |
8 ms |
4480 KB |
Output is correct |
21 |
Correct |
79 ms |
7496 KB |
Output is correct |
22 |
Correct |
6 ms |
4480 KB |
Output is correct |
23 |
Correct |
7 ms |
4480 KB |
Output is correct |
24 |
Correct |
79 ms |
7416 KB |
Output is correct |
25 |
Correct |
73 ms |
7288 KB |
Output is correct |
26 |
Correct |
83 ms |
7544 KB |
Output is correct |
27 |
Correct |
78 ms |
7416 KB |
Output is correct |
28 |
Correct |
105 ms |
7800 KB |
Output is correct |
29 |
Correct |
358 ms |
11896 KB |
Output is correct |
30 |
Correct |
99 ms |
7808 KB |
Output is correct |
31 |
Correct |
352 ms |
12024 KB |
Output is correct |
32 |
Correct |
216 ms |
11896 KB |
Output is correct |
33 |
Correct |
210 ms |
11896 KB |
Output is correct |
34 |
Correct |
366 ms |
12316 KB |
Output is correct |
35 |
Correct |
4 ms |
4480 KB |
Output is correct |
36 |
Correct |
4 ms |
4480 KB |
Output is correct |
37 |
Correct |
76 ms |
7288 KB |
Output is correct |
38 |
Correct |
221 ms |
11512 KB |
Output is correct |
39 |
Correct |
248 ms |
11512 KB |
Output is correct |
40 |
Correct |
333 ms |
11516 KB |
Output is correct |
41 |
Correct |
335 ms |
11512 KB |
Output is correct |
42 |
Correct |
339 ms |
11768 KB |
Output is correct |
43 |
Correct |
225 ms |
11512 KB |
Output is correct |
44 |
Correct |
216 ms |
11516 KB |
Output is correct |
45 |
Correct |
209 ms |
11464 KB |
Output is correct |
46 |
Correct |
218 ms |
11512 KB |
Output is correct |
47 |
Correct |
275 ms |
11768 KB |
Output is correct |
48 |
Correct |
5 ms |
4480 KB |
Output is correct |
49 |
Correct |
5 ms |
4480 KB |
Output is correct |
50 |
Correct |
6 ms |
4480 KB |
Output is correct |
51 |
Correct |
4 ms |
4480 KB |
Output is correct |
52 |
Correct |
4 ms |
4480 KB |
Output is correct |
53 |
Correct |
6 ms |
4480 KB |
Output is correct |
54 |
Correct |
19 ms |
5120 KB |
Output is correct |
55 |
Correct |
25 ms |
5112 KB |
Output is correct |
56 |
Correct |
20 ms |
5120 KB |
Output is correct |
57 |
Correct |
19 ms |
5120 KB |
Output is correct |
58 |
Correct |
19 ms |
5120 KB |
Output is correct |
59 |
Correct |
20 ms |
5120 KB |
Output is correct |
60 |
Correct |
19 ms |
5120 KB |
Output is correct |
61 |
Correct |
72 ms |
7288 KB |
Output is correct |
62 |
Correct |
93 ms |
7672 KB |
Output is correct |
63 |
Correct |
229 ms |
11512 KB |
Output is correct |
64 |
Correct |
250 ms |
11452 KB |
Output is correct |
65 |
Correct |
297 ms |
11512 KB |
Output is correct |
66 |
Correct |
330 ms |
11512 KB |
Output is correct |
67 |
Correct |
366 ms |
11768 KB |
Output is correct |
68 |
Correct |
223 ms |
11512 KB |
Output is correct |
69 |
Correct |
255 ms |
11768 KB |
Output is correct |
70 |
Correct |
219 ms |
11512 KB |
Output is correct |
71 |
Correct |
245 ms |
11512 KB |
Output is correct |
72 |
Correct |
283 ms |
11512 KB |
Output is correct |
73 |
Correct |
314 ms |
11640 KB |
Output is correct |
74 |
Correct |
216 ms |
11512 KB |
Output is correct |
75 |
Correct |
223 ms |
11516 KB |
Output is correct |