#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
#include <random>
using namespace std;
#define int long long
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
struct ooga{
int h, a, b;
};
struct node{
int s, e, m, val, mx, mn, lazymn, lazymx;
node *l, *r;
void propagate(){
val=max({val, mx-lazymn, lazymx-mn});
if (s!=e){
l->lazymx=max(l->lazymx, lazymx);
l->lazymn=min(l->lazymn, lazymn);
r->lazymx=max(r->lazymx, lazymx);
r->lazymn=min(r->lazymn, lazymn);
}
lazymx=LLONG_MIN/4;
lazymn=LLONG_MAX/4;
}
node(int S, int E){
s = S, e = E, m = (s+e)/2;
val=lazymx=mx=LLONG_MIN/4;
lazymn=mn=LLONG_MAX/4;
if (s!=e){
l = new node(s, m);
r = new node(m+1, e);
}
}
void up(int i, int v1, int v2){
propagate();
if (s==e){
mn=v1;
mx=v2;
return;
}
if (i<=m)l->up(i, v1, v2);
else r->up(i, v1, v2);
l->propagate(), r->propagate();
mx=max(l->mx, r->mx);
mn=min(l->mn, r->mn);
val=max(l->val, r->val);
}
void up2(int left, int right, int v){
if (s==left && e==right){
lazymx=max(lazymx, v);
lazymn=min(lazymn, v);
}
else{
if (right<=m)l->up2(left, right, v);
else if (left>m)r->up2(left, right, v);
else l->up2(left, m, v), r->up2(m+1, right, v);
l->propagate(), r->propagate();
val=max(l->val, r->val);
}
}
int query(int left, int right){
propagate();
if (s==left && e==right)return val;
if (right<=m)return l->query(left, right);
else if (left>m)return r->query(left, right);
else return max(l->query(left, m), r->query(m+1, right));
}
}*st;
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q, a, b;
cin>>n;
vector<ooga> vect(n);
vector<vector<pii> > quer(n), ord(n);
for (int i=0; i<n; ++i){
cin>>vect[i].h>>vect[i].a>>vect[i].b;
if (i+vect[i].a<n)ord[i+vect[i].a].pb(mp(i, 1));
if (i+vect[i].b+1<n)ord[i+vect[i].b+1].pb(mp(i, 0));
}
cin>>q;
vector<int> ans(q, -1);
st = new node(0, n-1);
for (int i=0; i<q; ++i)cin>>a>>b, quer[b-1].pb(mp(a-1, i));
for (int i=0; i<n; ++i){
for (auto c:ord[i]){
if (c.se)st->up(c.fi, vect[c.fi].h, vect[c.fi].h);
else st->up(c.fi, LLONG_MAX/2, LLONG_MIN/2);
}
if (i-vect[i].a>=0)st->up2(max(0ll, i-vect[i].b), i-vect[i].a, vect[i].h);
for (auto c:quer[i])ans[c.se]=max(ans[c.se], st->query(c.fi, i));
}
for (auto a:ans)cout<<a<<"\n";
}
| # | 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... |