奶牛邻居——treap+契比雪夫距离+并查集

题目描述

了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”. 
每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤[1~109];Xi,Yi∈整数. 
当满足下列两个条件之一,两只奶牛i和j是属于同一个群的:  
1.两只奶牛的曼哈顿距离不超过C(1≤C≤109),即lXi - Xjl+IYi- Yjl≤C.  
2.两只奶牛有共同的邻居.即,存在一只奶牛k,使i与k,j与k均同属一个群.  
给出奶牛们的位置,请计算草原上有多少个牛群,以及最大的牛群里有多少奶牛? 

思路:

蒟蒻$fhq Treap$初学,所以代码巨丑,封装的也不美观,导致$insert等操作在main$函数里显得很长,这也是我调了很长时间的原因。

对于曼哈顿距离$|x[i]-x[j]|+|y[i]-y[j]|$,我们将其转化为契比雪夫距离$max(|x[i]-x[j]|,|y[i]-y[j]|)$,此时坐标$(x,y)变成了(x+y,x-y)$。

这样问题便简化了:我们按x为第一关键字,$y$为第二关键字排序。把$x$扔进队列里,维护$fhq Treap$,里面存的是区间$[head,i](x[i]-x[head]<=C)$中所有的y值,这样我们找到$fhq Treap中y[i]$的前驱和后继用并查集合并即可。代码比较难打。

code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<ctime>
#define lc(x) ch[x][0]
#define rc(x) ch[x][1]
using namespace std;
typedef long long LL;
const LL N=100010;
const LL inf=99999999999999999;
struct node
{
    LL x,y;
    LL id;
}cow[N],fir[N];
 
LL ch[N][2],rnk[N],id[N],sz[N],root,x,y,z,cnt;
LL val[N];
LL n,C;
LL ans;
 
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
 
bool cmp(node a,node b)
{
    return a.x==b.x?a.y<b.y:a.x<b.x;
}
 
LL father[N],tot[N];
 
inline LL new_node(LL x,LL pos)
{
    cnt++;
    val[cnt]=x;
    rnk[cnt]=rand();
    sz[cnt]=1;
    id[cnt]=pos;
    return cnt;
}
 
inline void pushup(LL now)
{
    sz[now]=1+sz[lc(now)]+sz[rc(now)];
}
 
inline void split(LL now,LL k,LL &x,LL &y)
{
    if(!now)x=y=0;
    else
    {           
        if(val[now]<=k)x=now,split(rc(now),k,rc(now),y);
        else y=now,split(lc(now),k,x,lc(now));
        pushup(now);
    }
}
 
inline LL merge(LL x,LL y)
{
    if(!x||!y)return x+y;
    if(rnk[x]<rnk[y]){rc(x)=merge(rc(x),y);pushup(x);return x;}
    else {lc(y)=merge(x,lc(y));pushup(y);return y;}
}
 
inline LL kth(LL now,LL k)
{
    while(1)
    {
        if(sz[lc(now)]>=k)now=lc(now);
        else if(sz[lc(now)]+1==k)return now;
        else k-=sz[lc(now)]+1,now=rc(now);
    }
}
 
inline LL find(LL x){return x==father[x]?x:father[x]=find(father[x]);}
 
int main()
{
    //freopen("testdata.in","r",stdin);
    //freopen("bbb.out","w",stdout);
    srand(time(0));
    n=read();C=read();
    for(LL i=1;i<=n;i++)
    {
        LL x=read(),y=read();
        cow[i].id=i;
        cow[i].x=x+y;cow[i].y=x-y;
        fir[i]=cow[i];
    }
    sort(cow+1,cow+1+n,cmp);
    for(LL i=1;i<=n;i++)father[i]=i,tot[i]=1;
    root=new_node(cow[1].y,cow[1].id);
     
    split(root,-inf,x,y);root=merge(merge(x,new_node(-inf,0)),y);
    split(root,inf,x,y);root=merge(merge(x,new_node(inf,n+1)),y);
     
    LL head=1;
    for(LL i=2;i<=n;i++)
    {
        LL curx=cow[i].x,cury=cow[i].y;
    //  cout<<"cur="<<curx<<" "<<cury<<endl;
        while(cow[i].x-cow[head].x>C)
        {
            split(root,cow[head].y,x,z);
            split(x,cow[head].y-1,x,y);
            y=merge(lc(y),rc(y));
            root=merge(merge(x,y),z);
            head++;
        }
        split(root,cury,x,y);
        LL pre=id[kth(x,sz[x])];
        root=merge(x,y);
        split(root,cury-1,x,y);
        LL nxt=id[kth(y,1)];
        root=merge(x,y);
        //cout<<"pre nxt="<<pre<<" "<<nxt<<endl;
         
        if(abs(fir[nxt].y-cury)<=C)
        {
            LL r1=find(nxt),r2=find(cow[i].id);
            if(r1&&r2&&r1!=r2)
            {   
                father[r2]=r1;
                tot[r1]+=tot[r2];tot[r2]=0;
            }
        }
        if(abs(fir[pre].y-cury)<=C)
        {
            LL r1=find(pre),r2=find(cow[i].id);
            if(r1&&r2&&r1!=r2)
            {   
                father[r2]=r1;
                tot[r1]+=tot[r2];tot[r2]=0;
            }
        }
        split(root,cury,x,y);
        root=merge(merge(x,new_node(cury,cow[i].id)),y);        
    }
    LL ans=0;
    LL maxn=0;
    for(LL i=1;i<=n;i++)
    {    
        if(find(i)==i)ans++,maxn=max(maxn,tot[i]);
        //cout<<i<<" "<<find(i)<<" "<<ans<<endl;
    }
    cout<<ans<<" "<<maxn;
}

 

转载于:https://www.cnblogs.com/THRANDUil/p/11565521.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是Treap的代码: ``` #include <bits/stdc++.h> using namespace std; struct Node { int key, priority; Node *left, *right; Node(int key, int priority) : key(key), priority(priority), left(NULL), right(NULL) {} }; struct Treap { Node *root; Treap() : root(NULL) {} void rotateRight(Node *&p) { Node *q = p->left; p->left = q->right; q->right = p; p = q; } void rotateLeft(Node *&p) { Node *q = p->right; p->right = q->left; q->left = p; p = q; } void insert(Node *&p, int key, int priority) { if (p == NULL) { p = new Node(key, priority); return; } if (key < p->key) { insert(p->left, key, priority); if (p->priority < p->left->priority) { rotateRight(p); } } else { insert(p->right, key, priority); if (p->priority < p->right->priority) { rotateLeft(p); } } } void remove(Node *&p, int key) { if (p == NULL) { return; } if (key == p->key) { if (p->left == NULL || p->right == NULL) { Node *q = p; if (p->left == NULL) { p = p->right; } else { p = p->left; } delete q; } else { if (p->left->priority > p->right->priority) { rotateRight(p); remove(p->right, key); } else { rotateLeft(p); remove(p->left, key); } } } else if (key < p->key) { remove(p->left, key); } else { remove(p->right, key); } } bool search(Node *p, int key) { if (p == NULL) { return false; } if (key == p->key) { return true; } if (key < p->key) { return search(p->left, key); } else { return search(p->right, key); } } }; int main() { Treap t; t.insert(t.root, 5, rand()); t.insert(t.root, 3, rand()); t.insert(t.root, 8, rand()); t.insert(t.root, 1, rand()); t.insert(t.root, 4, rand()); t.insert(t.root, 6, rand()); t.insert(t.root, 9, rand());

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值