#include "towers.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int root = 0;
int par[N];
int L[N], R[N], mn[N];
struct ST {
vector<pair<int,int>> t;
int n;
ST(){}
ST(vector<int>&h) {
n = h.size();
t.resize(2*n);
for(int i = 0;i<n;i++)t[i+n] = {h[i], i};
for(int i = n-1;i>0;i--)t[i] = max(t[2*i], t[2*i+1]);
}
int query(int l, int r) {
l+=n,r+=n+1;
pair<int,int> ans = {-1, -1};
for(;l<r;l>>=1,r>>=1) {
if(l&1)ans = max(ans, t[l++]);
if(r&1)ans = max(ans, t[--r]);
}
return ans.second;
}
};
vector<int> H;
ST st;
int build(int l, int r) {
if(r < l)return -1;
int p = st.query(l, r);
L[p] = build(l, p-1);
R[p] = build(p+1, r);
mn[p] = H[p];
if(L[p]!=-1)mn[p] = min(mn[p], L[p]);
if(R[p]!=-1)mn[p] = min(mn[p], R[p]);
return p;
}
void init(int n, std::vector<int> h) {
st = ST(h);
H = h;
root = build(0, n-1);
}
int query(int v, int l, int r, int MX, int D) {
if(l > r)return 0;
if(v == -1)return 0;
if(v > r) {
return query(L[v], l, r, MX, D);
}
if(v < l)
return query(R[v], l, r, MX, D);
int ans = 0;
if(mn[v] <= MX)ans=1;
ans = max(ans, query(L[v], l, r, H[v]-D, D)+query(R[v], l, r, H[v]-D, D));
return ans;
}
int max_towers(int l, int r, int D) {
return query(root, l, r, 2e9, D);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4040 ms |
6252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '292', found: '299' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '292', found: '299' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4034 ms |
4184 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4051 ms |
1368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
1st lines differ - on the 1st token, expected: '292', found: '299' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4040 ms |
6252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |