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>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vi>
#define vvvi vector <vvi>
#define vp vector <pii>
#define vvp vector <vp>
#define vb vector <bool>
#define vvb vector <vb>;
#define INF INT_MAX
#define MOD 1000000007
#define MAXN 200000
using namespace std;
int n,q,h[MAXN+1],a[MAXN+1],b[MAXN+1],L[MAXN+1],R[MAXN+1];
int t[4*MAXN+1],lazy[4*MAXN+1],ans[4*MAXN+1];
void build(int v,int tl,int tr){
if (tl==tr)lazy[v]=ans[v]=t[v]=-INF;else{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=lazy[v]=ans[v]=-INF;
}
}
void push(int v){
ans[2*v]=max(ans[2*v],t[2*v]+lazy[v]);
ans[2*v+1]=max(ans[2*v+1],t[2*v+1]+lazy[v]);
lazy[2*v]=max(lazy[2*v],lazy[v]);
lazy[2*v+1]=max(lazy[2*v+1],lazy[v]);
lazy[v]=-INF;
}
void uprange(int v,int tl,int tr,int l,int r,int val){
if (l>r)return;
if (tl==l && tr==r){
ans[v]=max(ans[v],t[v]+val);
lazy[v]=max(lazy[v],val);
}else{
push(v);
int tm=(tl+tr)/2;
uprange(v*2,tl,tm,l,min(r,tm),val);
uprange(v*2+1,tm+1,tr,max(l,tm+1),r,val);
ans[v]=max(ans[v*2],ans[v*2+1]);
}
}
void uppoint(int v,int tl,int tr,int pos,int val){
if (tl==tr){
t[v]=val;
// ans[v]=-INF;
}else{
push(v);
int tm=(tl+tr)/2;
if (pos<=tm)uppoint(v*2,tl,tm,pos,val);else
uppoint(v*2+1,tm+1,tr,pos,val);
t[v]=max(t[v*2],t[v*2+1]);
ans[v]=max(ans[v*2],ans[v*2+1]);
}
}
int get(int v,int tl,int tr,int l,int r){
if (l>r)return -INF;
if (tl==l && tr==r)return ans[v];
push(v);
int tm=(tl+tr)/2;
return max(get(v*2,tl,tm,l,min(r,tm)),
get(v*2+1,tm+1,tr,max(l,tm+1),r));
}
vi res(MAXN+1,-INF);
void solve(){
vvp Q(n+1);
for (int i=1;i<=q;i++)Q[R[i]].pb({L[i],i});
build(1,1,n);
vvi end(n+1);
vvi start(n+1);
for (int i=1;i<=n;i++){
if (i+a[i]<=n)start[i+a[i]].pb(i);
if (i+b[i]<=n)end[i+b[i]].pb(i);
}
for (int i=1;i<=n;i++){
for (int x:start[i])uppoint(1,1,n,x,h[x]);
uprange(1,1,n,max(1ll,i-b[i]),i-a[i],-h[i]);
for (auto &[r,id]:Q[i])res[id]=max(res[id],get(1,1,n,r,i));
for (int x:end[i])uppoint(1,1,n,x,-INF);
}
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n;
for (int i=1;i<=n;i++)cin>>h[i]>>a[i]>>b[i];
cin>>q;
for (int i=1;i<=n;i++)cin>>L[i]>>R[i];
solve();
for (int i=1;i<=n/2;i++){
swap(h[i],h[n-i+1]);
swap(a[i],a[n-i+1]);
swap(b[i],b[n-i+1]);
}
for (int i=1;i<=q;i++){
int templ=L[i],tempr=R[i];
L[i]=n-tempr+1;
R[i]=n-templ+1;
}
solve();
for (int i=1;i<=q;i++)cout<<((res[i]>=0) ? res[i] : -1)<<'\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... |