嘟嘟嘟




做过[国家集训队]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. SpringMVC入门学习(二)

    SpringMVC入门学习(二) ssm框架 springMVC  在上一篇博客中,我简单介绍了一下SpringMVC的环境配置,和简单的使用,今天我们将进一步的学习下Springmvc的操作. mo ...

  2. 大数据技术之_08_Hive学习_04_压缩和存储(Hive高级)+ 企业级调优(Hive优化)

    第8章 压缩和存储(Hive高级)8.1 Hadoop源码编译支持Snappy压缩8.1.1 资源准备8.1.2 jar包安装8.1.3 编译源码8.2 Hadoop压缩配置8.2.1 MR支持的压缩 ...

  3. EXTJS4 Grid Filter 插件的使用 与后台数据解析------Extjs 查询筛选功能的实现

    先汗一个,一个小功能又踢腾了一天.本来这个带Demo的,但是上面介绍的不是很详细.用的时候问题不大,主要问题在文件导入方面.以为这个插件的使用和其他的不一样. 1.首先是需要引入文件的位置:如图 需要 ...

  4. 如何完全卸载oracle和删除oracle在注册表中的注册信息

    卸载步骤介绍 1.停止所有Oracle相关的服务 操作方法: 控制面板-->管理工具 -->服务 -->将所有oracle开头的服务均停止 2.卸载Oracle 10g数据库服务器组 ...

  5. 性能监控(1)--linux下的top命令

    Linux下的监控工具 top命令 top命令能够实时显示系统中各个进程的资源占用情况,其输出信息分为两部分,前半部分为系统统计信息,后半部分是进程信息. 第一行是任务队列信息,它的结果等同于upti ...

  6. vue常用笔记

    vue源码解析(勾三股四):http://jiongks.name/blog/vue-code-review/ 0.npm: https://www.npmjs.com/ 1. package.jso ...

  7. 20个Chrome DevTools调试技巧

    译者按: Chrome DevTools很强大,甚至可以替代IDE了! 原文: Art of debugging with Chrome DevTools 译者: Fundebug 为了保证可读性,本 ...

  8. inheritConstructorStealing.js

    // 借用构造函数 // 其基本思路是在子类型构造函数的内部调用父类型的构造函数 function Person(name){ this.name = name; this.friends = [&q ...

  9. 新浪微博POI点签到数据及可视化的初步成果

    目前仅对山东省区域进行了抓取,权限不够高,抓取的速度非常慢,所以导致效率比较低... 数据抓取采用调用微博开放平台API的方法,数据存储采用mysql,格点数据分辨率为30″,山东省的MBR范围内(包 ...

  10. CentOS7.4 系统下 Tomcat 启动慢解决方法

    CentOS7.4 系统下 Tomcat 启动慢解决的方法   首先查看日志信息,查看因为什么而启动慢 在CentOS7启动Tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是s ...