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;
#pragma GCC optimize("Ofast")
const int INF = 1e9 + 7;
struct SGT {
int n; vector<int> min_height, tag, max_dif;
SGT(int _n): n(_n), min_height(n * 4 + 4, INF),
tag(n * 4 + 4, -INF), max_dif(n * 4 + 4, -1) {}
void upd(int i, int v) {
tag[i] = max(tag[i], v);
max_dif[i] = max(max_dif[i], tag[i] - min_height[i]);
}
void push(int i) {
if (tag[i] == -INF) return;
upd(i * 2, tag[i]);
upd(i * 2 + 1, tag[i]);
tag[i] = -INF;
}
void pull(int i) {
min_height[i] = min(min_height[i * 2], min_height[i * 2 + 1]);
max_dif[i] = max(max_dif[i * 2], max_dif[i * 2 + 1]);
}
void set_height(int i, int l, int r, int p, int v) {
if (l == r) {
min_height[i] = v;
tag[i] = -INF;
return;
}
push(i);
int m = (l + r) / 2;
if (p <= m) set_height(i * 2, l, m, p, v);
else set_height(i * 2 + 1, m + 1, r, p, v);
pull(i);
return;
}
void chmax(int i, int l, int r, int ql, int qr, int v) {
if (ql <= l and r <= qr) {
upd(i, v);
return;
}
push(i);
int m = (l + r) / 2;
if (ql <= m) chmax(i * 2, l, m, ql, qr, v);
if (m < qr) chmax(i * 2 + 1, m + 1, r, ql, qr, v);
pull(i);
return;
}
int query(int i, int l, int r, int ql, int qr) {
if (ql <= l and r <= qr)
return max_dif[i];
push(i);
int m = (l + r) / 2, ans = -1;
if (ql <= m) ans = max(ans, query(i * 2, l, m, ql, qr));
if (m < qr) ans = max(ans, query(i * 2 + 1, m + 1, r, ql, qr));
return ans;
}
void set_height(int p, int v) { set_height(1, 1, n, p, v); }
void chmax(int ql, int qr, int v) { chmax(1, 1, n, ql, qr, v); }
int query(int ql, int qr) { return query(1, 1, n, ql, qr); }
};
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
vector<int> H(N + 1), A(N + 1), B(N + 1);
for (int i = 1; i <= N; ++i)
cin >> H[i] >> A[i] >> B[i];
int Q; cin >> Q;
vector<int> L(Q + 1), R(Q + 1);
for (int i = 1; i <= Q; ++i)
cin >> L[i] >> R[i];
vector<int> ans(Q + 1, -1);
auto solve = [&]() -> void {
vector<vector<int>> in(N + 1);
vector<vector<int>> ou(N + 2);
for (int i = 1; i <= N; ++i) {
int lb = min(N + 1, i + A[i]);
int rb = min(N, i + B[i]);
if (lb < N + 1) {
in[lb].push_back(i);
ou[rb + 1].push_back(i);
}
}
vector<vector<pair<int, int>>> qs(N + 1);
for (int i = 1; i <= Q; ++i)
qs[R[i]].emplace_back(L[i], i);
SGT sgt(N);
for (int i = 1; i <= N; ++i) {
for (int &j: in[i])
sgt.set_height(j, H[j]);
for (int &j: ou[i])
sgt.set_height(j, INF);
int lb = max(1, i - B[i]);
int rb = max(0, i - A[i]);
if (rb > 0)
sgt.chmax(lb, rb, H[i]);
for (auto &[ql, id]: qs[i])
ans[id] = max(ans[id], sgt.query(ql, i));
}
};
solve();
reverse(begin(H) + 1, end(H));
reverse(begin(A) + 1, end(A));
reverse(begin(B) + 1, end(B));
for (int i = 1; i <= Q; ++i) {
L[i] = N - L[i] + 1;
R[i] = N - R[i] + 1;
swap(L[i], R[i]);
}
solve();
for (int i = 1; i <= Q; ++i)
cout << ans[i] << '\n';
return 0;
}
# | 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... |