이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
struct T {
int mn_on, ans;
int lzy;
T() {
mn_on = MOD;
ans = lzy = -1;
}
T(int x, bool on) {
mn_on = (on ? x : MOD);
ans = lzy = -1;
}
friend T operator + (const T& one, const T& two) {
T res; res.lzy = -1;
res.ans = max(one.ans, two.ans);
res.mn_on = min(one.mn_on, two.mn_on);
return res;
}
} t[4 * N];
bool on[N];
void build(int v, int tl, int tr) {
if (tl == tr) t[v] = T(), on[tl] = 0;
else {
int tm = (tl + tr) / 2;
build(2*v, tl, tm), build(2*v+1, tm+1, tr);
t[v] = t[2*v] + t[2*v+1];
}
}
void push(int v, int tl, int tr, int x) {
if (x == -1) return;
t[v].ans = max(t[v].ans, x - t[v].mn_on);
if (tl != tr) {
t[2*v].lzy = max(t[2*v].lzy, x);
t[2*v+1].lzy = max(t[2*v+1].lzy, x);
}
}
void app(int v, int tl, int tr) {
push(v, tl, tr, t[v].lzy), t[v].lzy = -1;
}
void toggle(int v, int tl, int tr, int pos) {
app(v, tl, tr);
if (pos < tl || pos > tr) return;
if (tl == tr) {
int old_ans = t[v].ans;
on[pos] ^= 1;
t[v] = T(h[pos], on[pos]);
t[v].ans = old_ans;
return;
}
int tm = (tl + tr) / 2;
toggle(2*v, tl, tm, pos), toggle(2*v+1, tm+1, tr, pos);
t[v] = t[2*v] + t[2*v+1];
}
void upd(int v, int tl, int tr, int l, int r, int x) {
app(v, tl, tr);
if (r < tl || l > tr) return;
if (l <= tl && tr <= r) {
push(v, tl, tr, x);
return;
}
int tm = (tl + tr) / 2;
upd(2*v, tl, tm, l, r, x), upd(2*v+1, tm+1, tr, l, r, x);
t[v] = t[2*v] + t[2*v+1];
}
int qry(int v, int tl, int tr, int l, int r) {
app(v, tl, tr);
if (r < tl || l > tr) return -1;
if (l <= tl && tr <= r) return t[v].ans;
int tm = (tl + tr) / 2;
return max(qry(2*v, tl, tm, l, r), qry(2*v+1, tm+1, tr, l, r));
}
/*
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});
}
build(1, 0, n-1);
/*
memset(t, -1, sizeof(t));
memset(on, 0, sizeof(on));
*/
for (int i = n-1; i >= 0; i--) {
for (int x : ev[i]) {
toggle(1, 0, n-1, x);
}
int L = i + a[i], R = i + b[i];
R = min(R, n-1);
if (L <= R)
upd(1, 0, n-1, L, R, h[i]);
for (auto [r, id] : ql[i]) {
ans[id] = max(ans[id], qry(1, 0, n-1, 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... |