Advertisement
cnjzxy

Untitled

Mar 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF=2000000000;
  6.  
  7. int n,q;
  8. int X[100010],Y[100010];
  9. set<pair<pair<int,int>,int> >s;
  10. struct seg{
  11.     int l,r,min,max,min2,max2;
  12. }seg[2][400010];
  13.  
  14. inline void build(int pp,int p,int l,int r){
  15.     seg[pp][p].l=l;seg[pp][p].r=r;
  16.     if(l==r){
  17.         seg[pp][p].min=seg[pp][p].max=pp==0?X[l]:Y[l];
  18.         seg[pp][p].min2=INF;
  19.         seg[pp][p].max2=-INF;
  20.         return;
  21.     }
  22.  
  23.     int mid=(l+r)>>1;
  24.     build(pp,p<<1,l,mid);
  25.     build(pp,p<<1|1,mid+1,r);
  26.  
  27.     seg[pp][p].min=min(seg[pp][p<<1].min,seg[pp][p<<1|1].min);
  28.     seg[pp][p].max=max(seg[pp][p<<1].max,seg[pp][p<<1|1].max);
  29.     seg[pp][p].min2=seg[pp][p<<1].min<seg[pp][p<<1|1].min?min(seg[pp][p<<1].min2,seg[pp][p<<1|1].min):min(seg[pp][p<<1].min,seg[pp][p<<1|1].min2);
  30.     seg[pp][p].max2=seg[pp][p<<1].max>seg[pp][p<<1|1].max?max(seg[pp][p<<1].max2,seg[pp][p<<1|1].max):max(seg[pp][p<<1].max,seg[pp][p<<1|1].max2);
  31. }
  32.  
  33. inline pair<int,int> queryMin(int pp,int p,int l, int r){
  34.     if(l<=seg[pp][p].l&&seg[pp][p].r<=r)
  35.         return make_pair(seg[pp][p].min,seg[pp][p].min2);
  36.  
  37.     int mid=(seg[pp][p].l+seg[pp][p].r)>>1;
  38.     if(r<=mid)
  39.         return queryMin(pp,p<<1,l,r);
  40.     else
  41.         if(l>mid)
  42.             return queryMin(pp,p<<1|1,l,r);
  43.         else{
  44.             int temp[4];
  45.             pair<int,int>temp1=queryMin(pp,p<<1,l,mid);
  46.             temp[0]=temp1.first;
  47.             temp[1]=temp1.second;
  48.             temp1=queryMin(pp,p<<1|1,mid+1,r);
  49.             temp[2]=temp1.first;
  50.             temp[3]=temp1.second;
  51.             sort(temp,temp+4);
  52.             return make_pair(temp[0],temp[1]);
  53.         }
  54. }
  55. inline pair<int,int> queryMax(int pp,int p,int l, int r){
  56.     if(l<=seg[pp][p].l&&seg[pp][p].r<=r)
  57.         return make_pair(seg[pp][p].max,seg[pp][p].max2);
  58.  
  59.     int mid=(seg[pp][p].l+seg[pp][p].r)>>1;
  60.     if(r<=mid)
  61.         return queryMax(pp,p<<1,l,r);
  62.     else
  63.         if(l>mid)
  64.             return queryMax(pp,p<<1|1,l,r);
  65.         else{
  66.             int temp[4];
  67.             pair<int,int>temp1=queryMax(pp,p<<1,l,mid);
  68.             temp[0]=temp1.first;
  69.             temp[1]=temp1.second;
  70.             temp1=queryMax(pp,p<<1|1,mid+1,r);
  71.             temp[2]=temp1.first;
  72.             temp[3]=temp1.second;
  73.             sort(temp,temp+4);
  74.             return make_pair(temp[3],temp[2]);
  75.         }
  76. }
  77.  
  78. int main(){
  79.     scanf("%d%d",&n,&q);
  80.  
  81.     for(int i=1;i<=n;i++){
  82.         scanf("%d%d",X+i,Y+i);
  83.         s.insert(make_pair(make_pair(X[i],Y[i]),i));
  84.     }
  85.  
  86.     build(0,1,1,n);
  87.     build(1,1,1,n);
  88.  
  89.     for(;q;q--){
  90.         int ll,rr;
  91.         scanf("%d%d",&ll,&rr);
  92.  
  93.         pair<int,int>U=queryMax(1,1,ll,rr);
  94.         pair<int,int>D=queryMin(1,1,ll,rr);
  95.         pair<int,int>L=queryMin(0,1,ll,rr);
  96.         pair<int,int>R=queryMax(0,1,ll,rr);
  97.  
  98.         int temp=max(U.first-D.first,R.first-L.first);
  99.  
  100.         auto iter=s.lower_bound(make_pair(make_pair(L.first,D.first),ll));
  101.         if(iter!=s.end()&&iter->first==make_pair(L.first,D.first)&&iter->second<=rr){
  102.             temp=min(temp,max(U.first-D.second,R.first-L.second));
  103.         }
  104.  
  105.         iter=s.lower_bound(make_pair(make_pair(R.first,D.first),ll));
  106.         if(iter!=s.end()&&iter->first==make_pair(R.first,D.first)&&iter->second<=rr){
  107.             temp=min(temp,max(U.first-D.second,R.second-L.first));
  108.         }
  109.  
  110.         iter=s.lower_bound(make_pair(make_pair(L.first,U.first),ll));
  111.         if(iter!=s.end()&&iter->first==make_pair(L.first,U.first)&&iter->second<=rr){
  112.             temp=min(temp,max(U.second-D.first,R.first-L.second));
  113.         }
  114.  
  115.         iter=s.lower_bound(make_pair(make_pair(R.first,U.first),ll));
  116.         if(iter!=s.end()&&iter->first==make_pair(R.first,U.first)&&iter->second<=rr){
  117.             temp=min(temp,max(U.second-D.first,R.second-L.first));
  118.         }
  119.  
  120.         printf("%d\n",temp);
  121.     }
  122.  
  123.     return 0;
  124. }
  125. /*
  126. 4 1
  127. 3 3
  128. 5 7
  129. 8 3
  130. 10 3
  131. 1 4
  132. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement