【HDOJ6695】Welcome Party(multiset)

题意:

 

 n<=1e5,x[i],y[i]<=1e18

思路:

 

 

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 typedef unsigned int uint;
  5 typedef unsigned long long ull;
  6 typedef pair<int,int> PII;
  7 typedef pair<ll,ll> Pll;
  8 typedef vector<int> VI;
  9 typedef vector<PII> VII;
 10 #define N  210000
 11 #define M  4100000
 12 #define fi first
 13 #define se second
 14 #define MP make_pair
 15 #define pi acos(-1)
 16 #define mem(a,b) memset(a,b,sizeof(a))
 17 #define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
 18 #define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
 19 #define lowbit(x) x&(-x)
 20 #define Rand (rand()*(1<<16)+rand())
 21 #define id(x) ((x)<=B?(x):m-n/(x)+1)
 22 #define ls p<<1
 23 #define rs p<<1|1
 24 
 25 const ll MOD=1e9+7,inv2=(MOD+1)/2;
 26       double eps=1e-6;
 27       ll INF=1e18;
 28       int dx[4]={-1,1,0,0};
 29       int dy[4]={0,0,-1,1};
 30 
 31 struct arr
 32 {
 33     ll x,y;
 34 }a[N];
 35 
 36 multiset<ll> st;
 37 
 38 bool cmp(arr a,arr b)
 39 {
 40     return a.x<b.x;
 41 }
 42 
 43 ll read()
 44 {
 45    ll v=0,f=1;
 46    char c=getchar();
 47    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
 48    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
 49    return v*f;
 50 }
 51 
 52 int main()
 53 {
 54     //freopen("1.in","r",stdin);
 55     //freopen("1.out","w",stdout);
 56 
 57     int cas;
 58     scanf("%d",&cas);
 59 
 60     while(cas--)
 61     {
 62        int n;
 63        scanf("%d",&n);
 64        rep(i,1,n) a[i].x=read(),a[i].y=read();
 65        sort(a+1,a+n+1,cmp);
 66        st.clear();
 67        rep(i,1,n) st.insert(a[i].y);
 68        int t=n+1;
 69        ll mx=-1,ans=1e18;
 70        per(i,n,1)
 71        {
 72                while(t>1&&a[t-1].x>a[i].x)
 73                {
 74                    t--;
 75                    st.erase(st.find(a[t].y));
 76                    mx=max(mx,a[t].y);
 77                }
 78                st.erase(st.find(a[i].y));
 79                if(mx>=a[i].x) ans=min(ans,mx-a[i].x);
 80                 else
 81                 {
 82                     multiset<ll>::iterator it=st.lower_bound(a[i].x);
 83                     if(mx!=-1) ans=min(ans,a[i].x-mx);
 84                     if(it!=st.end()) ans=min(ans,(*it)-a[i].x);
 85                     if(it!=st.begin())
 86                     {
 87                         it--;
 88                         if((*it)>mx) ans=min(ans,a[i].x-(*it));
 89                     }
 90                 }
 91             st.insert(a[i].y);
 92        }
 93 
 94        printf("%I64d\n",ans);
 95 
 96     }
 97 
 98 
 99     return 0;
100 }

 

posted on 2019-10-14 15:35  myx12345  阅读(203)  评论(0编辑  收藏  举报

导航