#include <bits/stdc++.h>
using namespace std;
const int lg = 16;
int N;
vector<int> H;
struct SparseTable{
static pair<int, int> op(pair<int, int> a, pair<int, int> b){
return {std::min(a.first, b.first), std::max(a.second, b.second)};
}
vector<vector<pair<int, int>>> table;
SparseTable(){}
SparseTable(int N, vector<int> H): table(lg + 1, vector<pair<int, int>>(N)){
for(int i = 0; i < N; i++) table[0][i] = {H[i], H[i]};
for(int l = 1; l <= lg; l++) for(int i = 0; i + (1 << l) <= N; i++){
table[l][i] = op(table[l - 1][i], table[l - 1][i + (1 << l - 1)]);
}
}
pair<int, int> get(int L, int R) const {
const int l = __lg(R - L);
return op(table[l][L], table[l][R - (1 << l)]);
}
} st;
vector<vector<array<array<int, 3>, 2>>> table;
bool first = 1;
void init(int N, vector<int> H) {
::N = N;
::H = H;
st = SparseTable(N, H);
}
void init2(int D) {
first = 0;
table.assign(lg + 1, vector<array<array<int, 3>, 2>>(N));
for(int i = 0; i < N; i++){
int ok = i + 1, ng = N + 1;
auto check = [&](pair<int, int> x){
return H[i] < x.first && x.second < H[i] + D;
};
while(ng - ok > 1){
const int mid = (ok + ng) / 2;
(check(st.get(i + 1, mid)) ? ok : ng) = mid;
}
if(ok == N) table[0][i][0] = {N, 0, 0};
if(H[ok] < H[i]) table[0][i][0] = {ok, 0, 0};
else{
assert(H[ok] >= H[i] + D);
table[0][i][0] = {ok, 1, 0};
}
}
for(int i = 0; i < N; i++){
int ok = i + 1, ng = N + 1;
auto check = [&](pair<int, int> x){
return H[i] - D < x.first && x.second < H[i];
};
while(ng - ok > 1){
const int mid = (ok + ng) / 2;
(check(st.get(i + 1, mid)) ? ok : ng) = mid;
}
if(ok == N) table[0][i][1] = {N, 1, 0};
if(H[ok] > H[i]) table[0][i][1] = {ok, 1, 0};
else{
assert(H[ok] <= H[i] - D);
table[0][i][1] = {ok, 0, 1};
}
}
for(int l = 0; l < lg; l++) for(int i = 0; i < N; i++) for(int s = 0; s < 2; s++){
auto [i2, s2, c2] = table[l][i][s];
if(i2 == N){
table[l + 1][i][s] = {i2, s2, c2};
continue;
}
auto [i3, s3, c3] = table[l][i2][s2];
table[l + 1][i][s] = {i3, s3, c2 + c3};
}
}
int max_towers(int L, int R, int D) {
if(first) init2(D);
int i = L, s = 0, c = 1;
for(int l = lg; l >= 0; l--){
auto [i2, s2, c2] = table[l][i][s];
if(i2 > R) continue;
tie(i, s, c) = tuple{i2, s2, c + c2};
}
return c;
}
Compilation message
towers.cpp: In constructor 'SparseTable::SparseTable(int, std::vector<int>)':
towers.cpp:17:72: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
17 | table[l][i] = op(table[l - 1][i], table[l - 1][i + (1 << l - 1)]);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
67 ms |
65828 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Runtime error |
2 ms |
2768 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Runtime error |
2 ms |
2768 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
729 ms |
56168 KB |
Output is correct |
2 |
Correct |
1039 ms |
56592 KB |
Output is correct |
3 |
Correct |
925 ms |
56616 KB |
Output is correct |
4 |
Correct |
943 ms |
56616 KB |
Output is correct |
5 |
Correct |
999 ms |
56616 KB |
Output is correct |
6 |
Correct |
995 ms |
56704 KB |
Output is correct |
7 |
Correct |
996 ms |
56696 KB |
Output is correct |
8 |
Correct |
1067 ms |
56592 KB |
Output is correct |
9 |
Correct |
972 ms |
56616 KB |
Output is correct |
10 |
Correct |
1147 ms |
56696 KB |
Output is correct |
11 |
Correct |
1002 ms |
56692 KB |
Output is correct |
12 |
Correct |
1151 ms |
56700 KB |
Output is correct |
13 |
Correct |
929 ms |
56620 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
2 ms |
1448 KB |
Output is correct |
16 |
Correct |
1 ms |
1360 KB |
Output is correct |
17 |
Correct |
59 ms |
56700 KB |
Output is correct |
18 |
Correct |
58 ms |
56676 KB |
Output is correct |
19 |
Correct |
56 ms |
56604 KB |
Output is correct |
20 |
Correct |
61 ms |
56592 KB |
Output is correct |
21 |
Correct |
86 ms |
56704 KB |
Output is correct |
22 |
Correct |
57 ms |
56616 KB |
Output is correct |
23 |
Correct |
65 ms |
56616 KB |
Output is correct |
24 |
Correct |
56 ms |
56584 KB |
Output is correct |
25 |
Correct |
54 ms |
56660 KB |
Output is correct |
26 |
Correct |
81 ms |
56696 KB |
Output is correct |
27 |
Correct |
1 ms |
1360 KB |
Output is correct |
28 |
Correct |
1 ms |
1360 KB |
Output is correct |
29 |
Correct |
1 ms |
1360 KB |
Output is correct |
30 |
Correct |
1 ms |
1360 KB |
Output is correct |
31 |
Correct |
2 ms |
1360 KB |
Output is correct |
32 |
Correct |
1 ms |
1360 KB |
Output is correct |
33 |
Correct |
1 ms |
1360 KB |
Output is correct |
34 |
Correct |
1 ms |
1360 KB |
Output is correct |
35 |
Correct |
2 ms |
1360 KB |
Output is correct |
36 |
Correct |
1 ms |
1360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
23 ms |
26972 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Runtime error |
2 ms |
2768 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
67 ms |
65828 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |