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;
const int mxN = 2e5 + 5;
vector<pair<int, int>> Q[mxN];
vector<int> in[mxN], out[mxN];
pair<int, int> t[4 * mxN];
int f[4 * mxN];
pair<int, int> lazy[4 * mxN];
pair<int, int> merge(pair<int, int> a, pair<int, int> b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
void apply(int x, pair<int, int> v) {
f[x] = max({f[x], t[x].second - v.first, v.second - t[x].first});
lazy[x].first = min(lazy[x].first, v.first);
lazy[x].second = max(lazy[x].second, v.second);
}
void push(int x, int lx, int rx) {
if (lx == rx) return;
apply(x << 1, lazy[x]);
apply(x << 1 | 1, lazy[x]);
lazy[x] = {1e9, 0};
}
void update(int x, int lx, int rx, int i, pair<int, int> v) {
if (lazy[x] != pair<int, int> {1e9, 0}) push(x, lx, rx);
if (lx == rx) {
t[x] = v;
return;
}
int mx = (lx + rx) >> 1;
if (i <= mx) update(x << 1, lx, mx, i, v);
else update(x << 1 | 1, mx + 1, rx, i, v);
t[x] = merge(t[x << 1], t[x << 1 | 1]);
}
void update2(int x, int lx, int rx, int l, int r, int v) {
if (lazy[x] != pair<int, int> {1e9, 0}) push(x, lx, rx);
if (l > rx || lx > r) return;
if (l <= lx && r >= rx) {
apply(x, {v, v});
return;
}
int mx = (lx + rx) >> 1;
update2(x << 1, lx, mx, l, r, v);
update2(x << 1 | 1, mx + 1, rx, l, r, v);
f[x] = max(f[x << 1], f[x << 1 | 1]);
}
int query(int x, int lx, int rx, int l, int r) {
if (lazy[x] != pair<int, int> {1e9, 0}) push(x, lx, rx);
if (l > rx || lx > r) return -1;
if (l <= lx && r >= rx) return f[x];
int mx = (lx + rx) >> 1;
return max(query(x << 1, lx, mx, l, r), query(x << 1 | 1, mx + 1, rx, l, r));
}
void testCase() {
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;
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
Q[r].push_back({l, i});
}
for (int i = 1; i <= n; i++) {
in[min(n + 1, i + a[i])].push_back(i);
out[min(n + 1, i + b[i] + 1)].push_back(i);
}
vector<int> ans(q, -1);
for (int i = 0; i <= 4 * n; i++) lazy[i] = {1e9, 0}, f[i] = -1;
for (int i = 1; i <= n; i++) update(1, 1, n, i, {1e9, 0});
for (int i = 1; i <= n; i++) {
for (int j : in[i]) {
update(1, 1, n, j, {h[j], h[j]});
}
for (int j : out[i]) {
update(1, 1, n, j, {1e9, 0});
}
if (i - a[i] >= 1) {
int l = i - b[i], r = i - a[i];
update2(1, 1, n, l, r, h[i]);
}
for (auto [l, j] : Q[i]) {
ans[j] = query(1, 1, n, l, i);
}
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
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... |