Submission #917348

#TimeUsernameProblemLanguageResultExecution timeMemory
917348amirhoseinfar1385Two Antennas (JOI19_antennas)C++17
100 / 100
429 ms79696 KiB
#include<bits/stdc++.h> using namespace std; const long long maxn=200000+10; long long n,q; long long h[maxn],a[maxn],b[maxn]; long long kaf=(1<<19),inf=1e15; vector<long long>del[maxn],ins[maxn]; vector<pair<long long,long long>>allq[maxn]; long long res[maxn]; struct segment{ struct node{ long long r,mxq,mnq,mx,mn; node(){ r=-1; mxq=mx=-inf; mn=mnq=inf; } }; void calr(long long i){ seg[i].r=max(seg[i].r,seg[i].mxq-seg[i].mn); seg[i].r=max(seg[i].r,seg[i].mx-seg[i].mnq); } void calc(long long i){ calr(i); if(i>=kaf){ return ; } seg[i].r=max(seg[i].r,max(seg[(i<<1)].r,seg[(i<<1)^1].r)); seg[i].mx=max(seg[(i<<1)].mx,seg[(i<<1)^1].mx); seg[i].mn=min(seg[(i<<1)].mn,seg[(i<<1)^1].mn); } void shift(long long i){ if(i>=kaf){ return ; } seg[(i<<1)].mxq=max(seg[(i<<1)].mxq,seg[i].mxq); seg[(i<<1)].mnq=min(seg[(i<<1)].mnq,seg[i].mnq); seg[(i<<1)^1].mxq=max(seg[(i<<1)^1].mxq,seg[i].mxq); seg[(i<<1)^1].mnq=min(seg[(i<<1)^1].mnq,seg[i].mnq); seg[i].mxq=-inf; seg[i].mnq=inf; } node seg[(1<<20)]; void upd(long long i,long long l,long long r,long long tl,long long tr,long long mx,long long mn){ calr(i); if(l>r||l>tr||r<tl||tl>tr){ return ; } if(l>=tl&&r<=tr){ calr(i); seg[i].mxq=-inf; seg[i].mnq=inf; seg[i].mx=mx; seg[i].mn=mn; return ; } shift(i); long long m=(l+r)>>1; upd((i<<1),l,m,tl,tr,mx,mn); upd((i<<1)^1,m+1,r,tl,tr,mx,mn); calc(i); return ; } void qu(long long i,long long l,long long r,long long tl,long long tr,long long w){ calr(i); if(l>r||l>tr||r<tl||tl>tr){ return ; } if(l>=tl&&r<=tr){ seg[i].mnq=min(seg[i].mnq,w); seg[i].mxq=max(seg[i].mxq,w); calr(i); return ; } long long m=(l+r)>>1; qu((i<<1),l,m,tl,tr,w); qu((i<<1)^1,m+1,r,tl,tr,w); calc(i); } long long pors(long long i,long long l,long long r,long long tl,long long tr){ calr(i); if(l>r||l>tr||r<tl||tl>tr){ return -1; } if(l>=tl&&r<=tr){ return seg[i].r; } shift(i); long long m=(l+r)>>1; return max(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr)); } }seg; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(long long i=1;i<=n;i++){ cin>>h[i]>>a[i]>>b[i]; if(a[i]+i<maxn){ ins[i+a[i]].push_back(i); } if(b[i]+i+1<maxn){ del[b[i]+i+1].push_back(i); } } cin>>q; for(long long i=0;i<q;i++){ long long l,r; cin>>l>>r; allq[r].push_back(make_pair(l,i)); } for(long long i=1;i<=n;i++){ for(auto x:del[i]){ seg.upd(1,0,kaf-1,x,x,-inf,inf); } for(auto x:ins[i]){ seg.upd(1,0,kaf-1,x,x,h[x],h[x]); } seg.qu(1,0,kaf-1,i-b[i],i-a[i],h[i]); for(auto x:allq[i]){ res[x.second]=seg.pors(1,0,kaf-1,x.first,i); } } for(long long i=0;i<q;i++){ cout<<res[i]<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...