标签:x
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100010
using namespace std;
int fa[N];
int q[N];
int tot;
int root;
int cnt[N];
struct node
{
int l,r,w,v,no,rnd,siz;
}tr[N];
void pushup(int rt)
{
tr[rt].siz=tr[tr[rt].l].siz+tr[tr[rt].r].siz+tr[rt].w;
}
void rturn(int &rt)
{
int t=tr[rt].l;
tr[rt].l=tr[t].r;
tr[t].r=rt;
tr[t].siz=tr[rt].siz;
pushup(rt);
rt=t;
}
void lturn(int &rt)
{
int t=tr[rt].r;
tr[rt].r=tr[t].l;
tr[t].l=rt;
tr[t].siz=tr[rt].siz;
pushup(rt);
rt=t;
}
void insert(int &rt,int v,int no)
{
if(!rt)
{
rt=++tot;
tr[rt].siz=1,tr[rt].no=no,tr[rt].rnd=rand();
tr[rt].v=v,tr[rt].w=1;
return;
}
tr[rt].siz++;
if(v<=tr[rt].v)
{
insert(tr[rt].l,v,no);
if(tr[tr[rt].l].rnd<tr[rt].rnd)rturn(rt);
}else
{
insert(tr[rt].r,v,no);
if(tr[tr[rt].r].rnd<tr[rt].rnd)lturn(rt);
}
}
void del(int &rt,int v,int no)
{
if(!rt)return;
tr[rt].siz--;
if(tr[rt].v==v&&tr[rt].no==no)
{
if(tr[rt].l*tr[rt].r==0){rt=tr[rt].l+tr[rt].r;return;}
else
{
if(tr[tr[rt].l].rnd<tr[tr[rt].r].rnd)
{
rturn(rt);
del(rt,v,no);
}else
{
lturn(rt);
del(rt,v,no);
}
}
}else if(v<tr[rt].v)
{
del(tr[rt].l,v,no);
}else del(tr[rt].r,v,no);
}
int ans;
void q_pre(int rt,int v)
{
if(!rt)return;
if(v>=tr[rt].v)
{
ans=rt;
q_pre(tr[rt].r,v);
}else q_pre(tr[rt].l,v);
}
void q_sub(int rt,int v)
{
if(!rt)return;
if(v<tr[rt].v)
{
ans=rt;
q_sub(tr[rt].l,v);
}else q_sub(tr[rt].r,v);
}
struct point
{
int x,y;
}pt[N];
int n,c;
int find(int x)
{
if(x!=fa[x])return fa[x]=find(fa[x]);
return x;
}
int cmp(point a,point b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
int main()
{
scanf("%d%d",&n,&c);
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
pt[i].x=x+y,pt[i].y=x-y;
fa[i]=i;
}
sort(pt+1,pt+n+1,cmp);
int head=1,tail=0;
for(int i=1;i<=n;i++)
{
while(head<=tail&&pt[i].x-pt[q[head]].x>c)
{
del(root,pt[q[head]].y,q[head]);
head++;
}
ans=0;
q_pre(root,pt[i].y);
int tmp=ans;
if(tmp!=0)
{
if(pt[i].y-tr[tmp].v<=c)
{
if(find(i)!=find(tr[tmp].no))
{
fa[find(i)]=find(tr[tmp].no);
}
}
}
ans=0;
q_sub(root,pt[i].y);
tmp=ans;
if(tmp!=0)
{
if(tr[tmp].v-pt[i].y<=c)
{
if(find(i)!=find(tr[tmp].no))
{
fa[find(i)]=find(tr[tmp].no);
}
}
}
insert(root,pt[i].y,i);
q[++tail]=i;
}
int ma=0,print=0;
for(int i=1;i<=n;i++)
{
int fx=find(i);
if(!cnt[fx])
{
print++;
}
cnt[fx]++;
if(cnt[fx]>ma)ma=cnt[fx];
}
printf("%d %d\n",print,ma);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap
标签:x
原文地址:http://blog.csdn.net/wzq_qwq/article/details/47746091