#include <bits/stdc++.h>
#include "towers.h"
using namespace std;
using ll = long long;
constexpr int inf = 1e9;
template<typename T, typename Op>
struct SparseTable {
vector<vector<T>> sparse;
Op accum_func;
SparseTable() = default;
SparseTable(const vector<T>& arr, const Op func) : accum_func(func) {
int n = arr.size();
int logn = 32 - __builtin_clz(n);
sparse.resize(logn, vector<T>(n));
sparse[0] = arr;
for (int lg = 1; lg < logn; lg++) {
for (int i = 0; i + (1 << lg) <= n; i++) {
sparse[lg][i] = accum_func(sparse[lg - 1][i], sparse[lg - 1][i + (1 << (lg - 1))]);
}
}
}
T find(int l, int r) { // [l, r]
r++;
int cur_log = 31 - __builtin_clz(r - l);
return accum_func(sparse[cur_log][l], sparse[cur_log][r - (1 << cur_log)]);
}
};
struct SegmentTree {
using T = vector<int>;
using S = int;
const T id = {};
inline T single(S v) { return {v}; }
T merge(const T& l, const T& r) {
T res(l.size() + r.size());
std::merge(l.cbegin(), l.cend(),
r.cbegin(), r.cend(),
res.begin());
return res;
}
int n;
vector<T> tree;
SegmentTree() = default;
void init(vector<S> arr) {
n = arr.size();
tree.resize(n * 2, id);
for (int i = 0; i < n; i++) {
tree[i + n] = single(arr[i]);
}
build();
}
void build() {
for (int i = n-1; i >= 1; i--) {
tree[i] = merge(tree[i*2], tree[i*2 + 1]);
}
}
void update(int i, S v) {
tree[i+=n] = single(v);
for (i /= 2; i >= 1; i/= 2)
tree[i] = merge(tree[i*2], tree[i*2+1]);
}
tuple<int,int,int> query(int l, int r, int x) {
int cnt = 0;
int left = -1, right = -1;
l += n; r += n;
while (l <= r) {
if (l % 2 == 1) {
if (left ==-1) left = leftmost(l, x);
cnt += count_ge(tree[l++], x);
}
if (r % 2 == 0) {
if (right == -1) right = rightmost(r, x);
cnt += count_ge(tree[r--], x);
}
l /= 2; r /= 2;
}
return {cnt, left, right};
}
int count_ge(T& v, int x) {
return v.end() - lower_bound(v.begin(), v.end(), x);
}
int leftmost(int v, int x, bool rev=0) {
if (tree[v].back() < x) return -1;
while (v < n) {
if (tree[v*2+rev].back() >= x) v = v * 2 +rev;
else v = v * 2 + 1 -rev;
}
return v - n;
}
int rightmost(int v, int x) {
return leftmost(v, x, 1);
}
};
int st_min(int a, int b) { return min(a, b); }
int st_max(int a, int b) { return max(a, b); }
int n;
vector<int> h, ix;
SparseTable<int, decltype(&st_min)> hmax;
SparseTable<int, decltype(&st_max)> hmin;
vector<int> start;
SegmentTree ans;
int find_left(int i, int d) {
int l = -1, r = i;
while (l < r-1) {
int mid = (l+r) / 2;
if (hmax.find(mid, i) - h[i] >= d)
l = mid;
else r = mid;
}
return l;
}
int find_right(int i, int d) {
int l = i, r = n;
while (l < r-1) {
int mid = (l+r) / 2;
if (hmax.find(i, mid) - h[i] >= d)
r = mid;
else l = mid;
}
return r;
}
void init(int N, std::vector<int> H) {
n = N, h = H;
ix.resize(n);
start.resize(n);
iota(ix.begin(), ix.end(), 0);
sort(ix.begin(), ix.end(), [&](int i, int j) {
return h[i] < h[j];
});
hmax = SparseTable(h, st_max);
hmin = SparseTable(h, st_min);
const int L = 0, R = n-1;
for (int i = 0; i < n;i++) {
int lD = 0, rD = inf+1;
while (lD < rD-1) {
int D = (lD + rD) / 2;
bool ok = 1;
int lg = find_left(i, D);
if (hmin.find(lg+1, i) < h[i]) ok = 0;
int rg = find_right(i, D);
if (hmin.find(i, rg-1) < h[i]) ok = 0;
if (ok) lD = D;
else rD = D;
}
start[i] = lD;
}
ans.init(start);
}
int max_towers(int L, int R, int D) {
auto [cnt, left, right] = ans.query(L, R, D);
int lg = find_left(left, D);
int rg = find_right(right, D);
if (lg > L && hmin.find(L, lg) <= h[lg]-D) cnt++;
if (rg < R && hmin.find(rg, R) <= h[rg]-D) cnt++;
return cnt;
}
Compilation message
towers.cpp: In function 'void init(int, std::vector<int>)':
towers.cpp:151:15: warning: unused variable 'L' [-Wunused-variable]
151 | const int L = 0, R = n-1;
| ^
towers.cpp:151:22: warning: unused variable 'R' [-Wunused-variable]
151 | const int L = 0, R = n-1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
838 ms |
18652 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
336 KB |
Output is correct |
2 |
Correct |
12 ms |
720 KB |
Output is correct |
3 |
Correct |
10 ms |
720 KB |
Output is correct |
4 |
Correct |
10 ms |
720 KB |
Output is correct |
5 |
Correct |
10 ms |
772 KB |
Output is correct |
6 |
Correct |
10 ms |
768 KB |
Output is correct |
7 |
Incorrect |
10 ms |
680 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
336 KB |
Output is correct |
2 |
Correct |
12 ms |
720 KB |
Output is correct |
3 |
Correct |
10 ms |
720 KB |
Output is correct |
4 |
Correct |
10 ms |
720 KB |
Output is correct |
5 |
Correct |
10 ms |
772 KB |
Output is correct |
6 |
Correct |
10 ms |
768 KB |
Output is correct |
7 |
Incorrect |
10 ms |
680 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1532 ms |
31960 KB |
29th lines differ - on the 1st token, expected: '9839', found: '9838' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
7356 KB |
Output is correct |
2 |
Correct |
1511 ms |
32228 KB |
Output is correct |
3 |
Correct |
1594 ms |
32228 KB |
Output is correct |
4 |
Correct |
1515 ms |
32224 KB |
Output is correct |
5 |
Correct |
1586 ms |
32236 KB |
Output is correct |
6 |
Correct |
1739 ms |
32228 KB |
Output is correct |
7 |
Correct |
1549 ms |
32228 KB |
Output is correct |
8 |
Correct |
1547 ms |
32228 KB |
Output is correct |
9 |
Correct |
1460 ms |
32224 KB |
Output is correct |
10 |
Correct |
1555 ms |
32228 KB |
Output is correct |
11 |
Correct |
1544 ms |
32232 KB |
Output is correct |
12 |
Correct |
830 ms |
32224 KB |
Output is correct |
13 |
Correct |
836 ms |
32232 KB |
Output is correct |
14 |
Correct |
817 ms |
32232 KB |
Output is correct |
15 |
Correct |
784 ms |
32268 KB |
Output is correct |
16 |
Correct |
793 ms |
32232 KB |
Output is correct |
17 |
Correct |
782 ms |
31216 KB |
Output is correct |
18 |
Correct |
810 ms |
32232 KB |
Output is correct |
19 |
Correct |
796 ms |
32228 KB |
Output is correct |
20 |
Correct |
821 ms |
32232 KB |
Output is correct |
21 |
Correct |
814 ms |
32224 KB |
Output is correct |
22 |
Correct |
838 ms |
32284 KB |
Output is correct |
23 |
Correct |
816 ms |
32236 KB |
Output is correct |
24 |
Correct |
767 ms |
32228 KB |
Output is correct |
25 |
Correct |
767 ms |
32224 KB |
Output is correct |
26 |
Correct |
778 ms |
32224 KB |
Output is correct |
27 |
Correct |
788 ms |
32228 KB |
Output is correct |
28 |
Correct |
10 ms |
720 KB |
Output is correct |
29 |
Correct |
10 ms |
720 KB |
Output is correct |
30 |
Correct |
10 ms |
772 KB |
Output is correct |
31 |
Correct |
10 ms |
720 KB |
Output is correct |
32 |
Correct |
10 ms |
720 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
10 ms |
764 KB |
Output is correct |
35 |
Correct |
10 ms |
720 KB |
Output is correct |
36 |
Correct |
10 ms |
720 KB |
Output is correct |
37 |
Correct |
10 ms |
768 KB |
Output is correct |
38 |
Correct |
10 ms |
768 KB |
Output is correct |
39 |
Correct |
10 ms |
772 KB |
Output is correct |
40 |
Correct |
9 ms |
720 KB |
Output is correct |
41 |
Correct |
10 ms |
720 KB |
Output is correct |
42 |
Correct |
10 ms |
720 KB |
Output is correct |
43 |
Correct |
10 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
336 KB |
Output is correct |
2 |
Correct |
12 ms |
720 KB |
Output is correct |
3 |
Correct |
10 ms |
720 KB |
Output is correct |
4 |
Correct |
10 ms |
720 KB |
Output is correct |
5 |
Correct |
10 ms |
772 KB |
Output is correct |
6 |
Correct |
10 ms |
768 KB |
Output is correct |
7 |
Incorrect |
10 ms |
680 KB |
1st lines differ - on the 1st token, expected: '34', found: '33' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
838 ms |
18652 KB |
1st lines differ - on the 1st token, expected: '1', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |