#include "plants.h"
#include <bits/stdc++.h>
#define low(i) (i<<(__builtin_clz(i)-31+n_bits))-(1<<n_bits)
#define high(i) ((i+1)<<(__builtin_clz(i)-31+n_bits))-(1<<n_bits)-1
using namespace std;
vector<int> tallest;
vector<int> shortest;
int n_global;
const int n_bits=20;
const int inf = 1e9;
int arr[1<<(n_bits+1)];
int lazyadd[1<<(n_bits+1)];
struct segtree {
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;
}
}
int value(int node) {
arr[node] += lazyadd[node];
if(node<(1<<n_bits)) {
lazyadd[2*node] += lazyadd[node];
lazyadd[2*node+1] += lazyadd[node];
}
lazyadd[node] = 0;
return arr[node];
}
void update(int node, int left, int right, int change) {
if(right>=high(node) && left<=low(node)) {
lazyadd[node] += change;
} else if(right<low(node) || left>high(node)) {
return;
} else {
update(2*node, left, right, change);
update(2*node+1, left, right, change);
arr[node] = min(value(node*2), value(node*2+1));
}
}
void decr(int left, int right) {
update(1, left, right, -1);
}
int find_zero(int node) {
if(value(node)!=0) {
return -1;
}
if(node >= (1<<n_bits)) {
return arr[node]==0 ? node-(1<<n_bits) : -1;
}
int x = find_zero(node*2);
if(x!=-1) return x;
return find_zero(node*2+1);
}
};
// gives you the lexi smallest ordering given the doubled array r
vector<int> lexi_smallest(int k, vector<int> r) {
segtree s;
s.build(r);
vector<int> ret;
ret.resize(r.size());
queue<int> stash;
for(int i=0; i<(int)r.size(); i++) {
int p = s.find_zero(1);
while(p==-1) {
s.decr(stash.front(), 2*n_global-1);
p = s.find_zero(1);
stash.pop();
}
ret[p] = i;
s.update(1, p, p, 1e9);
s.decr(max(0, p-k+1), p);
if(p<k-1) {
stash.push(p-k+1+2*n_global);
}
}
return ret;
}
void init(int k, std::vector<int> r) {
n_global = r.size();
for(int i=0; i<n_global; i++) {
r.push_back(r[i]);
}
tallest = lexi_smallest(k,r);
for(int &i: r) i = k-1-i;
shortest = lexi_smallest(k,r);
}
int compare_plants(int x, int y) {
if(x>y) return -compare_plants(y,x);
if(tallest[x]>tallest[y] || shortest[y]>shortest[x+n_global]) return -1;
if(shortest[x]>shortest[y] || tallest[y]>tallest[x+n_global]) return 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
16 ms |
16768 KB |
Output is correct |
6 |
Correct |
80 ms |
19704 KB |
Output is correct |
7 |
Correct |
170 ms |
20120 KB |
Output is correct |
8 |
Correct |
824 ms |
26136 KB |
Output is correct |
9 |
Correct |
817 ms |
26164 KB |
Output is correct |
10 |
Correct |
823 ms |
26156 KB |
Output is correct |
11 |
Correct |
837 ms |
26156 KB |
Output is correct |
12 |
Correct |
819 ms |
26156 KB |
Output is correct |
13 |
Correct |
810 ms |
26156 KB |
Output is correct |
14 |
Correct |
822 ms |
26156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
17 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
16 ms |
16768 KB |
Output is correct |
6 |
Correct |
22 ms |
16768 KB |
Output is correct |
7 |
Correct |
115 ms |
19836 KB |
Output is correct |
8 |
Correct |
17 ms |
16768 KB |
Output is correct |
9 |
Correct |
23 ms |
16768 KB |
Output is correct |
10 |
Correct |
118 ms |
19832 KB |
Output is correct |
11 |
Correct |
108 ms |
19704 KB |
Output is correct |
12 |
Correct |
112 ms |
19832 KB |
Output is correct |
13 |
Correct |
118 ms |
19832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
17 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
16 ms |
16768 KB |
Output is correct |
6 |
Correct |
22 ms |
16768 KB |
Output is correct |
7 |
Correct |
115 ms |
19836 KB |
Output is correct |
8 |
Correct |
17 ms |
16768 KB |
Output is correct |
9 |
Correct |
23 ms |
16768 KB |
Output is correct |
10 |
Correct |
118 ms |
19832 KB |
Output is correct |
11 |
Correct |
108 ms |
19704 KB |
Output is correct |
12 |
Correct |
112 ms |
19832 KB |
Output is correct |
13 |
Correct |
118 ms |
19832 KB |
Output is correct |
14 |
Correct |
221 ms |
20120 KB |
Output is correct |
15 |
Correct |
1635 ms |
26136 KB |
Output is correct |
16 |
Correct |
207 ms |
20248 KB |
Output is correct |
17 |
Correct |
1653 ms |
26240 KB |
Output is correct |
18 |
Correct |
1058 ms |
26580 KB |
Output is correct |
19 |
Correct |
1047 ms |
26668 KB |
Output is correct |
20 |
Correct |
1662 ms |
26140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
94 ms |
19708 KB |
Output is correct |
4 |
Correct |
873 ms |
26232 KB |
Output is correct |
5 |
Correct |
969 ms |
26156 KB |
Output is correct |
6 |
Correct |
1295 ms |
26160 KB |
Output is correct |
7 |
Correct |
1539 ms |
26268 KB |
Output is correct |
8 |
Correct |
1612 ms |
26156 KB |
Output is correct |
9 |
Correct |
904 ms |
26156 KB |
Output is correct |
10 |
Correct |
909 ms |
26132 KB |
Output is correct |
11 |
Correct |
808 ms |
26284 KB |
Output is correct |
12 |
Correct |
909 ms |
26284 KB |
Output is correct |
13 |
Correct |
1048 ms |
26444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
15 ms |
16768 KB |
Output is correct |
6 |
Correct |
17 ms |
16768 KB |
Output is correct |
7 |
Correct |
31 ms |
17400 KB |
Output is correct |
8 |
Correct |
32 ms |
17400 KB |
Output is correct |
9 |
Correct |
32 ms |
17404 KB |
Output is correct |
10 |
Correct |
32 ms |
17400 KB |
Output is correct |
11 |
Correct |
32 ms |
17400 KB |
Output is correct |
12 |
Correct |
33 ms |
17400 KB |
Output is correct |
13 |
Correct |
32 ms |
17404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
17 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
16 ms |
16768 KB |
Output is correct |
5 |
Correct |
21 ms |
16768 KB |
Output is correct |
6 |
Correct |
955 ms |
26160 KB |
Output is correct |
7 |
Correct |
1228 ms |
26156 KB |
Output is correct |
8 |
Correct |
1432 ms |
26284 KB |
Output is correct |
9 |
Correct |
1579 ms |
26288 KB |
Output is correct |
10 |
Correct |
885 ms |
26156 KB |
Output is correct |
11 |
Correct |
1189 ms |
26408 KB |
Output is correct |
12 |
Correct |
845 ms |
26140 KB |
Output is correct |
13 |
Correct |
958 ms |
26156 KB |
Output is correct |
14 |
Correct |
1232 ms |
26196 KB |
Output is correct |
15 |
Correct |
1449 ms |
26156 KB |
Output is correct |
16 |
Correct |
854 ms |
26160 KB |
Output is correct |
17 |
Correct |
950 ms |
26160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16768 KB |
Output is correct |
2 |
Correct |
15 ms |
16768 KB |
Output is correct |
3 |
Correct |
15 ms |
16768 KB |
Output is correct |
4 |
Correct |
15 ms |
16768 KB |
Output is correct |
5 |
Correct |
16 ms |
16768 KB |
Output is correct |
6 |
Correct |
80 ms |
19704 KB |
Output is correct |
7 |
Correct |
170 ms |
20120 KB |
Output is correct |
8 |
Correct |
824 ms |
26136 KB |
Output is correct |
9 |
Correct |
817 ms |
26164 KB |
Output is correct |
10 |
Correct |
823 ms |
26156 KB |
Output is correct |
11 |
Correct |
837 ms |
26156 KB |
Output is correct |
12 |
Correct |
819 ms |
26156 KB |
Output is correct |
13 |
Correct |
810 ms |
26156 KB |
Output is correct |
14 |
Correct |
822 ms |
26156 KB |
Output is correct |
15 |
Correct |
16 ms |
16768 KB |
Output is correct |
16 |
Correct |
15 ms |
16768 KB |
Output is correct |
17 |
Correct |
17 ms |
16768 KB |
Output is correct |
18 |
Correct |
15 ms |
16768 KB |
Output is correct |
19 |
Correct |
16 ms |
16768 KB |
Output is correct |
20 |
Correct |
22 ms |
16768 KB |
Output is correct |
21 |
Correct |
115 ms |
19836 KB |
Output is correct |
22 |
Correct |
17 ms |
16768 KB |
Output is correct |
23 |
Correct |
23 ms |
16768 KB |
Output is correct |
24 |
Correct |
118 ms |
19832 KB |
Output is correct |
25 |
Correct |
108 ms |
19704 KB |
Output is correct |
26 |
Correct |
112 ms |
19832 KB |
Output is correct |
27 |
Correct |
118 ms |
19832 KB |
Output is correct |
28 |
Correct |
221 ms |
20120 KB |
Output is correct |
29 |
Correct |
1635 ms |
26136 KB |
Output is correct |
30 |
Correct |
207 ms |
20248 KB |
Output is correct |
31 |
Correct |
1653 ms |
26240 KB |
Output is correct |
32 |
Correct |
1058 ms |
26580 KB |
Output is correct |
33 |
Correct |
1047 ms |
26668 KB |
Output is correct |
34 |
Correct |
1662 ms |
26140 KB |
Output is correct |
35 |
Correct |
15 ms |
16768 KB |
Output is correct |
36 |
Correct |
15 ms |
16768 KB |
Output is correct |
37 |
Correct |
94 ms |
19708 KB |
Output is correct |
38 |
Correct |
873 ms |
26232 KB |
Output is correct |
39 |
Correct |
969 ms |
26156 KB |
Output is correct |
40 |
Correct |
1295 ms |
26160 KB |
Output is correct |
41 |
Correct |
1539 ms |
26268 KB |
Output is correct |
42 |
Correct |
1612 ms |
26156 KB |
Output is correct |
43 |
Correct |
904 ms |
26156 KB |
Output is correct |
44 |
Correct |
909 ms |
26132 KB |
Output is correct |
45 |
Correct |
808 ms |
26284 KB |
Output is correct |
46 |
Correct |
909 ms |
26284 KB |
Output is correct |
47 |
Correct |
1048 ms |
26444 KB |
Output is correct |
48 |
Correct |
15 ms |
16768 KB |
Output is correct |
49 |
Correct |
15 ms |
16768 KB |
Output is correct |
50 |
Correct |
15 ms |
16768 KB |
Output is correct |
51 |
Correct |
15 ms |
16768 KB |
Output is correct |
52 |
Correct |
15 ms |
16768 KB |
Output is correct |
53 |
Correct |
17 ms |
16768 KB |
Output is correct |
54 |
Correct |
31 ms |
17400 KB |
Output is correct |
55 |
Correct |
32 ms |
17400 KB |
Output is correct |
56 |
Correct |
32 ms |
17404 KB |
Output is correct |
57 |
Correct |
32 ms |
17400 KB |
Output is correct |
58 |
Correct |
32 ms |
17400 KB |
Output is correct |
59 |
Correct |
33 ms |
17400 KB |
Output is correct |
60 |
Correct |
32 ms |
17404 KB |
Output is correct |
61 |
Correct |
92 ms |
19704 KB |
Output is correct |
62 |
Correct |
166 ms |
19992 KB |
Output is correct |
63 |
Correct |
857 ms |
26160 KB |
Output is correct |
64 |
Correct |
964 ms |
26300 KB |
Output is correct |
65 |
Correct |
1236 ms |
26192 KB |
Output is correct |
66 |
Correct |
1461 ms |
26156 KB |
Output is correct |
67 |
Correct |
1564 ms |
26164 KB |
Output is correct |
68 |
Correct |
905 ms |
26136 KB |
Output is correct |
69 |
Correct |
1230 ms |
26344 KB |
Output is correct |
70 |
Correct |
861 ms |
26156 KB |
Output is correct |
71 |
Correct |
972 ms |
26156 KB |
Output is correct |
72 |
Correct |
1283 ms |
26288 KB |
Output is correct |
73 |
Correct |
1518 ms |
26104 KB |
Output is correct |
74 |
Correct |
880 ms |
26232 KB |
Output is correct |
75 |
Correct |
953 ms |
26156 KB |
Output is correct |