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 N = 3e5 + 7;
const int INF = 1e9 + 1010;
int n, q;
int h[N], a[N], b[N];
int L[N], R[N];
int ans[N];
int c[N];
int t[4 * N]; // max d[i]
int min_h[4 * N];
int max_c[4 * N];
vector<pair<int, int>> events[N];
vector<int> here[N];
void upd(int i, int val) {
/*if (i == 9 && val == 3) {
cout << max_c[i] << endl;
}*/
min_h[i] = min(min_h[i], val);
t[i] = max(t[i], max_c[i] - min_h[i]);
/*if (i == 9 && val == 3) {
cout << t[i] << " " << max_c[i] << " " << min_h[i] << endl;
}*/
}
void push(int i) {
upd(2 * i, min_h[i]);
upd(2 * i + 1, min_h[i]);
min_h[i] = INF;
}
void pull(int i) {
max_c[i] = max(max_c[2 * i], max_c[2 * i + 1]);
t[i] = max({t[i], max_c[i] - min_h[i], t[2 * i], t[2 * i + 1]});
}
void upd_seg(int i, int l, int r, int ql, int qr, int val) {
if (l >= qr || r <= ql)
return;
if (l >= ql && r <= qr) {
/*if (val == 3) {
cout << i << " " << l << " " << r << endl;
}*/
upd(i, val);
/*if (val == 3) {
cout << t[i] << endl;
}*/
return;
}
push(i);
int m = (l + r) / 2;
upd_seg(2 * i, l, m, ql, qr, val);
upd_seg(2 * i + 1, m, r, ql, qr, val);
pull(i);
}
void upd_c(int i, int l, int r, int pos, int new_c) {
if (r - l == 1) {
min_h[i] = INF;
c[pos] = max_c[i] = new_c;
} else {
push(i);
int m = (l + r) / 2;
if (pos < m)
upd_c(2 * i, l, m, pos, new_c);
else
upd_c(2 * i + 1, m, r, pos, new_c);
pull(i);
}
}
int get_max(int i, int l, int r, int ql, int qr) {
if (l >= qr || r <= ql)
return -1;
if (l >= ql && r <= qr) {
//cout << i << " " << l << " " << r << " " << t[i] << endl;
return t[i];
}
push(i);
int m = (l + r) / 2;
return max(get_max(2 * i, l, m, ql, qr), get_max(2 * i + 1, m, r, ql, qr));
}
void build(int i, int l, int r) {
min_h[i] = INF;
max_c[i] = -1;
t[i] = -1;
if (r - l == 1) {
c[l] = -1;
} else {
int m = (l + r) / 2;
build(2 * i, l, m);
build(2 * i + 1, m, r);
}
}
void solve() {
for (int i = 0; i <= n; i++) {
events[i].clear();
here[i].clear();
}
for (int i = 0; i < n; i++) {
int l = i + a[i], r = min(n, i + b[i] + 1);
if (l < r) {
events[l].emplace_back(i, h[i]);
events[r].emplace_back(i, -INF);
}
}
for (int i = 0; i < q; i++)
here[R[i]].push_back(i);
build(1, 0, n);
for (int i = 0; i <= n; i++) {
for (int qid : here[i])
ans[qid] = max(ans[qid], get_max(1, 0, n, L[qid], R[qid]));
for (auto [pos, new_c] : events[i]) {
//cout << "upd_c " << pos << " " << new_c << endl;
upd_c(1, 0, n, pos, new_c);
}
if (i != n) {
int l = max(0, i - b[i]), r = i - a[i] + 1;
//cout << "segadd " << i << " " << l << " " << r << " " << h[i] << endl;
/*if (h[i] == 3)
cout << l << " " << r << endl;*/
if (l < r)
upd_seg(1, 0, n, l, r, h[i]);
}
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
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++) {
cin >> L[i] >> R[i];
L[i]--;
ans[i] = -1;
}
for (int _ = 0; _ < 2; _++) {
solve();
reverse(h, h + n);
reverse(a, a + n);
reverse(b, b + n);
for (int i = 0; i < q; i++) {
R[i]--;
L[i] = n - 1 - L[i];
R[i] = n - 1 - R[i];
swap(L[i], R[i]);
R[i]++;
}
}
for (int i = 0; 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... |