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;
typedef long long llint;
typedef pair <llint, llint> pi;
const int MAXN = 200005;
const int MAXM = 2005;
const llint INF = 1000000000000000000LL;
int n, q, ofs = 1;
int h[MAXN], a[MAXN], b[MAXN];
int cost[MAXM][MAXM], dp[MAXM][MAXM], res[MAXM][MAXM];
vector <int> v[MAXN];
llint mx[MAXN * 4], mn[MAXN * 4];
void precompute () {
memset(cost, -1, sizeof cost);
memset(dp, -1, sizeof dp);
memset(res, -1, sizeof res);
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (a[i] <= j - i && j - i <= b[i] && a[j] <= j - i && j - i <= b[j]) {
cost[i][j] = abs(h[i] - h[j]);
}
dp[i][j] = max(dp[i][j - 1], cost[i][j]);
}
}
for (int rig = 1; rig <= n; rig++) {
for (int lef = rig - 1; lef >= 1; lef--) {
res[lef][rig] = max(res[lef + 1][rig], dp[lef][rig]);
}
}
}
void update (int pos, int val, bool flg) {
//cout << "update " << pos << " " << val << " " << flg << endl;
pos += ofs;
if (flg == 1) {
mn[pos] = mx[pos] = val;
} else {
mn[pos] = INF; mx[pos] = -INF;
}
pos /= 2;
while (pos > 0) {
mn[pos] = min(mn[pos * 2], mn[pos * 2 + 1]);
mx[pos] = max(mx[pos * 2], mx[pos * 2 + 1]);
pos /= 2;
}
}
pi upit (int x, int from, int to, int lo, int hi) {
if (from <= lo && hi <= to) return {mn[x], mx[x]};
if (to < lo || hi < from) return {INF, -INF};
pi lef = upit(2 * x, from, to, lo, (lo + hi) / 2);
pi rig = upit(2 * x + 1, from, to, (lo + hi) / 2 + 1, hi);
return {min(lef.first, rig.first), max(lef.second, rig.second)};
}
void sweep () {
for (int i = 1; i <= n; i++) {
int lo = i + a[i], hi = i + b[i];
if (lo <= n) v[lo].push_back(i);
if (hi + 1 <= n) v[hi + 1].push_back(-i);
}
llint sol = 0;
for (int i = 1; i <= n; i++) {
for (auto x : v[i]) {
if (x > 0) {
update(x, h[x], 1);
} else {
update(-x, 0, 0);
}
}
int lo = i - b[i], hi = i - a[i];
if (hi < 1) continue;
lo = max(lo, 1);
//cout << "bla " << lo << " " << hi << endl;
pi res = upit(1, lo, hi, 0, ofs - 1);
if (res.first <= h[i]) sol = max(sol, h[i] - res.first);
if (h[i] <= res.second) sol = max(sol, res.second - h[i]);
}
cout << sol << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i] >> a[i] >> b[i];
}
cin >> q;
if (n <= 2000) {
precompute();
for (int i = 0; i < q; i++) {
int lef, rig;
cin >> lef >> rig;
cout << res[lef][rig] << '\n';
}
} else if (q <= 1) {
cin >> q >> q;
while (ofs < n + 1) ofs *= 2;
for (int i = 0; i < 2 * ofs; i++) {
mn[i] = INF;
mx[i] = -INF;
}
sweep();
}
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... |