treap+并查集

我们能想到一个点和最近点对连接,用并查集维护,但是这个不仅不能求,而且还是不对的,于是就看了题解

把距离转为A(x-y,x+y),这样两点之间的距离就是max(x'-X',y'-Y'),那么就可以求了,我们按转换后的x排序,维护一个区间,最大的x和最小的x差不超过c,然后把y插进treap里,每次查找前驱后继,如果距离小于等于c就连接,最后扫一遍统计答案就行

曼哈顿和切比雪夫距离是可以互相转换的,x=x-y,y=x+y就行,切比雪夫距离转换为曼哈顿距离转换回去也可以

#include<bits/stdc++.h>
using namespace std;
const int N = , seed = , inf = ;
struct data {
int x, y;
data(int x = , int y = ) : x(x), y(y) {}
bool friend operator < (data A, data B) { return A.x < B.x; }
} a[N];
int n, ans, root;
long long c;
pair<int, int> pre, nxt;
int sum[N], fa[N];
struct Treap {
int cnt, P;
pair<int, int> key[N];
int size[N], child[N][], p[N], tot[N];
inline int rand() { P = P * seed + ; return abs(P); }
inline void update(int x) { size[x] = size[child[x][]] + size[child[x][]] + tot[x]; }
inline void rotate(int &x, int t)
{
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y); x = y;
}
inline void insert(int &x, pair<int, int> o)
{
if(x == ) { p[x = ++cnt] = rand(); key[x] = o; tot[x] = ; }
else
{
if(key[x] == o) ++tot[x];
else { int t = o > key[x]; insert(child[x][t], o); if(p[child[x][t]] > p[x]) rotate(x, t); }
}
update(x);
}
inline void erase(int &x, pair<int, int> o)
{
if(key[x] == o)
{
if(child[x][] == && child[x][] == ) { --tot[x]; if(tot[x] == ) x = ; else update(x); }
else { int t = p[child[x][]] > p[child[x][]]; rotate(x, t); erase(child[x][t ^ ], o); }
}
else erase(child[x][o > key[x]], o);
update(x);
}
inline void query_pre(int x, pair<int, int> o)
{
if(x == ) return;
if(key[x] > o) query_pre(child[x][], o);
else { if(key[x] > pre) pre = key[x]; query_pre(child[x][], o); }
}
inline void query_nxt(int x, pair<int, int> o)
{
if(x == ) return;
if(key[x] < o) query_nxt(child[x][], o);
else { if(key[x] < nxt) nxt = key[x]; query_nxt(child[x][], o); }
}
} treap;
inline int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
inline void connect(int x, int y)
{
int u = find(x), v = find(y);
if(u == v) return;
--ans;
fa[v] = u;
}
int main()
{
// freopen("nabor.in", "r", stdin);
// freopen("nabor.out", "w", stdout);
scanf("%d%lld", &n, &c);
ans = n;
for(int i = ; i <= n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
a[i] = data(x - y, x + y);
}
a[].x = inf;
sort(a + , a + n + );
treap.insert(root, {-inf, -});
treap.insert(root, {inf, -});
for(int i = ; i <= n; ++i) fa[i] = i;
int l = ;
for(int i = ; i <= n; ++i)
{
pair<int, int> o;
while(l <= i && a[i].x - a[l].x > c)
{
o = make_pair(a[l].y, l);
treap.erase(root, o);
++l;
}
nxt = {inf + , -};
pre = {-inf - , -};
o = make_pair(a[i].y, i);
treap.query_pre(root, o);
treap.query_nxt(root, o);
long long dis_pre, dis_nxt;
treap.insert(root, o);
dis_pre = (long long)a[i].y - (long long)pre.first;
dis_nxt = (long long)nxt.first - (long long)a[i].y;
if(dis_pre <= c && pre.second != -) connect(i, pre.second);
if(dis_nxt <= c && nxt.second != -) connect(i, nxt.second);
}
printf("%d ", ans);
ans = ;
for(int i = ; i <= n; ++i) ++sum[find(i)];
for(int i = ; i <= n; ++i) ans = max(ans, sum[i]);
printf("%d\n", ans);
// fclose(stdin);
// fclose(stdout);
return ;
}

bzoj1604的更多相关文章

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

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

  2. 【BZOJ1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Treap+并查集

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

  3. [BZOJ1604] [Usaco2008 Open] Cow Neighborhoods 奶牛的邻居 (queue & set)

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

  4. bzoj1604 / P2906 [USACO08OPEN]牛的街区Cow Neighborhoods

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 考虑维护曼哈顿距离:$\left | x_{1}-x_{2} \right |+\left | y_{1}-y_{2} ...

  5. BZOJ1604 & 洛谷2906:[USACO2008 OPEN]Cow Neighborhoods 奶牛的邻居——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1604 https://www.luogu.org/problemnew/show/P2906#sub ...

  6. 【bzoj1604】【[Usaco2008 Open]Cow Neighborhoods】简单的谈谈曼哈顿距离

    (最近p站上不去要死了) Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个"群".每只奶牛在吃 ...

  7. 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set

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

  8. [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)

    传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...

  9. bzoj1604 牛的邻居 STL

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

随机推荐

  1. Volume 1. Maths - Misc

    113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...

  2. day 21 03 补全异常处理

    day 21 03  异常处理(补全) 1.异常处理的整体几个语句: try: .......#有可能出错的代码 ret=int(input('number >>>')) print ...

  3. 84-Market Facilitation Index 市场促进指数指标.(2015.7.3)

    Market Facilitation Index 市场促进指数指标 MFI指标的计算方式为: MFI=High(最高价)-Low(最低价))/ Volume(成交量) MFI上升,成交量上升,表示价 ...

  4. python 调用 C 动态库

    首先是 C 的头文件和源文件, #ifndef POINT_H #define POINT_H struct point { int x; int y; }; void point_print(str ...

  5. Mysql学习总结(43)——MySQL主从复制详细配置

    环境 操作系统:CentOS-6.6-x86_64-bin-DVD1.iso MySQL版本:mysql-5.6.26.tar.gz 主节点IP:192.168.1.205 主机名:edu-mysql ...

  6. [HDU1576] A/B(扩展欧几里得)

    传送门 n = A % 9973 -> n = A - A / 9973 * 9973 设 x = A / B(题目所述,B|A) -> A = B * x 所以 B * x - A / ...

  7. bzoj 1962 硬币游戏 (猜数问题)

    [bzoj1962]模型王子 2015年3月26日1,6460 Description Input 输入数据共一行,两个整数N,K,用一个空格隔开,具体意义如题目中所述. Output 输出数据共一行 ...

  8. 详解SpringBoot 添加对JSP的支持(附常见坑点)

    序言: SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作.为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP . 常见 ...

  9. JSP内置对象和EL内置对象

    JSP共有九大内置对象: (1) HttpSession类的session对象作用:主要用于来分别保存每个用户信息,与请求关联的会话:         会话状态维持是Web应用开发者必须面对的问题. ...

  10. Operating system management of address-translation-related data structures and hardware lookasides

    An approach is provided in a hypervised computer system where a page table request is at an operatin ...