// time_limit/solution-ayaze-d-equals.cpp
#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int kLogN = 17;
const int kInf = 2'000'000'000;
struct Fenwick {
int n;
vector<vector<int>> tree;
vector<vector<int>> nums;
void init(int _n, vector<tuple<int, int, int>> vec) {
n = _n;
tree.clear();
nums.clear();
tree.resize(n+5);
nums.resize(n+5);
for (auto &[row, col, _] : vec) { // normalize
row++; col++;
}
for (auto [row, col, _] : vec) {
for (int i = row ; i < n+5 ; i += i & -i) {
nums[i].push_back(col);
}
}
for (int i = 0 ; i < n+5 ; i++) {
nums[i].push_back(-1);
sort(nums[i].begin(), nums[i].end());
nums[i].erase(unique(nums[i].begin(), nums[i].end()), nums[i].end());
tree[i].resize(nums[i].size());
}
for (auto [row, col, val] : vec) {
update(row, col, val);
}
}
int getIdx(int r, int val) {
return upper_bound(nums[r].begin(), nums[r].end(), val) - nums[r].begin() - 1;
}
void update(int r, int c, int val) {
for (int i = r ; i < n+5 ; i += i & -i) {
int idx = getIdx(i, c);
for (int j = idx ; j < static_cast<int>(tree[i].size()) ; j += j & -j) {
tree[i][j] += val;
}
}
}
int query(int l, int r) {
l++; r++;
int ret = 0;
for (int x = l ; x > 0 ; x -= x & -x) {
int idx = getIdx(x, r);
for (int j = idx ; j > 0 ; j -= j & -j) {
ret += tree[x][j];
}
}
return ret;
}
};
vector<int> D_bounds_left, D_bounds_right;
vector<int> left_lower, right_lower;
int N;
void init(int _N, std::vector<int> H) {
N = _N;
vector<pair<int, int>> ordered_H;
for (int i = 0 ; i < N ; i++) {
ordered_H.push_back({H[i], i});
}
sort(ordered_H.begin(), ordered_H.end());
left_lower.resize(N, -1);
right_lower.resize(N, -1);
set<int> active;
for (auto [_, i] : ordered_H) {
{
auto it = active.lower_bound(i);
if (it != active.begin()) {
it--;
left_lower[i] = *it;
}
}
{
auto it = active.upper_bound(i);
if (it != active.end()) {
right_lower[i] = *it;
}
}
active.insert(i);
}
vector<vector<int>> sparse_table(N, vector<int>(kLogN, -1));
for (int i = 0 ; i < N ; i++) {
sparse_table[i][0] = H[i];
}
for (int j = 1 ; j < kLogN ; j++) {
for (int i = 0 ; i < N ; i++) {
if (i + (1 << (j-1)) < N) {
sparse_table[i][j] = max(sparse_table[i][j-1], sparse_table[i + (1 << (j-1))][j-1]);
}
}
}
function<int(int, int)> queryMax = [&](int l, int r) -> int {
int lg = 0;
while (l + (1 << lg) - 1 <= r) lg++;
lg--;
return max(sparse_table[l][lg], sparse_table[r - (1 << lg) + 1][lg]);
};
for (int i = 0 ; i < N ; i++) {
int D_max_left = kInf, D_max_right = kInf;
if (left_lower[i] != -1) {
D_max_left = -1;
int mx = queryMax(left_lower[i], i);
D_max_left = mx - H[i];
}
if (right_lower[i] != -1) {
D_max_right = -1;
int mx = queryMax(i, right_lower[i]);
D_max_right = mx - H[i];
}
D_bounds_left.push_back(D_max_left);
D_bounds_right.push_back(D_max_right);
}
}
Fenwick ft;
int last_D = -1;
void build(int D) {
vector<tuple<int, int, int>> vec;
for (int i = 0 ; i < N ; i++) {
int l, r;
if (left_lower[i] == -1 || D_bounds_left[i] >= D) l = 0;
else l = left_lower[i]+1;
if (right_lower[i] == -1 || D_bounds_right[i] >= D) r = N-1;
else r = right_lower[i]-1;
// l <= L <= i <= R <= r
// (l, i) to (i, r)
// add(l, i)
// dec(l, r+1)
// dec(i+1, i)
// add(i+1, r+1)
vec.push_back(make_tuple(l, i, 1));
vec.push_back(make_tuple(l, r+1, -1));
vec.push_back(make_tuple(i+1, i, -1));
vec.push_back(make_tuple(i+1, r+1, 1));
}
ft.init(N, vec);
}
int max_towers(int L, int R, int D) {
if (D != last_D) {
last_D = D;
build(D);
}
return ft.query(L, R);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4051 ms |
31984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
388 KB |
Output is correct |
2 |
Correct |
9 ms |
1024 KB |
Output is correct |
3 |
Correct |
8 ms |
1008 KB |
Output is correct |
4 |
Correct |
8 ms |
976 KB |
Output is correct |
5 |
Correct |
6 ms |
1024 KB |
Output is correct |
6 |
Correct |
7 ms |
976 KB |
Output is correct |
7 |
Correct |
7 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
976 KB |
Output is correct |
9 |
Correct |
12 ms |
1192 KB |
Output is correct |
10 |
Correct |
12 ms |
1052 KB |
Output is correct |
11 |
Correct |
11 ms |
1140 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
11 ms |
1104 KB |
Output is correct |
14 |
Correct |
12 ms |
1140 KB |
Output is correct |
15 |
Correct |
7 ms |
1024 KB |
Output is correct |
16 |
Correct |
7 ms |
1064 KB |
Output is correct |
17 |
Correct |
8 ms |
976 KB |
Output is correct |
18 |
Correct |
12 ms |
1192 KB |
Output is correct |
19 |
Correct |
7 ms |
992 KB |
Output is correct |
20 |
Correct |
9 ms |
996 KB |
Output is correct |
21 |
Correct |
7 ms |
1024 KB |
Output is correct |
22 |
Correct |
8 ms |
1020 KB |
Output is correct |
23 |
Correct |
9 ms |
1092 KB |
Output is correct |
24 |
Correct |
6 ms |
976 KB |
Output is correct |
25 |
Correct |
4 ms |
592 KB |
Output is correct |
26 |
Correct |
8 ms |
980 KB |
Output is correct |
27 |
Correct |
7 ms |
1048 KB |
Output is correct |
28 |
Correct |
7 ms |
1020 KB |
Output is correct |
29 |
Correct |
6 ms |
1024 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
7 ms |
976 KB |
Output is correct |
32 |
Correct |
6 ms |
996 KB |
Output is correct |
33 |
Correct |
9 ms |
1104 KB |
Output is correct |
34 |
Correct |
6 ms |
976 KB |
Output is correct |
35 |
Correct |
9 ms |
1104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
388 KB |
Output is correct |
2 |
Correct |
9 ms |
1024 KB |
Output is correct |
3 |
Correct |
8 ms |
1008 KB |
Output is correct |
4 |
Correct |
8 ms |
976 KB |
Output is correct |
5 |
Correct |
6 ms |
1024 KB |
Output is correct |
6 |
Correct |
7 ms |
976 KB |
Output is correct |
7 |
Correct |
7 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
976 KB |
Output is correct |
9 |
Correct |
12 ms |
1192 KB |
Output is correct |
10 |
Correct |
12 ms |
1052 KB |
Output is correct |
11 |
Correct |
11 ms |
1140 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
11 ms |
1104 KB |
Output is correct |
14 |
Correct |
12 ms |
1140 KB |
Output is correct |
15 |
Correct |
7 ms |
1024 KB |
Output is correct |
16 |
Correct |
7 ms |
1064 KB |
Output is correct |
17 |
Correct |
8 ms |
976 KB |
Output is correct |
18 |
Correct |
12 ms |
1192 KB |
Output is correct |
19 |
Correct |
7 ms |
992 KB |
Output is correct |
20 |
Correct |
9 ms |
996 KB |
Output is correct |
21 |
Correct |
7 ms |
1024 KB |
Output is correct |
22 |
Correct |
8 ms |
1020 KB |
Output is correct |
23 |
Correct |
9 ms |
1092 KB |
Output is correct |
24 |
Correct |
6 ms |
976 KB |
Output is correct |
25 |
Correct |
4 ms |
592 KB |
Output is correct |
26 |
Correct |
8 ms |
980 KB |
Output is correct |
27 |
Correct |
7 ms |
1048 KB |
Output is correct |
28 |
Correct |
7 ms |
1020 KB |
Output is correct |
29 |
Correct |
6 ms |
1024 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
7 ms |
976 KB |
Output is correct |
32 |
Correct |
6 ms |
996 KB |
Output is correct |
33 |
Correct |
9 ms |
1104 KB |
Output is correct |
34 |
Correct |
6 ms |
976 KB |
Output is correct |
35 |
Correct |
9 ms |
1104 KB |
Output is correct |
36 |
Correct |
408 ms |
29480 KB |
Output is correct |
37 |
Correct |
671 ms |
42544 KB |
Output is correct |
38 |
Correct |
746 ms |
45516 KB |
Output is correct |
39 |
Correct |
600 ms |
41224 KB |
Output is correct |
40 |
Correct |
666 ms |
46516 KB |
Output is correct |
41 |
Correct |
633 ms |
43764 KB |
Output is correct |
42 |
Correct |
689 ms |
47048 KB |
Output is correct |
43 |
Correct |
395 ms |
45976 KB |
Output is correct |
44 |
Correct |
1118 ms |
55412 KB |
Output is correct |
45 |
Correct |
405 ms |
45812 KB |
Output is correct |
46 |
Correct |
1062 ms |
54260 KB |
Output is correct |
47 |
Correct |
622 ms |
47740 KB |
Output is correct |
48 |
Correct |
627 ms |
47168 KB |
Output is correct |
49 |
Correct |
639 ms |
47200 KB |
Output is correct |
50 |
Correct |
1036 ms |
55420 KB |
Output is correct |
51 |
Correct |
393 ms |
45820 KB |
Output is correct |
52 |
Correct |
679 ms |
47796 KB |
Output is correct |
53 |
Correct |
685 ms |
47212 KB |
Output is correct |
54 |
Correct |
677 ms |
47228 KB |
Output is correct |
55 |
Correct |
1022 ms |
55412 KB |
Output is correct |
56 |
Correct |
463 ms |
45924 KB |
Output is correct |
57 |
Correct |
637 ms |
41504 KB |
Output is correct |
58 |
Correct |
651 ms |
44348 KB |
Output is correct |
59 |
Correct |
607 ms |
46060 KB |
Output is correct |
60 |
Correct |
633 ms |
47344 KB |
Output is correct |
61 |
Correct |
605 ms |
46636 KB |
Output is correct |
62 |
Correct |
623 ms |
45420 KB |
Output is correct |
63 |
Correct |
593 ms |
46720 KB |
Output is correct |
64 |
Correct |
371 ms |
45968 KB |
Output is correct |
65 |
Correct |
914 ms |
55412 KB |
Output is correct |
66 |
Correct |
395 ms |
45920 KB |
Output is correct |
67 |
Correct |
843 ms |
55440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1558 ms |
47536 KB |
Output is correct |
2 |
Correct |
1660 ms |
47828 KB |
Output is correct |
3 |
Correct |
1838 ms |
47796 KB |
Output is correct |
4 |
Correct |
1730 ms |
47168 KB |
Output is correct |
5 |
Correct |
1760 ms |
47236 KB |
Output is correct |
6 |
Correct |
1590 ms |
47188 KB |
Output is correct |
7 |
Correct |
1586 ms |
47176 KB |
Output is correct |
8 |
Correct |
1455 ms |
45872 KB |
Output is correct |
9 |
Correct |
2111 ms |
55412 KB |
Output is correct |
10 |
Correct |
1523 ms |
46000 KB |
Output is correct |
11 |
Correct |
1686 ms |
55948 KB |
Output is correct |
12 |
Correct |
1473 ms |
46380 KB |
Output is correct |
13 |
Correct |
1992 ms |
55416 KB |
Output is correct |
14 |
Correct |
0 ms |
208 KB |
Output is correct |
15 |
Correct |
8 ms |
1168 KB |
Output is correct |
16 |
Correct |
11 ms |
1104 KB |
Output is correct |
17 |
Correct |
602 ms |
47836 KB |
Output is correct |
18 |
Correct |
521 ms |
47164 KB |
Output is correct |
19 |
Correct |
518 ms |
47164 KB |
Output is correct |
20 |
Correct |
802 ms |
55444 KB |
Output is correct |
21 |
Correct |
334 ms |
45828 KB |
Output is correct |
22 |
Correct |
604 ms |
47792 KB |
Output is correct |
23 |
Correct |
635 ms |
47200 KB |
Output is correct |
24 |
Correct |
566 ms |
47224 KB |
Output is correct |
25 |
Correct |
824 ms |
55524 KB |
Output is correct |
26 |
Correct |
337 ms |
45932 KB |
Output is correct |
27 |
Correct |
6 ms |
976 KB |
Output is correct |
28 |
Correct |
6 ms |
976 KB |
Output is correct |
29 |
Correct |
6 ms |
1036 KB |
Output is correct |
30 |
Correct |
7 ms |
1104 KB |
Output is correct |
31 |
Correct |
5 ms |
976 KB |
Output is correct |
32 |
Correct |
8 ms |
976 KB |
Output is correct |
33 |
Correct |
6 ms |
976 KB |
Output is correct |
34 |
Correct |
6 ms |
976 KB |
Output is correct |
35 |
Correct |
9 ms |
1104 KB |
Output is correct |
36 |
Correct |
4 ms |
1068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4070 ms |
13740 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
388 KB |
Output is correct |
2 |
Correct |
9 ms |
1024 KB |
Output is correct |
3 |
Correct |
8 ms |
1008 KB |
Output is correct |
4 |
Correct |
8 ms |
976 KB |
Output is correct |
5 |
Correct |
6 ms |
1024 KB |
Output is correct |
6 |
Correct |
7 ms |
976 KB |
Output is correct |
7 |
Correct |
7 ms |
976 KB |
Output is correct |
8 |
Correct |
4 ms |
976 KB |
Output is correct |
9 |
Correct |
12 ms |
1192 KB |
Output is correct |
10 |
Correct |
12 ms |
1052 KB |
Output is correct |
11 |
Correct |
11 ms |
1140 KB |
Output is correct |
12 |
Correct |
1 ms |
208 KB |
Output is correct |
13 |
Correct |
11 ms |
1104 KB |
Output is correct |
14 |
Correct |
12 ms |
1140 KB |
Output is correct |
15 |
Correct |
7 ms |
1024 KB |
Output is correct |
16 |
Correct |
7 ms |
1064 KB |
Output is correct |
17 |
Correct |
8 ms |
976 KB |
Output is correct |
18 |
Correct |
12 ms |
1192 KB |
Output is correct |
19 |
Correct |
7 ms |
992 KB |
Output is correct |
20 |
Correct |
9 ms |
996 KB |
Output is correct |
21 |
Correct |
7 ms |
1024 KB |
Output is correct |
22 |
Correct |
8 ms |
1020 KB |
Output is correct |
23 |
Correct |
9 ms |
1092 KB |
Output is correct |
24 |
Correct |
6 ms |
976 KB |
Output is correct |
25 |
Correct |
4 ms |
592 KB |
Output is correct |
26 |
Correct |
8 ms |
980 KB |
Output is correct |
27 |
Correct |
7 ms |
1048 KB |
Output is correct |
28 |
Correct |
7 ms |
1020 KB |
Output is correct |
29 |
Correct |
6 ms |
1024 KB |
Output is correct |
30 |
Correct |
6 ms |
1024 KB |
Output is correct |
31 |
Correct |
7 ms |
976 KB |
Output is correct |
32 |
Correct |
6 ms |
996 KB |
Output is correct |
33 |
Correct |
9 ms |
1104 KB |
Output is correct |
34 |
Correct |
6 ms |
976 KB |
Output is correct |
35 |
Correct |
9 ms |
1104 KB |
Output is correct |
36 |
Correct |
408 ms |
29480 KB |
Output is correct |
37 |
Correct |
671 ms |
42544 KB |
Output is correct |
38 |
Correct |
746 ms |
45516 KB |
Output is correct |
39 |
Correct |
600 ms |
41224 KB |
Output is correct |
40 |
Correct |
666 ms |
46516 KB |
Output is correct |
41 |
Correct |
633 ms |
43764 KB |
Output is correct |
42 |
Correct |
689 ms |
47048 KB |
Output is correct |
43 |
Correct |
395 ms |
45976 KB |
Output is correct |
44 |
Correct |
1118 ms |
55412 KB |
Output is correct |
45 |
Correct |
405 ms |
45812 KB |
Output is correct |
46 |
Correct |
1062 ms |
54260 KB |
Output is correct |
47 |
Correct |
622 ms |
47740 KB |
Output is correct |
48 |
Correct |
627 ms |
47168 KB |
Output is correct |
49 |
Correct |
639 ms |
47200 KB |
Output is correct |
50 |
Correct |
1036 ms |
55420 KB |
Output is correct |
51 |
Correct |
393 ms |
45820 KB |
Output is correct |
52 |
Correct |
679 ms |
47796 KB |
Output is correct |
53 |
Correct |
685 ms |
47212 KB |
Output is correct |
54 |
Correct |
677 ms |
47228 KB |
Output is correct |
55 |
Correct |
1022 ms |
55412 KB |
Output is correct |
56 |
Correct |
463 ms |
45924 KB |
Output is correct |
57 |
Correct |
637 ms |
41504 KB |
Output is correct |
58 |
Correct |
651 ms |
44348 KB |
Output is correct |
59 |
Correct |
607 ms |
46060 KB |
Output is correct |
60 |
Correct |
633 ms |
47344 KB |
Output is correct |
61 |
Correct |
605 ms |
46636 KB |
Output is correct |
62 |
Correct |
623 ms |
45420 KB |
Output is correct |
63 |
Correct |
593 ms |
46720 KB |
Output is correct |
64 |
Correct |
371 ms |
45968 KB |
Output is correct |
65 |
Correct |
914 ms |
55412 KB |
Output is correct |
66 |
Correct |
395 ms |
45920 KB |
Output is correct |
67 |
Correct |
843 ms |
55440 KB |
Output is correct |
68 |
Correct |
1558 ms |
47536 KB |
Output is correct |
69 |
Correct |
1660 ms |
47828 KB |
Output is correct |
70 |
Correct |
1838 ms |
47796 KB |
Output is correct |
71 |
Correct |
1730 ms |
47168 KB |
Output is correct |
72 |
Correct |
1760 ms |
47236 KB |
Output is correct |
73 |
Correct |
1590 ms |
47188 KB |
Output is correct |
74 |
Correct |
1586 ms |
47176 KB |
Output is correct |
75 |
Correct |
1455 ms |
45872 KB |
Output is correct |
76 |
Correct |
2111 ms |
55412 KB |
Output is correct |
77 |
Correct |
1523 ms |
46000 KB |
Output is correct |
78 |
Correct |
1686 ms |
55948 KB |
Output is correct |
79 |
Correct |
1473 ms |
46380 KB |
Output is correct |
80 |
Correct |
1992 ms |
55416 KB |
Output is correct |
81 |
Correct |
0 ms |
208 KB |
Output is correct |
82 |
Correct |
8 ms |
1168 KB |
Output is correct |
83 |
Correct |
11 ms |
1104 KB |
Output is correct |
84 |
Correct |
602 ms |
47836 KB |
Output is correct |
85 |
Correct |
521 ms |
47164 KB |
Output is correct |
86 |
Correct |
518 ms |
47164 KB |
Output is correct |
87 |
Correct |
802 ms |
55444 KB |
Output is correct |
88 |
Correct |
334 ms |
45828 KB |
Output is correct |
89 |
Correct |
604 ms |
47792 KB |
Output is correct |
90 |
Correct |
635 ms |
47200 KB |
Output is correct |
91 |
Correct |
566 ms |
47224 KB |
Output is correct |
92 |
Correct |
824 ms |
55524 KB |
Output is correct |
93 |
Correct |
337 ms |
45932 KB |
Output is correct |
94 |
Correct |
6 ms |
976 KB |
Output is correct |
95 |
Correct |
6 ms |
976 KB |
Output is correct |
96 |
Correct |
6 ms |
1036 KB |
Output is correct |
97 |
Correct |
7 ms |
1104 KB |
Output is correct |
98 |
Correct |
5 ms |
976 KB |
Output is correct |
99 |
Correct |
8 ms |
976 KB |
Output is correct |
100 |
Correct |
6 ms |
976 KB |
Output is correct |
101 |
Correct |
6 ms |
976 KB |
Output is correct |
102 |
Correct |
9 ms |
1104 KB |
Output is correct |
103 |
Correct |
4 ms |
1068 KB |
Output is correct |
104 |
Correct |
1464 ms |
41564 KB |
Output is correct |
105 |
Correct |
1808 ms |
45824 KB |
Output is correct |
106 |
Correct |
1618 ms |
43780 KB |
Output is correct |
107 |
Correct |
1632 ms |
45516 KB |
Output is correct |
108 |
Correct |
1196 ms |
41276 KB |
Output is correct |
109 |
Correct |
1633 ms |
45444 KB |
Output is correct |
110 |
Correct |
1607 ms |
47316 KB |
Output is correct |
111 |
Correct |
1415 ms |
46064 KB |
Output is correct |
112 |
Correct |
2005 ms |
55412 KB |
Output is correct |
113 |
Correct |
1220 ms |
45944 KB |
Output is correct |
114 |
Correct |
1953 ms |
55684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
4051 ms |
31984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |