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 sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 1e9+7;
// activate i, deactivate i
// setmax range of active things
// query max{value - h[i]}
//
// support this with lazy segtree?
int n, q, h[N], a[N], b[N], ans[N];
ar<int, 2> qs[N];
vector<pair<int, int>> ql[N];
bool on[N];
int t[N];
void toggle(int x) {
on[x] ^= 1;
}
void upd(int l, int r, int x) {
for (int i = l; i <= r; i++) if (on[i])
t[i] = max(t[i], x);
}
int qry(int l, int r) {
int cur = -1;
for (int i = l; i <= r; i++)
cur = max(cur, t[i] - h[i]);
return cur;
}
void calc() {
for (int i = 0; i < n; i++) ql[i].clear();
vector<vector<int>> ev(n);
for (int i = 0; i < n; i++) {
int L = i - b[i], R = i - a[i];
L = max(L, 0);
if (L <= R) {
ev[R].push_back(i);
if (L) ev[L-1].push_back(i);
}
}
for (int i = 0; i < q; i++) {
auto [l, r] = qs[i];
ql[l].push_back({r, i});
}
memset(t, -1, sizeof(t));
memset(on, 0, sizeof(on));
for (int i = n-1; i >= 0; i--) {
for (int x : ev[i]) toggle(x);
int L = i + a[i], R = i + b[i];
R = min(R, n-1);
if (L <= R)
upd(L, R, h[i]);
for (auto [r, id] : ql[i]) {
ans[id] = max(ans[id], qry(i, r));
}
}
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> h[i] >> a[i] >> b[i];
}
cin >> q;
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r, --l, --r;
qs[i] = {l, r};
}
memset(ans, -1, sizeof(ans));
for (int rep = 0; rep < 2; rep++) {
calc();
reverse(h, h + n);
reverse(a, a + n);
reverse(b, b + n);
for (int i = 0; i < q; i++) {
auto [l, r] = qs[i];
qs[i] = {n - 1 - r, n - 1 - l};
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# | 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... |