#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;
vector<int> base;
vector<int> ord;
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]);
}
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);
}
base = vector<int>(s.begin(), s.end());
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);
ord.push_back(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);
ord.push_back(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]);
}
}
}
bool has[N];
int max_towers(int l, int r, int d) {
// find the last thing < d
int idx = lower_bound(store.begin(), store.end(), pair<int, int>{d, -1}) - store.begin() - 1;
int gone = sz(base) - store[idx].second;
memset(has, 0, sizeof(has));
for (int x : base) has[x] = 1;
// cerr << "> " << gone << endl;
for (int i = 0; i < gone; i++) has[ord[i]] = 0;
int ans = 0;
int first = n, last = -1;
for (int i = l; i <= r; i++) {
ans += has[i];
if (has[i]) {
first = min(first, i);
last = max(last, i);
// cerr << i << ' ';
}
}
// cerr << endl;
if (ans == 0) {
int mx = qry_max(l, r);
return max(1, (mx - d >= a[l]) + (mx - d >= a[r]));
}
bool one = 0, two = 0;
for (int i = l; i < first; i++) {
int x = qry_max(i, first);
if (a[i] <= x - d && x - d >= a[first]) {
one = 1;
break;
}
}
for (int i = last+1; i <= r; i++) {
int x = qry_max(last, i);
if (a[i] <= x - d && x - d >= a[last]) {
two = 1;
break;
}
}
ans += one;
ans += two;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
991 ms |
5680 KB |
Output is correct |
2 |
Correct |
2993 ms |
9296 KB |
Output is correct |
3 |
Correct |
2995 ms |
9296 KB |
Output is correct |
4 |
Correct |
3157 ms |
9296 KB |
Output is correct |
5 |
Correct |
3059 ms |
9408 KB |
Output is correct |
6 |
Correct |
3139 ms |
9264 KB |
Output is correct |
7 |
Correct |
2930 ms |
9296 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
51 ms |
8636 KB |
Output is correct |
37 |
Correct |
85 ms |
13336 KB |
Output is correct |
38 |
Correct |
101 ms |
13272 KB |
Output is correct |
39 |
Correct |
125 ms |
15032 KB |
Output is correct |
40 |
Correct |
134 ms |
15032 KB |
Output is correct |
41 |
Correct |
135 ms |
15036 KB |
Output is correct |
42 |
Correct |
128 ms |
14932 KB |
Output is correct |
43 |
Correct |
14 ms |
9296 KB |
Output is correct |
44 |
Correct |
19 ms |
9276 KB |
Output is correct |
45 |
Correct |
14 ms |
9244 KB |
Output is correct |
46 |
Correct |
18 ms |
9276 KB |
Output is correct |
47 |
Correct |
89 ms |
13232 KB |
Output is correct |
48 |
Correct |
154 ms |
14948 KB |
Output is correct |
49 |
Correct |
141 ms |
14968 KB |
Output is correct |
50 |
Correct |
14 ms |
9296 KB |
Output is correct |
51 |
Correct |
13 ms |
9268 KB |
Output is correct |
52 |
Correct |
95 ms |
13316 KB |
Output is correct |
53 |
Correct |
133 ms |
14944 KB |
Output is correct |
54 |
Correct |
131 ms |
14944 KB |
Output is correct |
55 |
Correct |
23 ms |
9196 KB |
Output is correct |
56 |
Correct |
16 ms |
9264 KB |
Output is correct |
57 |
Correct |
86 ms |
12560 KB |
Output is correct |
58 |
Correct |
89 ms |
13324 KB |
Output is correct |
59 |
Correct |
83 ms |
13252 KB |
Output is correct |
60 |
Correct |
124 ms |
15008 KB |
Output is correct |
61 |
Correct |
126 ms |
15040 KB |
Output is correct |
62 |
Correct |
136 ms |
15008 KB |
Output is correct |
63 |
Correct |
125 ms |
14948 KB |
Output is correct |
64 |
Correct |
14 ms |
9268 KB |
Output is correct |
65 |
Correct |
14 ms |
9260 KB |
Output is correct |
66 |
Correct |
18 ms |
9408 KB |
Output is correct |
67 |
Correct |
18 ms |
9268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4006 ms |
13160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1889 ms |
3452 KB |
Output is correct |
2 |
Execution timed out |
4073 ms |
13260 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
1 ms |
464 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
2 ms |
592 KB |
Output is correct |
29 |
Correct |
2 ms |
592 KB |
Output is correct |
30 |
Correct |
2 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
51 ms |
8636 KB |
Output is correct |
37 |
Correct |
85 ms |
13336 KB |
Output is correct |
38 |
Correct |
101 ms |
13272 KB |
Output is correct |
39 |
Correct |
125 ms |
15032 KB |
Output is correct |
40 |
Correct |
134 ms |
15032 KB |
Output is correct |
41 |
Correct |
135 ms |
15036 KB |
Output is correct |
42 |
Correct |
128 ms |
14932 KB |
Output is correct |
43 |
Correct |
14 ms |
9296 KB |
Output is correct |
44 |
Correct |
19 ms |
9276 KB |
Output is correct |
45 |
Correct |
14 ms |
9244 KB |
Output is correct |
46 |
Correct |
18 ms |
9276 KB |
Output is correct |
47 |
Correct |
89 ms |
13232 KB |
Output is correct |
48 |
Correct |
154 ms |
14948 KB |
Output is correct |
49 |
Correct |
141 ms |
14968 KB |
Output is correct |
50 |
Correct |
14 ms |
9296 KB |
Output is correct |
51 |
Correct |
13 ms |
9268 KB |
Output is correct |
52 |
Correct |
95 ms |
13316 KB |
Output is correct |
53 |
Correct |
133 ms |
14944 KB |
Output is correct |
54 |
Correct |
131 ms |
14944 KB |
Output is correct |
55 |
Correct |
23 ms |
9196 KB |
Output is correct |
56 |
Correct |
16 ms |
9264 KB |
Output is correct |
57 |
Correct |
86 ms |
12560 KB |
Output is correct |
58 |
Correct |
89 ms |
13324 KB |
Output is correct |
59 |
Correct |
83 ms |
13252 KB |
Output is correct |
60 |
Correct |
124 ms |
15008 KB |
Output is correct |
61 |
Correct |
126 ms |
15040 KB |
Output is correct |
62 |
Correct |
136 ms |
15008 KB |
Output is correct |
63 |
Correct |
125 ms |
14948 KB |
Output is correct |
64 |
Correct |
14 ms |
9268 KB |
Output is correct |
65 |
Correct |
14 ms |
9260 KB |
Output is correct |
66 |
Correct |
18 ms |
9408 KB |
Output is correct |
67 |
Correct |
18 ms |
9268 KB |
Output is correct |
68 |
Execution timed out |
4006 ms |
13160 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
991 ms |
5680 KB |
Output is correct |
2 |
Correct |
2993 ms |
9296 KB |
Output is correct |
3 |
Correct |
2995 ms |
9296 KB |
Output is correct |
4 |
Correct |
3157 ms |
9296 KB |
Output is correct |
5 |
Correct |
3059 ms |
9408 KB |
Output is correct |
6 |
Correct |
3139 ms |
9264 KB |
Output is correct |
7 |
Correct |
2930 ms |
9296 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
464 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
2 ms |
592 KB |
Output is correct |
17 |
Correct |
2 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
1 ms |
464 KB |
Output is correct |
22 |
Correct |
0 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
2 ms |
592 KB |
Output is correct |
26 |
Correct |
2 ms |
592 KB |
Output is correct |
27 |
Correct |
2 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
464 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
2 ms |
592 KB |
Output is correct |
32 |
Correct |
2 ms |
592 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
464 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
2 ms |
592 KB |
Output is correct |
42 |
Correct |
1 ms |
592 KB |
Output is correct |
43 |
Correct |
1 ms |
464 KB |
Output is correct |
44 |
Correct |
1 ms |
464 KB |
Output is correct |
45 |
Correct |
1 ms |
464 KB |
Output is correct |
46 |
Correct |
51 ms |
8636 KB |
Output is correct |
47 |
Correct |
85 ms |
13336 KB |
Output is correct |
48 |
Correct |
101 ms |
13272 KB |
Output is correct |
49 |
Correct |
125 ms |
15032 KB |
Output is correct |
50 |
Correct |
134 ms |
15032 KB |
Output is correct |
51 |
Correct |
135 ms |
15036 KB |
Output is correct |
52 |
Correct |
128 ms |
14932 KB |
Output is correct |
53 |
Correct |
14 ms |
9296 KB |
Output is correct |
54 |
Correct |
19 ms |
9276 KB |
Output is correct |
55 |
Correct |
14 ms |
9244 KB |
Output is correct |
56 |
Correct |
18 ms |
9276 KB |
Output is correct |
57 |
Correct |
89 ms |
13232 KB |
Output is correct |
58 |
Correct |
154 ms |
14948 KB |
Output is correct |
59 |
Correct |
141 ms |
14968 KB |
Output is correct |
60 |
Correct |
14 ms |
9296 KB |
Output is correct |
61 |
Correct |
13 ms |
9268 KB |
Output is correct |
62 |
Correct |
95 ms |
13316 KB |
Output is correct |
63 |
Correct |
133 ms |
14944 KB |
Output is correct |
64 |
Correct |
131 ms |
14944 KB |
Output is correct |
65 |
Correct |
23 ms |
9196 KB |
Output is correct |
66 |
Correct |
16 ms |
9264 KB |
Output is correct |
67 |
Correct |
86 ms |
12560 KB |
Output is correct |
68 |
Correct |
89 ms |
13324 KB |
Output is correct |
69 |
Correct |
83 ms |
13252 KB |
Output is correct |
70 |
Correct |
124 ms |
15008 KB |
Output is correct |
71 |
Correct |
126 ms |
15040 KB |
Output is correct |
72 |
Correct |
136 ms |
15008 KB |
Output is correct |
73 |
Correct |
125 ms |
14948 KB |
Output is correct |
74 |
Correct |
14 ms |
9268 KB |
Output is correct |
75 |
Correct |
14 ms |
9260 KB |
Output is correct |
76 |
Correct |
18 ms |
9408 KB |
Output is correct |
77 |
Correct |
18 ms |
9268 KB |
Output is correct |
78 |
Execution timed out |
4006 ms |
13160 KB |
Time limit exceeded |
79 |
Halted |
0 ms |
0 KB |
- |