嘟嘟嘟




做过[国家集训队]JZPFAR这道题的话,这题就不难了。




我们维护一个长度为\(k\)的小根堆,在加入第\(i\)个点之前,用\([1, i - 1]\)这些点离点\(i\)的距离更新答案。这样也能保证每一对点之间的距离一定只算了一次。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int SIZE = 5000;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, K, Dim;
priority_queue<ll, vector<ll>, greater<ll> > q;
struct Tree
{
int ch[2];
ll d[2], Min[2], Max[2];
In bool operator < (const Tree& oth)const
{
return d[Dim] < oth.d[Dim];
}
}t[maxn], a[maxn];
int root, tcnt = 0, cnt = 0;
In void pushup(int now)
{
for(int i = 0; i < 2; ++i)
{
if(t[now].ch[0])
{
t[now].Min[i] = min(t[now].Min[i], t[t[now].ch[0]].Min[i]);
t[now].Max[i] = max(t[now].Max[i], t[t[now].ch[0]].Max[i]);
}
if(t[now].ch[1])
{
t[now].Min[i] = min(t[now].Min[i], t[t[now].ch[1]].Min[i]);
t[now].Max[i] = max(t[now].Max[i], t[t[now].ch[1]].Max[i]);
}
}
}
In void new_node(int& now, Tree a)
{
t[now = ++tcnt] = a;
for(int i = 0; i < 2; ++i)
{
t[now].ch[i] = 0;
t[now].Min[i] = t[now].Max[i] = t[now].d[i];
}
}
In void build(int& now, int L, int R, int d)
{
if(L > R) return;
int mid = (L + R) >> 1;
Dim = d;
nth_element(a + L, a + mid, a + R + 1);
new_node(now, a[mid]);
build(t[now].ch[0], L, mid - 1, d ^ 1);
build(t[now].ch[1], mid + 1, R, d ^ 1);
pushup(now);
}
In void insert(int& now, Tree a, int d)
{
if(!now) {new_node(now, a); return;}
if(a.d[d] <= t[now].d[d]) insert(t[now].ch[0], a, d ^ 1);
else insert(t[now].ch[1], a, d ^ 1);
pushup(now);
}
In ll dis(int now, ll * d)
{
ll ret = 0;
for(int i = 0; i < 2; ++i) ret += (d[i] - t[now].d[i]) * (d[i] - t[now].d[i]);
return ret;
}
In ll price(int now, ll* d)
{
ll ret = 0;
for(int i = 0; i < 2; ++i)
{
ll Max = max(abs(t[now].Min[i] - d[i]), abs(t[now].Max[i] - d[i]));
ret += Max * Max;
}
return ret;
}
In void query(int now, ll* d)
{
if(!now) return;
ll tp = dis(now, d);
if(tp > q.top()) q.pop(), q.push(tp);
ll disL = price(t[now].ch[0], d), disR = price(t[now].ch[1], d);
if(disL < disR) swap(t[now].ch[0], t[now].ch[1]), swap(disL, disR);
if(disL > q.top()) query(t[now].ch[0], d);
if(disR > q.top()) query(t[now].ch[1], d);
} int main()
{
n = read(); K = read();
for(int i = 1; i <= K; ++i) q.push(-1);
for(int i = 1; i <= n; ++i)
{
a[++cnt].d[0] = read(), a[cnt].d[1] = read();
query(root, a[cnt].d);
if(cnt % SIZE == 0)
{
for(int i = 1; i <= tcnt; ++i) t[i].ch[0] = t[i].ch[1] = 0;
tcnt = 0;
build(root, 1, cnt, 0);
}
else insert(root, a[cnt], 0);
}
write(q.top()), enter;
return 0;
}

[CQOI2016]K远点对的更多相关文章

  1. BZOJ 4520: [Cqoi2016]K远点对

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 638  Solved: 340[Submit][Status ...

  2. [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1285  Solved: 708[Submit][Statu ...

  3. 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆

    [BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...

  4. [Cqoi2016]K远点对 K-Dtree

    4520: [Cqoi2016]K远点对 链接 bzoj 思路 用K-Dtree求点的最远距离. 求的时候顺便维护一个大小为2k的小根堆. 不知道为啥一定会对. 代码 #include <bit ...

  5. [bzoj4520][Cqoi2016]K远点对_KD-Tree_堆

    K远点对 bzoj-4520 Cqoi-2016 题目大意:已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. 注释:$1\le n\le 10^5$,$1\le k\le 100$,$k\l ...

  6. BZOJ4520 [Cqoi2016]K远点对

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  7. 【bzoj4520】 Cqoi2016—K远点对

    http://www.lydsy.com/JudgeOnline/problem.php?id=4520 (题目链接) 题意 求平面内第K远点对的距离. Solution 左转题解:jump 细节 刚 ...

  8. BZOJ 4520 [Cqoi2016]K远点对(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4520 [题目大意] 求K远点对距离 [题解] 修改估价函数为欧式上界估价,对每个点进行 ...

  9. BZOJ 4520: [Cqoi2016]K远点对(k-d tree)

    Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1162  Solved: 618[Submit][Status][Discuss] Descripti ...

  10. BZOJ4520:[CQOI2016]K远点对(K-D Tree)

    Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 N 行,每行两个整数 X,Y,表示一个点 的坐标 ...

随机推荐

  1. IOS中input键盘事件支持的解决方法

    欢迎大家去我的网站详细查看http://genghongshuo.com.cn/ IOS中input键盘事件keyup.keydown.等支持不是很好, 用input监听键盘keyup事件,在安卓手机 ...

  2. Linux中ssh介绍与ssh+key密钥登陆部署

    环境内核信息: [root@zabbix- ~]# uname -a Linux zabbix- -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 x86 ...

  3. JS通过百度地图API获取当前定位信息

    $(function(){ var latlon=null; //ajax获取用户所在经纬度 $.ajax({ url:"http://api.map.baidu.com/location/ ...

  4. Spark的优势

    首先了解一下Mapreduce 它最本质的两个过程就是Map和Reduce,Map的应用在于我们需要数据一对一的元素的映射转换,比如说进行截取,进行过滤,或者任何的转换操作,这些一对一的元素转换就称作 ...

  5. Java--实现单点登录

    1 什么是单点登陆 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用 ...

  6. JavaScript一团乱,这是好事

    译者按: JavaScript从简单变复杂了,作者从另一个角度看待这个问题. 原文: JavaScript’s a mess – and that’s a good thing 译者: Fundebu ...

  7. JavaScript--水平幻灯片

    // 列表布局,图片左浮动,外侧容器设置overflow:hidden; // 组合使用构造函数模式(对象不共享的数据)和原型模式(对象共享的数据) <!DOCTYPE html> < ...

  8. 浅析requests库响应对象的text和content属性

    在做爬虫时请求网页的requests库是必不可少的,我们常常会用到 res = resquests.get(url) 方法,在获取网页的html代码时常常使用res的text属性: html = re ...

  9. javascript刷新父页面的各种方法汇总

    1.用iframe.弹出子页面刷新父页面iframe <script language=JavaScript> parent.location.reload(); </script& ...

  10. [VUE ERROR] Duplicate keys detected: 'tab-user'. This may cause an update error.

    错误消息如图: 如果你看到此错误消息,则说明 v-for 指令的 key值 重复了,只需修改你的 key值 让其不会重复即可.