大致题意:统计平面上由曼哈顿距离小于等于c的点对组成联通块的个数。

  曼哈顿生成树的模板题。有关讲解:http://blog.csdn.net/acm_cxlove/article/details/8890003

  

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 100100
#define MAXE MAXN*8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
int n,m;
struct edge
{
int x,y;
qword d;
}e[MAXE];
int tope=-;
bool cmp_d(edge e1,edge e2)
{
return e1.d<e2.d;
}
int uf[MAXN];
int get_fa(int now)
{
return uf[now]==now ? now : uf[now]=get_fa(uf[now]);
}
int comb(int x,int y)
{
x=get_fa(x);
y=get_fa(y);
if (x==y)return false;
uf[x]=y;
return true;
}
struct point
{
qword x,y;
int id;
}pl[MAXN];
bool cmp_x(point p1,point p2)
{
if (p1.x==p2.x)return p1.y>p2.y;
return p1.x>p2.x;
}
void rotate1()
{
for (int i=;i<n;i++)
pl[i].x=-pl[i].x;
}
void rotate2()
{
for (int i=;i<n;i++)
pl[i].y=-pl[i].y;
}
void rotate3()
{
for (int i=;i<n;i++)
swap(pl[i].x,pl[i].y);
}
qword h[MAXN],toph=-;
qword tarr[MAXN];
int tarr_id[MAXN];
void Add_tarr(int pos,qword v,int id)
{
while (pos<MAXN)
{
tarr[pos]=min(tarr[pos],v);
if (v==tarr[pos])tarr_id[pos]=id;
pos+=pos&(-pos);
}
}
pair<qword,int> Qry_tarr(int pos)
{
pair<qword,int> ret;
ret.first=INFL;
while (pos)
{
ret.first=min(ret.first,tarr[pos]);
if (ret.first==tarr[pos])
ret.second=tarr_id[pos];
pos-=pos&(-pos);
}
return ret;
}
void work()
{
int i,j;
toph=;
memset(tarr,0x3f,sizeof(tarr));
for (i=;i<n;i++)
h[++toph]=pl[i].x-pl[i].y;
sort(h+,h+toph+);
toph=unique(h+,h+toph+)-h-;
sort(pl,pl+n,cmp_x);
for (i=;i<n;i++)
{
pair<qword,int> pr;
qword t=pl[i].x-pl[i].y;
t=lower_bound(h+,h+toph+,t)-h;
pr=Qry_tarr(t);
if (pr.first!=INFL)
{
e[++tope].x=pl[i].id;
e[tope].y=pr.second;
e[tope].d=pr.first-pl[i].x-pl[i].y;
}
Add_tarr(t,pl[i].x+pl[i].y,pl[i].id);
}
}
int is_root[MAXN],uf_size[MAXN];
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k,x,y,z;
scanf("%d%d",&n,&m);
for (i=;i<n;i++)
{
scanf("%lld%lld",&pl[i].x,&pl[i].y);
pl[i].id=i;
}
//
work();
//
rotate3();
work();
rotate3();
//
rotate2();
rotate3();
work();
rotate3();
rotate2();
//
rotate2();
work();
rotate2();
//
rotate2();
rotate1();
work();
rotate1();
rotate2();
//
rotate1();
rotate2();
rotate3();
work();
rotate3();
rotate2();
rotate1();
//
rotate1();
rotate3();
work();
rotate3();
rotate1();
//
rotate1();
work();
rotate1();
sort(e,e+tope+,cmp_d);
for (i=;i<=n;i++)uf[i]=i;
for (i=;i<=tope && e[i].d<=m;i++)
{
comb(e[i].x,e[i].y);
}
int ans2=;
for (i=;i<n;i++)
{
is_root[get_fa(i)]=;
uf_size[uf[i]]++;
}
for (i=;i<n;i++)
is_root[i]+=is_root[i-];
for (i=;i<n;i++)
ans2=max(ans2,uf_size[i]);
printf("%d %d\n",is_root[n-],ans2);
return ;
}

bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 曼哈顿生成树的更多相关文章

  1. BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec  Memory Limit: 64 MB Description ...

  2. bzoj 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集)

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的 时候有一个独一无二的位置坐标Xi,Yi( ...

  3. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...

  4. BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居:队列 + multiset + 并查集【曼哈顿距离变形】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1604 题意: 平面直角坐标系中,有n个点(n <= 100000,坐标范围10^9) ...

  5. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】

    参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...

  6. BZOJ 1604 [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap

    题意:链接 方法: Treap 解析: 前几道资格赛的题水的不行,这道Gold的题就够分量辣. 首先这个曼哈顿距离啥的肯定能做文章,怎么转化是个问题,自己玩了一会没玩出来,就查了查曼哈顿距离的转化,发 ...

  7. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(set+并查集+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 这题太神了... 简直就是 神思想+神做法+神stl.. 被stl整的我想cry...首先,, ...

  8. 【BZOJ】1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [算法]并查集+平衡树+数学+扫描线 [题解] 经典曼哈顿距离转切比雪夫距离. 曼哈顿距离:S=|x1-x2|+|y1-y2|<=c 即:max(x1-x2+y1-y2,x1-x2-y1+y2, ...

  9. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

随机推荐

  1. Enable SPI 1.0 and 1.1 with device tre overlays on BeagleBone

    For most people the above image means absolutely nothing, but for that one guy that has been searchi ...

  2. 【Unicode】字符编码表信息

    UTF-8有点类似于Haffman编码,它将Unicode编码为:0x00-0x7F的字符,用单个字节来表示:0x80-0x7FF的字符用两个字节表示:0x800-0xFFFF的字符用3字节表示:   ...

  3. web.xml配置文件 taglib

      web.xml的内容如下: <?xml version="1.0" encoding="UTF-8"?><web-app version= ...

  4. 利用C语言强行点击置灰的按钮

    通常很多情况下,会有这样的事情,就是: 我们在运行某些程序的时候,发现按钮置灰了,比如购买版权或者输入序列号才能够获得访问权限.某个按钮才允许点击. 其实所有的这些东西都是 别的人或者公司利用一些编程 ...

  5. YII缓存Cache

    缓存Cache 定义:将数据暂时存放在一个存储速度更快的介质上,下次读取数据时就可以从这个介质上来读取数据 介质:内存.文件.数据库(优化好的数据库) Yii缓存的分类:(framework/cach ...

  6. sublime text 3快捷键设置

    sublime text 3  v-3103默认快捷键设置 [ { "keys": ["ctrl+shift+n"], "command": ...

  7. JQuery设置input属性(disabled、enabled)

    document.getElementById("removeButton").disabled = false; //普通Js写法 $("#removeButton&q ...

  8. ADO.NET连接数据库的两种方式

    //实现了IDisposable接口的类,用using括起来 //插入数据 string connString = "Data Source=(local);Initial Catalog= ...

  9. 20160314 Servlet 入门

    一.Servlet 1.sun提供的一种动态web资源开发技术.本质上就是一段java小程序.可以将Servlet加入到Servlet容器中运行. *Servlet容器 -- 能够运行Servlet的 ...

  10. 阿里云ECS被攻击

    今天发现阿里云ECS被攻击了,记录一下, /1.1 Match1:{ :;};/usr/bin/perl -e 'print .content-type: text/plain.r.n.r.nxsuc ...