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 TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll cc[4*maxN],st[4*maxN],mx[4*maxN];
void down(ll t,ll id)
{
cc[id*2]=max(cc[id*2],cc[id]);
mx[id*2]=max(mx[id*2],cc[id]+st[id*2]);
cc[id*2+1]=max(cc[id*2+1],cc[id]);
mx[id*2+1]=max(mx[id*2+1],cc[id]+st[id*2+1]);
cc[id]=-inf;
}
ll n;
void update(ll t,ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
st[id]=val;
return;
}
ll mid=l+r>>1;
down(t,id);
if(pos<=mid) update(t,pos,val,id*2,l,mid);
else update(t,pos,val,id*2+1,mid+1,r);
st[id]=max(st[id*2],st[id*2+1]);
mx[id]=max({mx[id],mx[id*2],mx[id*2+1]});
}
void upd(ll t,ll i,ll j,ll v,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i)
{
return;
}
if(i<=l&&r<=j)
{
cc[id]=max(cc[id],v);
mx[id]=max(mx[id],st[id]+v) ;
return;
}
ll mid=l+r>>1;
down(t,id);
upd(t,i,j,v,id*2,l,mid);
upd(t,i,j,v,id*2+1,mid+1,r);
mx[id]=max({mx[id],mx[id*2],mx[id*2+1]});
}
ll get(ll t,ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return -inf;
if(i<=l&&r<=j) return mx[id];
ll mid=l+r>>1;
down(t,id);
mx[id]=max({mx[id],mx[id*2],mx[id*2+1]});
return max(get(t,i,j,id*2,l,mid),get(t,i,j,id*2+1,mid+1,r));
}
ll a[maxN],b[maxN],h[maxN],q;
vector<ll>in[maxN],out[maxN];
vector<pli>quer[maxN];
ll ans[maxN];
void solve()
{
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> h[i] >> a[i] >> b[i];
ll l,r;
r=i-a[i];
l=i-b[i];
r=max(r,0ll);
l=max(l,1ll);
in[r].pb(i);
out[l-1].pb(i);
}
cin >> q;
for(int i=1;i<=q;i++)
{
ll l,r;
cin >> l >> r;
quer[l].pb({r,i});
ans[i]=-inf;
}
fill(mx,mx+4*maxN,-inf);
fill(cc,cc+4*maxN,-inf);
fill(st,st+4*maxN,-inf);
for(int i=n;i>=1;i--)
{
for(auto j:in[i])
{
update(1,j,-h[j]);
}
for(auto j:out[i])
{
update(1,j,-inf);
}
ll l,r;
l=i+a[i];
r=i+b[i];
r=min(r,n);
upd(1,l,r,h[i]);
for(auto zz:quer[i])
{
ll id=zz.se;
ll R=zz.fi;
ans[id]=max(ans[id],get(1,1,R));
}
}
fill(mx,mx+4*maxN,-inf);
fill(cc,cc+4*maxN,-inf);
fill(st,st+4*maxN,-inf);
for(int i=n;i>=1;i--)
{
for(auto j:in[i])
{
update(2,j,h[j]);
}
for(auto j:out[i])
{
update(2,j,-inf);
}
ll l,r;
l=i+a[i];
r=i+b[i];
r=min(r,n);
upd(2,l,r,-h[i]);
for(auto zz:quer[i])
{
ll id=zz.se;
ll R=zz.fi;
ans[id]=max(ans[id],get(2,1,R));
}
}
for(int i=1;i<=q;i++)
{
if(ans[i]<0) cout << -1 << '\n';
else cout << ans[i] << '\n';
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
antennas.cpp: In function 'void update(ll, ll, ll, ll, ll, ll)':
antennas.cpp:30:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
30 | ll mid=l+r>>1;
| ~^~
antennas.cpp: In function 'void upd(ll, ll, ll, ll, ll, ll, ll)':
antennas.cpp:49:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | ll mid=l+r>>1;
| ~^~
antennas.cpp: In function 'll get(ll, ll, ll, ll, ll, ll)':
antennas.cpp:59:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | ll mid=l+r>>1;
| ~^~
# | 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... |