This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
//#include "rail.h"
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,j,rs[nmax],h[nmax],a[nmax],b[nmax],q,x,l[nmax],r[nmax],dmn[4*nmax],dmx[4*nmax],lzmn[4*nmax],lzmx[4*nmax];
vector<int>ad[nmax],re[nmax],qr[nmax];
pair<int,int>st[4*nmax];
void push(int nod,int l,int r)
{
if(lzmx[nod]==-inf || lzmn[nod]==inf)return;
if(st[nod].sc!=inf)dmn[nod]=max(dmn[nod],lzmx[nod]-st[nod].sc);
if(st[nod].fr!=-inf)dmx[nod]=max(dmx[nod],st[nod].fr-lzmn[nod]);
if(l!=r)
{
lzmn[2*nod]=min(lzmn[2*nod],lzmn[nod]);
lzmn[2*nod+1]=min(lzmn[2*nod+1],lzmn[nod]);
lzmx[2*nod]=max(lzmx[2*nod],lzmx[nod]);
lzmx[2*nod+1]=max(lzmx[2*nod+1],lzmx[nod]);
}
lzmn[nod]=inf,lzmx[nod]=-inf;
}
void upd(int nod,int l,int r,int tl,int tr,int v)
{
push(nod,l,r);
if(tl<=l && r<=tr)
{
lzmn[nod]=min(lzmn[nod],v);
lzmx[nod]=max(lzmx[nod],v);
push(nod,l,r);
return;
}
int mid=(l+r)/2;
if(tl<=mid)upd(nod*2,l,mid,tl,tr,v);
if(mid<tr)upd(nod*2+1,mid+1,r,tl,tr,v);
push(nod*2,l,mid);
push(nod*2+1,mid+1,r);
dmn[nod]=max(dmn[2*nod],dmn[2*nod+1]);
dmx[nod]=max(dmx[2*nod],dmx[2*nod+1]);
}
pair<int,int> merge(pair<int,int>x,pair<int,int>y)
{
return mkp(max(x.fr,y.fr),min(x.sc,y.sc));
}
void up(int x,int l,int r,int p,int v)
{
if(l>r)return;
push(x,l,r);
if(l==r)
{
if(v==-1)st[x]=mkp(-inf,inf);
else st[x]=mkp(v,v);
return;
}
int mid=(l+r)/2;
if(p<=mid)up(2*x,l,mid,p,v);
else up(2*x+1,mid+1,r,p,v);
push(x*2,l,mid);
push(x*2+1,mid+1,r);
st[x]=merge(st[2*x],st[2*x+1]);
dmn[x]=max(dmn[2*x],dmn[2*x+1]);
dmx[x]=max(dmx[2*x],dmx[2*x+1]);
}
int qry(int nod,int l,int r,int tl,int tr)
{
push(nod,l,r);
if(tl<=l && r<=tr)return max(dmn[nod],dmx[nod]);
int mid=(l+r)/2,vl=-inf;
if(tl<=mid)vl=max(vl,qry(nod*2,l,mid,tl,tr));
if(mid<tr)vl=max(vl,qry(nod*2+1,mid+1,r,tl,tr));
return vl;
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<=n;i++)
{
cin>>h[i]>>a[i]>>b[i];
ad[min(n+1,i+a[i])].pb(i);
re[min(n+1,i+b[i]+1)].pb(i);
}
cin>>q;
for(i=1;i<=q;i++)
{
cin>>l[i]>>r[i];
qr[r[i]].pb(i);
}
for(i=1;i<=4*n;i++)
{
st[i]=mkp(-inf,inf);
lzmn[i]=inf;
lzmx[i]=-inf;
dmn[i]=dmx[i]=-inf;
}
for(i=1;i<=n;i++)
{
for(j=0;j<ad[i].size();j++)
{
up(1,1,n,ad[i][j],h[ad[i][j]]);
}
for(j=0;j<re[i].size();j++)
{
up(1,1,n,re[i][j],-1);
}
if(i-a[i]>=1)
{
upd(1,1,n,max(i-b[i],1),max(i-a[i],1),h[i]);
}
for(j=0;j<qr[i].size();j++)
{
rs[qr[i][j]]=qry(1,1,n,l[qr[i][j]],i);
}
}
for(i=1;i<=q;i++)cout<<max(-1,rs[i])<<'\n';
return 0;
}
Compilation message (stderr)
antennas.cpp:2:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("O3")
antennas.cpp:3:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization("unroll-loops")
antennas.cpp: In function 'int main()':
antennas.cpp:117:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<ad[i].size();j++)
~^~~~~~~~~~~~~
antennas.cpp:121:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<re[i].size();j++)
~^~~~~~~~~~~~~
antennas.cpp:129:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<qr[i].size();j++)
~^~~~~~~~~~~~~
# | 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... |