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>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=4e5+5;
const ll K=350;
const ld pi=3.14159265359;
const ll INF=(1LL<<40);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
ll seg[4*N],ma[4*N],val[4*N];
ll n,q,L[N],R[N],h[N],a[N],b[N],ans[N];
vector<ll> add[N],rem[N],Q[N];
void push(ll id,ll l,ll r){
val[id]=max(val[id],ma[id]+seg[id]);
if(l!=r-1){
ma[id*2]=max(ma[id*2],ma[id]);
ma[id*2+1]=max(ma[id*2+1],ma[id]);
}
ma[id]=-INF;
}
void ins(ll id,ll l,ll r,ll to,ll vl){
push(id,l,r);
if(l==r-1){
seg[id]=vl;val[id]=max(val[id],ma[id]+seg[id]);
return ;
}
ll mid=(l+r)/2;
if(to<mid){
ins(id*2,l,mid,to,vl);
}else{
ins(id*2+1,mid,r,to,vl);
}
val[id]=max({val[id],val[id*2],val[id*2+1]});
seg[id]=max(seg[id*2],seg[id*2+1]);
}
void upd(ll id,ll l,ll r,ll ql,ll qr,ll vl){
push(id,l,r);
if(l>=ql&&r<=qr){ma[id]=vl;push(id,l,r);return ;}
if(l>=qr||r<=ql)return ;
ll mid=(l+r)/2;
upd(id*2,l,mid,ql,qr,vl);
upd(id*2+1,mid,r,ql,qr,vl);
val[id]=max(val[id],max(val[id*2],val[id*2+1]));
}
ll query(ll id,ll l,ll r,ll ql,ll qr){
push(id,l,r);
if(l>=ql&&r<=qr)return val[id];
if(l>=qr||r<=ql)return -INF;
ll mid=(l+r)/2;
return max(query(id*2,l,mid,ql,qr),query(id*2+1,mid,r,ql,qr));
}
void solve(){
//reset
REP(i,4*N)ma[i]=seg[i]=val[i]=-INF;
//end
REP1(i,n){
for(auto j:add[i])ins(1,1,n+1,j,-h[j]);
for(auto j:rem[i])ins(1,1,n+1,j,-INF);
ll tmpl=max(1LL,i-b[i]),tmpr=max(1LL,i-a[i]+1);
upd(1,1,n+1,tmpl,tmpr,h[i]);
for(auto j:Q[i])ans[j]=max(ans[j],query(1,1,n+1,L[j],R[j]+1));
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
REP1(i,n){
cin>>h[i]>>a[i]>>b[i];
add[i+a[i]].pb(i);
rem[i+b[i]+1].pb(i);
}
cin>>q;
REP1(i,q){
cin>>L[i]>>R[i];
Q[R[i]].pb(i);
ans[i]=-INF;
}
solve();
REP1(i,n)h[i]=-h[i];
solve();
REP1(i,q){
cout<<(ans[i]<0?-1: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... |