This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int Z = 2e5, INF = 1e12;
int sL, sR, sV;
struct SegmentTree{
int l, r, x = INF, y = -INF, res = -INF;
SegmentTree *L, *R;
void pull() {
x = min(L->x, R->x);
res = max({L->res, R->res, y - x});
}
void apply(int val) {
y = max(y, val);
res = max(res, y - x);
}
void push() {
L->apply(y), R->apply(y);
y = -INF;
}
SegmentTree(int lx, int rx) : l(lx), r(rx) {
if(r - l < 2) return;
int m = (l + r) / 2;
L = new SegmentTree(l, m);
R = new SegmentTree(m, r);
}
void rangeMaximise() {
if(sL<=l && r<=sR) return apply(sV);
if(sR<=l || r<=sL) return;
push();
L->rangeMaximise(), R->rangeMaximise();
pull();
}
void pointUpdate() {
if(r - l < 2)
y = -INF, x = sV, res = max(res, y - x);
else
push(), (sL < L->r ? L : R)->pointUpdate(), pull();
}
int rangeMaxDiff() {
if(sL<=l && r<=sR) return res;
if(sR<=l || r<=sL) return -INF;
push();
return max(L->rangeMaxDiff(), R->rangeMaxDiff());
}
};
int N, Q, H[Z], A[Z], B[Z], qL[Z], ans[Z];
vector<int> add[Z+1], qAt[Z];
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
for(int i = 0; i < N; ++i) {
cin >> H[i] >> A[i] >> B[i];
if(i + A[i] < N) {
add[i + A[i]].push_back(i);
add[min(N, i+B[i]+1)].push_back(-i-1);
}
}
cin >> Q;
for(int i = 0, qR; i < Q; ++i) {
cin >> qL[i] >> qR;
--qL[i];
qAt[--qR].push_back(i);
ans[i] = -1;
}
for(int _ : {1, 0}) {
SegmentTree st(0, N);
for(int i = 0; i < N; ++i) {
for(int &j : add[i])
sL = max(j, -j-1), sV = j < 0 ? INF : H[j], st.pointUpdate();
if(i - A[i] >= 0)
sL = max(0LL, i-B[i]), sR = i-A[i]+1, sV = H[i], st.rangeMaximise();
for(int &j : qAt[i])
sL = qL[j], sR = N, ans[j] = max(ans[j], st.rangeMaxDiff());
}
if(_) for(int &i : H) i = -i;
}
for(int i = 0; i < Q; ++i)
cout << ans[i] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |