#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int MAXN = 20 * MAX;
int n, root[MAX], tsz, st[MAXN], ls[MAXN], rs[MAXN];
void Modify(int& v, int p, int l, int r, int i, int x) {
v = ++tsz;
ls[v] = ls[p];
rs[v] = rs[p];
st[v] = st[p] + x;
if (l == r) {
return;
}
int mid = l + r >> 1;
if (i <= mid) {
Modify(ls[v], ls[p], l, mid, i, x);
} else {
Modify(rs[v], rs[p], mid + 1, r, i, x);
}
}
int Query(int v, int l, int r, int ll, int rr) {
if (l > r || l > rr || r < ll) {
return 0;
}
if (ll <= l && r <= rr) {
return st[v];
}
int mid = l + r >> 1;
return Query(ls[v], l, mid, ll, rr) + Query(rs[v], mid + 1, r, ll, rr);
}
vector<pair<int, int>> qp;
void init(int n, std::vector<int> h) {
::n = n;
vector<int> L(n);
vector<int> R(n);
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && h[i] < h[stk.back()]) {
stk.pop_back();
}
L[i] = (stk.empty() ? -1 : stk.back());
stk.push_back(i);
}
stk.clear();
for (int i = n - 1; i >= 0; i--) {
while (!stk.empty() && h[i] < h[stk.back()]) {
stk.pop_back();
}
R[i] = (stk.empty() ? -1 : stk.back());
stk.push_back(i);
}
for (int i = 0; i < n; i++) {
if (L[i] == -1 || R[i] == -1) {
continue;
}
int mx = max(h[L[i]], h[R[i]]);
qp.push_back({h[i] - mx, i});
}
sort(qp.begin(), qp.end());
for (int i = (int) qp.size() - 1; i >= 0; i--) {
Modify(root[i], root[i + 1], 0, n - 1, qp[i].second, +1);
}
}
int max_towers(int L, int R, int D) {
if (qp.empty())
return 1;
int pos = lower_bound(qp.begin(), qp.end(), make_pair(D, -1)) - qp.begin();
while (pos >= 0 && (pos >= (int) qp.size() || qp[pos].second > D)) pos--;
if (pos == -1)
return 1;
return Query(root[pos], 0, n - 1, L, R) + 1;
}
Compilation message
towers.cpp: In function 'void Modify(int&, int, int, int, int, int)':
towers.cpp:20:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
20 | int mid = l + r >> 1;
| ~~^~~
towers.cpp: In function 'int Query(int, int, int, int, int)':
towers.cpp:35:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
416 ms |
14112 KB |
5th lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '28' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '28' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
496 ms |
23692 KB |
1st lines differ - on the 1st token, expected: '11903', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
285 ms |
5452 KB |
1st lines differ - on the 1st token, expected: '7197', found: '12120' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
1st lines differ - on the 1st token, expected: '13', found: '28' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
416 ms |
14112 KB |
5th lines differ - on the 1st token, expected: '1', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |