LibreOJ2043 - 「CQOI2016」K 远点对
Description
给出平面上的\(n(n\leq10^5)\)个整点,求在欧几里得距离下第\(k\)远的点对之间的距离。
Solution
k-d树+堆。
用小根堆维护当前找到的第\(k\)大,然后以堆顶元素为基准在k-d树上搜索即可。搜索到一个新值\(d\)时,将其与堆顶元素比较,若大于堆顶元素则弹出堆顶并加入\(d\)。
Code
//「CQOI2016」K 远点对
#include <algorithm>
#include <cstdio>
#include <queue>
typedef long long lint;
using namespace std;
inline char gc()
{
static char now[1<<16],*s,*t;
if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}
return *s++;
}
inline int read()
{
int x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
const int N=1e5+10;
const int INF=0x3FFFFFFF;
int n,k;
int rt,ch[N][2];
struct point{int c[2];} pt[N];
struct zone{int c1[2],c2[2];} zn[N];
void update(int p)
{
int ch0=ch[p][0],ch1=ch[p][1];
for(int k=0;k<2;k++)
zn[p].c1[k]=min(pt[p].c[k],min(zn[ch0].c1[k],zn[ch1].c1[k])),
zn[p].c2[k]=max(pt[p].c[k],max(zn[ch0].c2[k],zn[ch1].c2[k]));
}
int D;
bool operator<(point A,point B) {return A.c[D]<B.c[D];}
void bldTr(int &p,int L,int R)
{
if(L>R) return;
int mid=L+R>>1; p=mid;
nth_element(pt+L,pt+mid,pt+R+1);
bldTr(ch[p][0],L,mid-1),bldTr(ch[p][1],mid+1,R);
update(p);
}
priority_queue< lint,vector<lint>,greater<lint> > Q;
point A;
lint dist(point B)
{
lint d=0;
for(int k=0;k<2;k++) d+=1LL*(A.c[k]-B.c[k])*(A.c[k]-B.c[k]);
return d;
}
lint dist(zone Z)
{
if(Z.c1[0]>Z.c2[0]) return 0;
int d[2]; d[0]=d[1]=0;
for(int k=0;k<2;k++) d[k]=max(abs(Z.c1[k]-A.c[k]),abs(Z.c2[k]-A.c[k]));
return 1LL*d[0]*d[0]+1LL*d[1]*d[1];
}
void query(int p)
{
if(!p) return;
lint d=dist(pt[p]);
if(d>Q.top()) Q.pop(),Q.push(d);
lint d0=dist(zn[ch[p][0]]),d1=dist(zn[ch[p][1]]);
if(d0>Q.top()) query(ch[p][0]);
if(d1>Q.top()) query(ch[p][1]);
}
int main()
{
n=read(),k=read();
for(int i=1;i<=n;i++) pt[i].c[0]=read(),pt[i].c[1]=read();
zn[0].c1[0]=zn[0].c1[1]=INF,zn[0].c2[0]=zn[0].c2[1]=-INF;
bldTr(rt,1,n);
for(int i=1;i<=k+k;i++) Q.push(0);
for(int i=1;i<=n;i++) A=pt[i],query(rt);
printf("%d\n",Q.top());
return 0;
}
P.S.
网上好多题解是凸包+旋转卡壳...吓死我了∑(゚Д゚ノ)ノ
LibreOJ2043 - 「CQOI2016」K 远点对的更多相关文章
- 「CQOI2016」K 远点对
/* 考虑暴力 可以n ^ 2枚举点对 然后用一个容量为2k的小根堆来维护第k大 kd-tree呢就是来将这个暴力优化, 每次先找远的并且最远距离不如堆顶的话就不继续找下去 貌似挺难构造数据卡的 */ ...
- loj2043 「CQOI2016」K 远点对
k-d tree 裸题------ #include <algorithm> #include <iostream> #include <cstdio> using ...
- LibreOJ2044 - 「CQOI2016」手机号码
Portal Description 给出两个十一位数\(L,R\),求\([L,R]\)内所有满足以下两个条件的数的个数. 出现至少\(3\)个相邻的相同数字: 不能同时出现\(4\)和\(8\). ...
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- loj #2044. 「CQOI2016」手机号码
#2044. 「CQOI2016」手机号码 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- LibreOJ2045 - 「CQOI2016」密钥破解
Portal Description 给出三个正整数\(e,N,c(\leq2^{62})\).已知\(N\)能表示成\(p\cdot q\)的形式,其中\(p,q\)为质数.计算\(r=(p-1)( ...
- LibreOJ2042 - 「CQOI2016」不同的最小割
Portal Description 给出一个给出一个\(n(n\leq850)\)个点\(m(m\leq8500)\)条边的无向图.定义\(cut(s,t)\)等于\(s,t\)的最小割的容量,求在 ...
- 「CQOI2016」不同的最小割
「CQOI2016」不同的最小割 传送门 建出最小割树,把每一个点对的最小割抠出来 \(\text{unique}\) 一下就好了. 参考代码: #include <algorithm> ...
- 「ZJOI2013」K大数查询
「ZJOI2013」K大数查询 传送门 整体二分,修改的时候用线段树代替树状数组即可. 参考代码: #include <cstdio> #define rg register #defin ...
随机推荐
- HDU - 5491 The Next 2015 ACM/ICPC Asia Regional Hefei Online
从D+1开始,对于一个数x,区间[x,x+lowbit(x))内的数字的二进制位上1的数量整体来说是单调不减的,因此可快速得出1在这个区间的取值范围. 每次判断一下有没有和[s1,s2]有没有交集,一 ...
- AutoWidthInput
import React from 'react'; import PropTypes from 'prop-types'; class AutoWidthInput extends React.Co ...
- NOIP模拟赛 准考证号
准考证号 128M 0.1s ticket.cpp escription 蒟蒻hzwer NOIP2014惨跪,他依稀记得他的准考证号是37,现在hzwer又将要面临一场比赛,他希望准考证号不出现37 ...
- node中的定时任务
node-schedule每次都是通过新建一个scheduleJob对象来执行具体方法. 时间数值按下表表示 * * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ ...
- ubuntu18.04+win10解决时钟不同步办法
安装ntpdate: 执行命令: sudo apt-get install ntpdate 设置校正服务器: sudo ntpdate time.windows.com 设置硬件时间为本地时间: 执行 ...
- webpack4搭建Vue开发环境笔记~~持续更新
项目git地址 一.node知识 __dirname: 获取当前文件所在路径,等同于path.dirname(__filename) console.log(__dirname); // Prints ...
- 【php】【特殊案例】数组调用方法
As of PHP 5.4.0, you can call any callable stored in a variable. <?php class Foo { static functio ...
- redis集群监控之Redis-monitor部
为了对以后有可能面临的redis集群监控做准备,这两天在准备这方面的事情,现在将其中的过程记录一下. 首先是“Ronney-Hua”的这篇文章对三中开源监控软件做了对比 文章地址:https://bl ...
- Nastya Studies Informatics CodeForces - 992B (大整数)
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- 开源OA系统启动:基础数据,工作流设计
原文:http://www.cnblogs.com/kwklover/archive/2007/01/13/bpoweroa_03_baseandworkflowdesign.html自从开源OA系统 ...