#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const ll INF = 1e18+10;
const int L = 20;
int n, a[N], st[N][L];
vector<pair<int, int>> store;
void build_rmq() {
for (int i = 0; i < n; i++) st[i][0] = a[i];
for (int k = 2, l = 1; k <= n; k *= 2, l++) {
for (int i = 0; i + k <= n; i++) {
st[i][l] = max(st[i][l-1], st[i + k / 2][l-1]);
}
}
}
int qry_max(int l, int r) {
int len = r - l + 1;
int use = 31 - __builtin_clz(len);
return max(st[l][use], st[r - (1 << use) + 1][use]);
/*
int ans = 0;
for (int i = l; i <= r; i++) ans = max(ans, a[i]);
return ans;
*/
}
void init(int _n, vector<int> H) {
n = _n;
for (int i = 0; i < n; i++) {
a[i] = H[i];
}
build_rmq();
set<int> s;
for (int i = 0; i < n; i++) {
bool use = 1;
if (i && a[i-1] < a[i]) use = 0;
if (i < n-1 && a[i+1] < a[i]) use = 0;
if (use) s.insert(i);
}
auto f = [&](int i) {
auto it = s.lower_bound(i); assert(*it == i);
--it;
return qry_max(*it, i) - max(a[*it], a[i]);
};
set<pair<int, int>> q;
auto get_v = [&](int x) {
return pair<int, int>{f(x), x};
};
for (auto it = next(s.begin()); it != s.end(); it++) {
q.insert(get_v(*it));
}
vector<pair<int, int>> v;
v.emplace_back(0, sz(s)); // d > 0, ans = sz(s)
while (sz(q)) {
auto [d, i] = *q.begin(); q.erase(q.begin());
v.emplace_back(d, -1); // need to update v.back().second
auto it = s.lower_bound(i); assert(*it == i);
int one = i;
int two = *prev(it);
if (next(it) != s.end()) {
q.erase(get_v(*next(it)));
}
if (prev(it) != s.begin()) {
q.erase(get_v(two));
}
if (a[one] > a[two]) {
s.erase(one);
it = s.lower_bound(two);
if (next(it) != s.end()) {
q.insert(get_v(*next(it)));
}
if (it != s.begin()) {
q.insert(get_v(*it));
}
} else {
s.erase(two);
it = s.lower_bound(one);
if (next(it) != s.end()) {
q.insert(get_v(*next(it)));
}
if (it != s.begin()) {
q.insert(get_v(*it));
}
}
v.back().second = sz(s);
}
store.push_back(v[0]);
for (int i = 1; i < sz(v); i++) {
if (v[i].first <= store.back().first) {
store.back().second = v[i].second;
} else {
store.push_back(v[i]);
}
}
}
int max_towers(int l, int r, int d) {
assert(l == 0 && r == n-1);
// find the last thing < d
int i = lower_bound(store.begin(), store.end(), pair<int, int>{d, -1}) - store.begin() - 1;
return store[i].second;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
14 ms |
10848 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
92 ms |
24208 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
249 ms |
3244 KB |
Output is correct |
2 |
Correct |
682 ms |
12988 KB |
Output is correct |
3 |
Correct |
753 ms |
13060 KB |
Output is correct |
4 |
Correct |
920 ms |
14624 KB |
Output is correct |
5 |
Correct |
646 ms |
14536 KB |
Output is correct |
6 |
Correct |
735 ms |
14608 KB |
Output is correct |
7 |
Correct |
718 ms |
14628 KB |
Output is correct |
8 |
Correct |
546 ms |
9260 KB |
Output is correct |
9 |
Correct |
489 ms |
9280 KB |
Output is correct |
10 |
Correct |
755 ms |
9264 KB |
Output is correct |
11 |
Correct |
599 ms |
9296 KB |
Output is correct |
12 |
Correct |
91 ms |
12952 KB |
Output is correct |
13 |
Correct |
136 ms |
14612 KB |
Output is correct |
14 |
Correct |
128 ms |
14620 KB |
Output is correct |
15 |
Correct |
21 ms |
9280 KB |
Output is correct |
16 |
Correct |
16 ms |
9272 KB |
Output is correct |
17 |
Correct |
82 ms |
12368 KB |
Output is correct |
18 |
Correct |
95 ms |
13084 KB |
Output is correct |
19 |
Correct |
90 ms |
13040 KB |
Output is correct |
20 |
Correct |
129 ms |
14504 KB |
Output is correct |
21 |
Correct |
131 ms |
14628 KB |
Output is correct |
22 |
Correct |
147 ms |
14664 KB |
Output is correct |
23 |
Correct |
130 ms |
14624 KB |
Output is correct |
24 |
Correct |
14 ms |
9296 KB |
Output is correct |
25 |
Correct |
14 ms |
9260 KB |
Output is correct |
26 |
Correct |
14 ms |
9296 KB |
Output is correct |
27 |
Correct |
14 ms |
9276 KB |
Output is correct |
28 |
Correct |
2 ms |
560 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
556 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
2 ms |
592 KB |
Output is correct |
37 |
Correct |
2 ms |
464 KB |
Output is correct |
38 |
Correct |
2 ms |
560 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
1 ms |
464 KB |
Output is correct |
41 |
Correct |
1 ms |
464 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
14 ms |
10848 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |