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. django 使用框架下auth.models自带的User进行扩展增加字段

    需要改动三个地方: 1.models.py   创建模型User,并继承原模型类AbstraUser(在此处我增加了一个新的字段手机号) from django.db import models # ...

  2. Python中接收用户的输入

    一.如何去接收用户的输入?使用函数 input() 函数 input() 让程序暂停运行,等待用户输入一些文本,获取用户的输入后,Python将其存储到一个变量中,以方便后期使用. name = in ...

  3. Anaconda换源及常用命令

    推荐一篇文章:http://www.cnblogs.com/IT-LearnHall/p/9486029.html 另外,记录几个自己遇到的问题 首先是换源.无论是安装包还是安装后更新python包, ...

  4. Spider-Python爬虫之PyQuery基本用法

    1.安装方法 pip install pyquery 2.引用方法 from pyquery import PyQuery as pq 3.简介 pyquery 是类型jquery 的一个专供pyth ...

  5. Numpy第一课

    import numpy as np a = np.array([1,2,3]) print(a) # [1 2 3] a2 = np.array([[1, 2], [3, 4]]) print('a ...

  6. 基本dos

    文件夹的操作:   进入指定盘符:盘符名+:     dir:列出当前控制台下的所有文件以及文件夹  . cd +文件夹名称:进入指定文件夹     cd.. 返回上一级 cd \返回到当前目录的根目 ...

  7. 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction

    http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...

  8. CSU - 1115 最短的名字(字典树模板题)

    Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaa ...

  9. poj-1979 && hdoj - 1312 Red and Black (简单dfs)

    http://poj.org/problem?id=1979 基础搜索. #include <iostream> #include <cstdio> #include < ...

  10. js格式化日期时间

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).周(E).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...