#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)]);
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
424 ms |
33956 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
1360 KB |
Output is correct |
3 |
Correct |
2 ms |
1408 KB |
Output is correct |
4 |
Correct |
1 ms |
1360 KB |
Output is correct |
5 |
Correct |
2 ms |
1360 KB |
Output is correct |
6 |
Correct |
2 ms |
1360 KB |
Output is correct |
7 |
Correct |
2 ms |
1360 KB |
Output is correct |
8 |
Correct |
2 ms |
1360 KB |
Output is correct |
9 |
Correct |
1 ms |
1360 KB |
Output is correct |
10 |
Correct |
1 ms |
1360 KB |
Output is correct |
11 |
Correct |
2 ms |
1360 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
1360 KB |
Output is correct |
14 |
Correct |
2 ms |
1460 KB |
Output is correct |
15 |
Correct |
2 ms |
1360 KB |
Output is correct |
16 |
Correct |
1 ms |
1360 KB |
Output is correct |
17 |
Correct |
2 ms |
1360 KB |
Output is correct |
18 |
Correct |
1 ms |
1360 KB |
Output is correct |
19 |
Correct |
2 ms |
1360 KB |
Output is correct |
20 |
Correct |
2 ms |
1360 KB |
Output is correct |
21 |
Correct |
2 ms |
1360 KB |
Output is correct |
22 |
Correct |
1 ms |
1360 KB |
Output is correct |
23 |
Correct |
1 ms |
1360 KB |
Output is correct |
24 |
Correct |
2 ms |
1432 KB |
Output is correct |
25 |
Correct |
1 ms |
720 KB |
Output is correct |
26 |
Correct |
1 ms |
1360 KB |
Output is correct |
27 |
Correct |
2 ms |
1360 KB |
Output is correct |
28 |
Correct |
2 ms |
1360 KB |
Output is correct |
29 |
Correct |
2 ms |
1360 KB |
Output is correct |
30 |
Correct |
1 ms |
1360 KB |
Output is correct |
31 |
Correct |
1 ms |
1360 KB |
Output is correct |
32 |
Correct |
3 ms |
1448 KB |
Output is correct |
33 |
Correct |
1 ms |
1360 KB |
Output is correct |
34 |
Correct |
2 ms |
1360 KB |
Output is correct |
35 |
Correct |
2 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
1360 KB |
Output is correct |
3 |
Correct |
2 ms |
1408 KB |
Output is correct |
4 |
Correct |
1 ms |
1360 KB |
Output is correct |
5 |
Correct |
2 ms |
1360 KB |
Output is correct |
6 |
Correct |
2 ms |
1360 KB |
Output is correct |
7 |
Correct |
2 ms |
1360 KB |
Output is correct |
8 |
Correct |
2 ms |
1360 KB |
Output is correct |
9 |
Correct |
1 ms |
1360 KB |
Output is correct |
10 |
Correct |
1 ms |
1360 KB |
Output is correct |
11 |
Correct |
2 ms |
1360 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
1360 KB |
Output is correct |
14 |
Correct |
2 ms |
1460 KB |
Output is correct |
15 |
Correct |
2 ms |
1360 KB |
Output is correct |
16 |
Correct |
1 ms |
1360 KB |
Output is correct |
17 |
Correct |
2 ms |
1360 KB |
Output is correct |
18 |
Correct |
1 ms |
1360 KB |
Output is correct |
19 |
Correct |
2 ms |
1360 KB |
Output is correct |
20 |
Correct |
2 ms |
1360 KB |
Output is correct |
21 |
Correct |
2 ms |
1360 KB |
Output is correct |
22 |
Correct |
1 ms |
1360 KB |
Output is correct |
23 |
Correct |
1 ms |
1360 KB |
Output is correct |
24 |
Correct |
2 ms |
1432 KB |
Output is correct |
25 |
Correct |
1 ms |
720 KB |
Output is correct |
26 |
Correct |
1 ms |
1360 KB |
Output is correct |
27 |
Correct |
2 ms |
1360 KB |
Output is correct |
28 |
Correct |
2 ms |
1360 KB |
Output is correct |
29 |
Correct |
2 ms |
1360 KB |
Output is correct |
30 |
Correct |
1 ms |
1360 KB |
Output is correct |
31 |
Correct |
1 ms |
1360 KB |
Output is correct |
32 |
Correct |
3 ms |
1448 KB |
Output is correct |
33 |
Correct |
1 ms |
1360 KB |
Output is correct |
34 |
Correct |
2 ms |
1360 KB |
Output is correct |
35 |
Correct |
2 ms |
1360 KB |
Output is correct |
36 |
Correct |
37 ms |
36960 KB |
Output is correct |
37 |
Correct |
77 ms |
56592 KB |
Output is correct |
38 |
Correct |
66 ms |
56656 KB |
Output is correct |
39 |
Correct |
72 ms |
56596 KB |
Output is correct |
40 |
Correct |
72 ms |
56696 KB |
Output is correct |
41 |
Correct |
66 ms |
56696 KB |
Output is correct |
42 |
Correct |
61 ms |
56664 KB |
Output is correct |
43 |
Correct |
79 ms |
56700 KB |
Output is correct |
44 |
Correct |
62 ms |
56696 KB |
Output is correct |
45 |
Correct |
77 ms |
56684 KB |
Output is correct |
46 |
Correct |
64 ms |
56700 KB |
Output is correct |
47 |
Correct |
57 ms |
56628 KB |
Output is correct |
48 |
Correct |
62 ms |
56700 KB |
Output is correct |
49 |
Correct |
57 ms |
56676 KB |
Output is correct |
50 |
Correct |
59 ms |
56660 KB |
Output is correct |
51 |
Correct |
90 ms |
56604 KB |
Output is correct |
52 |
Correct |
56 ms |
56676 KB |
Output is correct |
53 |
Correct |
59 ms |
56672 KB |
Output is correct |
54 |
Correct |
55 ms |
56588 KB |
Output is correct |
55 |
Correct |
53 ms |
56620 KB |
Output is correct |
56 |
Correct |
80 ms |
56616 KB |
Output is correct |
57 |
Correct |
64 ms |
54796 KB |
Output is correct |
58 |
Correct |
64 ms |
56696 KB |
Output is correct |
59 |
Correct |
61 ms |
56676 KB |
Output is correct |
60 |
Correct |
56 ms |
56608 KB |
Output is correct |
61 |
Correct |
62 ms |
56704 KB |
Output is correct |
62 |
Correct |
67 ms |
56624 KB |
Output is correct |
63 |
Correct |
58 ms |
56664 KB |
Output is correct |
64 |
Correct |
88 ms |
56600 KB |
Output is correct |
65 |
Correct |
73 ms |
56576 KB |
Output is correct |
66 |
Correct |
77 ms |
56700 KB |
Output is correct |
67 |
Correct |
61 ms |
56696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
866 ms |
56216 KB |
Output is correct |
2 |
Correct |
912 ms |
56696 KB |
Output is correct |
3 |
Correct |
1038 ms |
56672 KB |
Output is correct |
4 |
Correct |
1035 ms |
56580 KB |
Output is correct |
5 |
Correct |
894 ms |
56616 KB |
Output is correct |
6 |
Correct |
1008 ms |
56696 KB |
Output is correct |
7 |
Correct |
947 ms |
56576 KB |
Output is correct |
8 |
Correct |
964 ms |
56616 KB |
Output is correct |
9 |
Correct |
817 ms |
56696 KB |
Output is correct |
10 |
Correct |
913 ms |
56700 KB |
Output is correct |
11 |
Correct |
1047 ms |
56620 KB |
Output is correct |
12 |
Correct |
950 ms |
56612 KB |
Output is correct |
13 |
Correct |
1004 ms |
56704 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
1 ms |
1360 KB |
Output is correct |
16 |
Correct |
1 ms |
1360 KB |
Output is correct |
17 |
Correct |
58 ms |
56588 KB |
Output is correct |
18 |
Correct |
57 ms |
56596 KB |
Output is correct |
19 |
Correct |
59 ms |
56640 KB |
Output is correct |
20 |
Correct |
60 ms |
56608 KB |
Output is correct |
21 |
Correct |
84 ms |
56636 KB |
Output is correct |
22 |
Correct |
68 ms |
56576 KB |
Output is correct |
23 |
Correct |
55 ms |
56632 KB |
Output is correct |
24 |
Correct |
55 ms |
56700 KB |
Output is correct |
25 |
Correct |
56 ms |
56616 KB |
Output is correct |
26 |
Correct |
88 ms |
56708 KB |
Output is correct |
27 |
Correct |
2 ms |
1360 KB |
Output is correct |
28 |
Correct |
1 ms |
1360 KB |
Output is correct |
29 |
Correct |
2 ms |
1484 KB |
Output is correct |
30 |
Correct |
1 ms |
1360 KB |
Output is correct |
31 |
Correct |
1 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 |
1 ms |
1360 KB |
Output is correct |
36 |
Correct |
1 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
277 ms |
13972 KB |
2nd lines differ - on the 1st token, expected: '7063', found: '7197' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
1360 KB |
Output is correct |
3 |
Correct |
2 ms |
1408 KB |
Output is correct |
4 |
Correct |
1 ms |
1360 KB |
Output is correct |
5 |
Correct |
2 ms |
1360 KB |
Output is correct |
6 |
Correct |
2 ms |
1360 KB |
Output is correct |
7 |
Correct |
2 ms |
1360 KB |
Output is correct |
8 |
Correct |
2 ms |
1360 KB |
Output is correct |
9 |
Correct |
1 ms |
1360 KB |
Output is correct |
10 |
Correct |
1 ms |
1360 KB |
Output is correct |
11 |
Correct |
2 ms |
1360 KB |
Output is correct |
12 |
Correct |
0 ms |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
1360 KB |
Output is correct |
14 |
Correct |
2 ms |
1460 KB |
Output is correct |
15 |
Correct |
2 ms |
1360 KB |
Output is correct |
16 |
Correct |
1 ms |
1360 KB |
Output is correct |
17 |
Correct |
2 ms |
1360 KB |
Output is correct |
18 |
Correct |
1 ms |
1360 KB |
Output is correct |
19 |
Correct |
2 ms |
1360 KB |
Output is correct |
20 |
Correct |
2 ms |
1360 KB |
Output is correct |
21 |
Correct |
2 ms |
1360 KB |
Output is correct |
22 |
Correct |
1 ms |
1360 KB |
Output is correct |
23 |
Correct |
1 ms |
1360 KB |
Output is correct |
24 |
Correct |
2 ms |
1432 KB |
Output is correct |
25 |
Correct |
1 ms |
720 KB |
Output is correct |
26 |
Correct |
1 ms |
1360 KB |
Output is correct |
27 |
Correct |
2 ms |
1360 KB |
Output is correct |
28 |
Correct |
2 ms |
1360 KB |
Output is correct |
29 |
Correct |
2 ms |
1360 KB |
Output is correct |
30 |
Correct |
1 ms |
1360 KB |
Output is correct |
31 |
Correct |
1 ms |
1360 KB |
Output is correct |
32 |
Correct |
3 ms |
1448 KB |
Output is correct |
33 |
Correct |
1 ms |
1360 KB |
Output is correct |
34 |
Correct |
2 ms |
1360 KB |
Output is correct |
35 |
Correct |
2 ms |
1360 KB |
Output is correct |
36 |
Correct |
37 ms |
36960 KB |
Output is correct |
37 |
Correct |
77 ms |
56592 KB |
Output is correct |
38 |
Correct |
66 ms |
56656 KB |
Output is correct |
39 |
Correct |
72 ms |
56596 KB |
Output is correct |
40 |
Correct |
72 ms |
56696 KB |
Output is correct |
41 |
Correct |
66 ms |
56696 KB |
Output is correct |
42 |
Correct |
61 ms |
56664 KB |
Output is correct |
43 |
Correct |
79 ms |
56700 KB |
Output is correct |
44 |
Correct |
62 ms |
56696 KB |
Output is correct |
45 |
Correct |
77 ms |
56684 KB |
Output is correct |
46 |
Correct |
64 ms |
56700 KB |
Output is correct |
47 |
Correct |
57 ms |
56628 KB |
Output is correct |
48 |
Correct |
62 ms |
56700 KB |
Output is correct |
49 |
Correct |
57 ms |
56676 KB |
Output is correct |
50 |
Correct |
59 ms |
56660 KB |
Output is correct |
51 |
Correct |
90 ms |
56604 KB |
Output is correct |
52 |
Correct |
56 ms |
56676 KB |
Output is correct |
53 |
Correct |
59 ms |
56672 KB |
Output is correct |
54 |
Correct |
55 ms |
56588 KB |
Output is correct |
55 |
Correct |
53 ms |
56620 KB |
Output is correct |
56 |
Correct |
80 ms |
56616 KB |
Output is correct |
57 |
Correct |
64 ms |
54796 KB |
Output is correct |
58 |
Correct |
64 ms |
56696 KB |
Output is correct |
59 |
Correct |
61 ms |
56676 KB |
Output is correct |
60 |
Correct |
56 ms |
56608 KB |
Output is correct |
61 |
Correct |
62 ms |
56704 KB |
Output is correct |
62 |
Correct |
67 ms |
56624 KB |
Output is correct |
63 |
Correct |
58 ms |
56664 KB |
Output is correct |
64 |
Correct |
88 ms |
56600 KB |
Output is correct |
65 |
Correct |
73 ms |
56576 KB |
Output is correct |
66 |
Correct |
77 ms |
56700 KB |
Output is correct |
67 |
Correct |
61 ms |
56696 KB |
Output is correct |
68 |
Correct |
866 ms |
56216 KB |
Output is correct |
69 |
Correct |
912 ms |
56696 KB |
Output is correct |
70 |
Correct |
1038 ms |
56672 KB |
Output is correct |
71 |
Correct |
1035 ms |
56580 KB |
Output is correct |
72 |
Correct |
894 ms |
56616 KB |
Output is correct |
73 |
Correct |
1008 ms |
56696 KB |
Output is correct |
74 |
Correct |
947 ms |
56576 KB |
Output is correct |
75 |
Correct |
964 ms |
56616 KB |
Output is correct |
76 |
Correct |
817 ms |
56696 KB |
Output is correct |
77 |
Correct |
913 ms |
56700 KB |
Output is correct |
78 |
Correct |
1047 ms |
56620 KB |
Output is correct |
79 |
Correct |
950 ms |
56612 KB |
Output is correct |
80 |
Correct |
1004 ms |
56704 KB |
Output is correct |
81 |
Correct |
0 ms |
208 KB |
Output is correct |
82 |
Correct |
1 ms |
1360 KB |
Output is correct |
83 |
Correct |
1 ms |
1360 KB |
Output is correct |
84 |
Correct |
58 ms |
56588 KB |
Output is correct |
85 |
Correct |
57 ms |
56596 KB |
Output is correct |
86 |
Correct |
59 ms |
56640 KB |
Output is correct |
87 |
Correct |
60 ms |
56608 KB |
Output is correct |
88 |
Correct |
84 ms |
56636 KB |
Output is correct |
89 |
Correct |
68 ms |
56576 KB |
Output is correct |
90 |
Correct |
55 ms |
56632 KB |
Output is correct |
91 |
Correct |
55 ms |
56700 KB |
Output is correct |
92 |
Correct |
56 ms |
56616 KB |
Output is correct |
93 |
Correct |
88 ms |
56708 KB |
Output is correct |
94 |
Correct |
2 ms |
1360 KB |
Output is correct |
95 |
Correct |
1 ms |
1360 KB |
Output is correct |
96 |
Correct |
2 ms |
1484 KB |
Output is correct |
97 |
Correct |
1 ms |
1360 KB |
Output is correct |
98 |
Correct |
1 ms |
1360 KB |
Output is correct |
99 |
Correct |
1 ms |
1360 KB |
Output is correct |
100 |
Correct |
1 ms |
1360 KB |
Output is correct |
101 |
Correct |
1 ms |
1360 KB |
Output is correct |
102 |
Correct |
1 ms |
1360 KB |
Output is correct |
103 |
Correct |
1 ms |
1360 KB |
Output is correct |
104 |
Correct |
837 ms |
50364 KB |
Output is correct |
105 |
Correct |
1000 ms |
56700 KB |
Output is correct |
106 |
Correct |
1084 ms |
56664 KB |
Output is correct |
107 |
Correct |
1048 ms |
56700 KB |
Output is correct |
108 |
Correct |
967 ms |
56696 KB |
Output is correct |
109 |
Correct |
1031 ms |
56636 KB |
Output is correct |
110 |
Correct |
933 ms |
56584 KB |
Output is correct |
111 |
Correct |
824 ms |
56576 KB |
Output is correct |
112 |
Correct |
1102 ms |
56616 KB |
Output is correct |
113 |
Correct |
877 ms |
56684 KB |
Output is correct |
114 |
Correct |
1079 ms |
56568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
424 ms |
33956 KB |
31st lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |