This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
#define ld double
#define ll long long
#define llinf 1000000000000000000LL
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
using namespace std;
#define maxn 200005
ll n,q;
ll h[maxn],a[maxn],b[maxn];
pll v[maxn];
ll t[4*maxn],t2[4*maxn],lazy[4*maxn];
ll ans[maxn];
vector<pll> kv[maxn];
vector<ll> add[maxn],del[maxn];
ll ls[4*maxn],rs[4*maxn],tsz = 0,root = 0;
void init(ll &v,ll tl,ll tr){
v = ++tsz;
t[v] = t2[v] = lazy[v] = -llinf;
if(tl==tr) return;
ll mid = (tl+tr)/2;
init(ls[v],tl,mid);
init(rs[v],mid+1,tr);
}
void upd(ll v,ll x){
t[v] = max(t[v],t2[v]+x);
lazy[v] = max(lazy[v],x);
}
void push(ll v){
if(lazy[v]!=-llinf){
upd(ls[v],lazy[v]);
upd(rs[v],lazy[v]);
lazy[v] = -llinf;
}
}
void upd2(ll v,ll tl,ll tr,ll i,ll x){
if(tl==tr){t2[v] = x;return;}
ll mid = (tl+tr)/2;
if(i<=mid) upd2(ls[v],tl,mid,i,x);
else upd2(rs[v],mid+1,tr,i,x);
t2[v] = max(t2[ls[v]],t2[rs[v]]);
}
void upd(ll v,ll tl,ll tr,ll l,ll r,ll x){
if(tl>tr||r<tl||l>tr) return;
if(tl>=l&&tr<=r){upd(v,x);return;}
push(v);
ll mid = (tl+tr)/2;
upd(ls[v],tl,mid,l,r,x);
upd(rs[v],mid+1,tr,l,r,x);
t[v] = max(t[ls[v]],t[rs[v]]);
}
ll get(ll v,ll tl,ll tr,ll l,ll r){
if(l>r) return -llinf;
if(tl==l&&tr==r) return t[v];
push(v);
ll mid = (tl+tr)/2;
return max(get(ls[v],tl,mid,l,min(mid,r)),get(rs[v],mid+1,tr,max(mid+1,l),r));
}
void solve(){
init(root,1,n);
for(ll i = 1;i<=n;i++){
ll l = min(n+1,i+a[i]);
ll r = min(n+1,i+b[i]);
add[l].pb(i);
del[r].pb(i);
for(ll j : add[i]) upd2(root,1,n,j,h[j]);
upd(root,1,n,i-b[i],i-a[i],-h[i]);
for(pll p : kv[i]) ans[p.sc] = max(ans[p.sc],get(root,1,n,p.fi,i));
for(ll j : del[i]) upd2(root,1,n,j,-llinf);
add[i].clear();
del[i].clear();
}
for(ll i = 0;i<=tsz;i++) rs[i] = ls[i] = 0;
root = tsz = 0;
}
void tc(){
cin >> n;
for(ll i = 1;i<=n;i++) cin >> h[i] >> a[i] >> b[i];
cin >> q;
for(ll i = 1;i<=q;i++) cin >> v[i].fi >> v[i].sc;
for(ll i = 1;i<=q;i++) kv[v[i].sc].pb({v[i].fi,i}),ans[i] = -1;
solve();
for(ll i = 1;i<=n;i++) h[i] = -h[i];
solve();
for(ll i = 1;i<=q;i++) cout<<ans[i]<<endl;
}
int main(){
int t; t = 1;
while(t--){
tc();
}
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... |