19 hdu多校 Welcome Party //主席树

http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=857&pid=1005

题意:n个人,有xi和yi属性,将n个人分成两个队伍,分到a队只有xi属性,分到b队只有yi属性,每个队伍得至少一人,求分队后最小的abs(a队xi的最大值-b队yi的最大值)。

思路:主席树,枚举x最大是谁,考虑y最大是谁。枚举到x最大的人的下标为p时,当确定p之后,属性xi大于p.xi的人只能在b队。属性xi小于p.xi的人,考虑yi和p.xi最接近的。(大于等于或者小于)

/*   (o O o)
 *   Author : Rshs
 *   Data : 2019-08-21-14.10
 */
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define FI first
#define SE second
#define MP make_pair
#define PII pair<int,int>
const LL mod = 1e9+7;
const int MX = 1e5+5;
const int max_n =1e5+5,logn=20;
int root[max_n*logn],ls[max_n*logn],rs[max_n*logn]; //下标为i的树他的左右子树的对应sum的下标
int sum[max_n*logn];//下标为i的区间(上面的区间对应进来)的数字数量
int cnt;//树的数量
struct no{
    LL x,y;
}a[MX];
bool operator<(const no &x,const no &y){
    return x.x<y.x;
}
vector<LL>Y;
void build(int l,int r,int &now){
    now=++cnt;//对应新开的区域
    sum[now]=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(l,mid,ls[now]);
    build(mid+1,r,rs[now]);
}
void update(int l,int r,int &now,int last,int pos){
    now=++cnt;//对应新开的区域
    ls[now]=ls[last];
    rs[now]=rs[last];
    sum[now]=sum[last]+1;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(pos<=mid) update(l,mid,ls[now],ls[last],pos);
    else update(mid+1,r,rs[now],rs[last],pos);
}
int query(int l,int r,int t1,int t2,int num){//区间第k大
    if(l==r) return l;
    int mid=(l+r)>>1;
    int cc=sum[rs[t2]]-sum[rs[t1]]; //第一棵树左儿子减第二颗树左儿子
    if(num<=cc) query(mid+1,r,rs[t1],rs[t2],num);
    else query(l,mid,ls[t1],ls[t2],num-cc); //减去已有的数量
}
LL Query(int L,int R,int l,int r,int t1,int t2){ //[L,R]里面元素个数
    if(l>=L&&r<=R)
        return 1LL*sum[t2]-sum[t1];
    int mid=(l+r)>>1;
    LL re=0;
    if(mid>=L) re+=Query(L,R,l,mid,ls[t1],ls[t2]);
    if(mid<R) re+=Query(L,R,mid+1,r,rs[t1],rs[t2]);
    return re;
}
int main(){
    //out<<LLONG_MAX;
    int T;cin>>T;while(T--){
        cnt=0;Y.clear();
        int n;cin>>n;for(int i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].y);
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++) Y.push_back(a[i].y);
        sort(Y.begin(),Y.end());Y.erase(unique(Y.begin(),Y.end()),Y.end());
        int sz=Y.size();
       // build(1,sz,root[0]);
        for(int i=1;i<=n;i++){
            int id=lower_bound(Y.begin(),Y.end(),a[i].y)-Y.begin()+1;
            update(1,sz,root[i],root[i-1],id);
        }
        LL ans=LLONG_MAX;
        for(int i=1;i<=n;i++){
            LL MX=LLONG_MIN/2;
            if(i!=n){
                LL g=Y[query(1,sz,root[i],root[n],1)-1];
                MX=g;
            }
            LL tMX=MX;
            if(i!=1){
                int id=upper_bound(Y.begin(),Y.end(),a[i].x)-Y.begin()-1+1;
                int cc;
                if(id<1)cc=0;
                else cc=Query(1,id,1,sz,root[0],root[i-1]);//小于等于a[i].x
                int dd=Query(1,sz,1,sz,root[0],root[i-1]);
                cc=dd-cc;
                for(int j=cc;j<=cc+1;j++){
                    if(j>=1&&j<=dd){
                        LL g=Y[query(1,sz,root[0],root[i-1],j)-1];
                        if(g>tMX&&abs(g-a[i].x)<abs(a[i].x-MX)) MX=g;
                    }
                }
            }
            ans=min(ans,abs(a[i].x-MX));
        }
        cout<<ans<<'\n';
    }
    return 0;
}
/*
10
4
1 9
7 6
5 5
4 7*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值