#ifndef Nhoksocqt1
#include "towers.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 100005;
vector<ii> B[MAXN];
int dp[MAXN], nxt[MAXN], prv[MAXN];
int h[MAXN], pos[MAXN], nArr, ksub1;
bool check_sub1;
void init(int _N, vector<int> _H) {
nArr = _N;
ksub1 = 1;
for (int i = 1; i <= nArr; ++i) {
h[i] = _H[i - 1];
if(h[ksub1] < h[i])
ksub1 = i;
}
check_sub1 = 1;
for (int i = 3; i <= nArr; ++i)
check_sub1 &= (h[i - 2] < h[i - 1] || h[i - 1] > h[i]);
}
struct SegNode {
int maxv, lz;
} seg[4 * MAXN];
void build(int id, int l, int r) {
seg[id] = {1, 0};
if(l == r)
return;
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
}
inline void pushDown(int id) {
int &lz(seg[id].lz);
for (int t = (id << 1); t <= (id << 1 | 1); ++t)
seg[t] = {max(seg[t].maxv, lz), max(seg[t].lz, lz)};
lz = 0;
}
void update(int id, int l, int r, int u, int v, int k) {
if(u <= l && r <= v) {
seg[id].maxv = max(seg[id].maxv, k);
seg[id].lz = max(seg[id].lz, k);
return;
}
if(seg[id].lz)
pushDown(id);
int mid = (l + r) >> 1;
if(mid >= u)
update(id << 1, l, mid, u, v, k);
if(mid + 1 <= v)
update(id << 1 | 1, mid + 1, r, u, v, k);
seg[id].maxv = max(seg[id << 1].maxv, seg[id << 1 | 1].maxv);
}
int query(int id, int l, int r, int u, int v) {
if(u <= l && r <= v)
return seg[id].maxv;
if(seg[id].lz)
pushDown(id);
int res(0), mid = (l + r) >> 1;
if(mid >= u)
res = query(id << 1, l, mid, u, v);
if(mid + 1 <= v)
res = max(res, query(id << 1 | 1, mid + 1, r, u, v));
return res;
}
int sub3(int l, int r, int d) {
for (int i = l; i <= r; ++i)
B[i].clear();
stack<int> st;
for (int i = l; i <= r; ++i) {
while(sz(st) && h[st.top()] - d < h[i])
st.pop();
prv[i] = (sz(st)) ? st.top() : l - 1;
st.push(i);
}
while(sz(st))
st.pop();
for (int i = r; i >= l; --i) {
while(sz(st) && h[st.top()] - d < h[i])
st.pop();
nxt[i] = (sz(st)) ? st.top() : r + 1;
st.push(i);
}
vector<ii> sorted_h;
for (int i = l; i <= r; ++i)
sorted_h.push_back(ii(h[i], i));
sort(sorted_h.begin(), sorted_h.end());
int res(0), nTree(sz(sorted_h));
build(1, 1, nTree);
for (int i = l; i <= r; ++i) {
for (int it = 0; it < sz(B[i]); ++it) {
int j(B[i][it].fi), k(B[i][it].se);
update(1, 1, nTree, 1, j, k);
}
int pos = upper_bound(sorted_h.begin(), sorted_h.end(), ii(h[i], 1e9)) - sorted_h.begin();
int dpi = query(1, 1, nTree, pos, pos);
//cout << i << ' ' << pos << ' ' << dpi << '\n';
if(nxt[i] < r) {
int pos = upper_bound(sorted_h.begin(), sorted_h.end(), ii(h[nxt[i]] - d, 1e9)) - sorted_h.begin();
//cout << '.' << pos << '\n';
B[nxt[i] + 1].push_back(ii(pos, 1 + dpi));
}
res = max(res, dpi);
}
return res;
}
int max_towers(int l, int r, int d) {
++l, ++r;
if(check_sub1) {
int res = 1 + (l < ksub1 && ksub1 < r && max(h[l], h[r]) <= h[ksub1] - d);
return res;
}
return sub3(l, r, d);
for (int i = l; i <= r; ++i)
dp[i] = 1;
for (int i = l; i <= r; ++i) {
int k(i + 1);
for (int j = i + 2; j <= r; ++j) {
if(h[k] - d >= max(h[i], h[j]))
dp[j] = max(dp[j], dp[i] + 1);
if(h[j] > h[k])
k = j;
}
}
int res(0);
for (int i = l; i <= r; ++i)
res = max(res, dp[i]);
//cout << sub3(l, r, d) << ' ';
return res;
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "towers"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
vector<int> _H;
int _N, _Q;
cin >> _N;
_H.resize(_N);
for (int i = 0; i < _N; ++i)
cin >> _H[i];
init(_N, _H);
cin >> _Q;
for (int t = 0; t < _Q; ++t) {
int _L, _R, _D;
cin >> _L >> _R >> _D;
cout << "ANSWER FOR " << _L + 1 << ' ' << _R + 1 << ' ' << _D << ": " << max_towers(_L, _R, _D) << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
5896 KB |
Output is correct |
2 |
Correct |
528 ms |
6308 KB |
Output is correct |
3 |
Correct |
532 ms |
6300 KB |
Output is correct |
4 |
Correct |
531 ms |
6312 KB |
Output is correct |
5 |
Correct |
552 ms |
6232 KB |
Output is correct |
6 |
Correct |
523 ms |
6308 KB |
Output is correct |
7 |
Correct |
554 ms |
6308 KB |
Output is correct |
8 |
Correct |
1 ms |
5464 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
1 ms |
5464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5464 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5720 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5464 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5720 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4016 ms |
12192 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4029 ms |
9336 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5464 KB |
Output is correct |
2 |
Incorrect |
2 ms |
5720 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
5896 KB |
Output is correct |
2 |
Correct |
528 ms |
6308 KB |
Output is correct |
3 |
Correct |
532 ms |
6300 KB |
Output is correct |
4 |
Correct |
531 ms |
6312 KB |
Output is correct |
5 |
Correct |
552 ms |
6232 KB |
Output is correct |
6 |
Correct |
523 ms |
6308 KB |
Output is correct |
7 |
Correct |
554 ms |
6308 KB |
Output is correct |
8 |
Correct |
1 ms |
5464 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
1 ms |
5464 KB |
Output is correct |
11 |
Correct |
1 ms |
5464 KB |
Output is correct |
12 |
Incorrect |
2 ms |
5720 KB |
1st lines differ - on the 1st token, expected: '292', found: '286' |
13 |
Halted |
0 ms |
0 KB |
- |