洛谷4631 [APIO2018] Circle selection 选圆圈 (KD树)
qwq纪念AC450
一开始想这个题想复杂了。
首先,正解的做法是比较麻烦的。
qwqq
那么就不如来一点暴力的东西,看到平面上点的距离的题,不难想到\(KD-Tree\)
我们用类似平面最近点对那个题一样的维护方式,对于一个子树内部,分别维护每一个维度的最大值和最小值,还有半径的最大值。
然后\(sort\)一遍,从半径大到小依次\(query\),每次\(query\)的时候,对于当前点,合法的条件是他和目标点的距离要小于等于两个圆的半径的和。
那么对于子树的估价函数,我们默认如果当前目标点的当前维度的范围是\(mn-mx\)之间的话,距离就是\(0\),否则取一个最短距离,然后和上述要求一样的比较方式。进行剪枝。
这样就能直接通过洛谷的数据了,但是由于\(loj\)的数据比较强,所以还需要将原来的点绕着一个点旋转一下。
那么假设我们旋转的角度是\(\phi\)
那么我们需要将原来那两个坐标乘上一个矩阵
cos -sin
sin cos
直接上代码了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define pb push_back
#define mk make_pair
#define ll long long
#define lson ch[x][0]
#define rson ch[x][1]
#define double long double
#define int long long
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int maxn = 4e5+1e2;
const double phi = 1.04719755116;
const double eps = 1e-4;
struct KD{
double d[2],mx[2],mn[2];
double bj,mr;
int l,r;
int num;
};
KD now,t[maxn];
int root,ymh,n,m;
int ans[maxn];
struct Node{
double x,y;
double r;
int num;
};
Node a[maxn];
bool cmp(Node a,Node b)
{
if (a.r==b.r) return a.num<b.num;
return a.r>b.r;
}
bool operator <(KD a,KD b)
{
return a.d[ymh]<b.d[ymh];
}
void up(int root)
{
for (int i=0;i<=1;i++)
{
if (t[root].l) t[root].mn[i]=min(t[root].mn[i],t[t[root].l].mn[i]);
if (t[root].l) t[root].mx[i]=max(t[root].mx[i],t[t[root].l].mx[i]);
if (t[root].l) t[root].mr=max(t[root].mr,t[t[root].l].mr);
if (t[root].r) t[root].mr=max(t[root].mr,t[t[root].r].mr);
if (t[root].r) t[root].mn[i]=min(t[root].mn[i],t[t[root].r].mn[i]);
if (t[root].r) t[root].mx[i]=max(t[root].mx[i],t[t[root].r].mx[i]);
}
}
void build(int &x,int l,int r,int dd)
{
ymh = dd;
int mid = l+r >> 1;
x=mid;
nth_element(t+l,t+mid,t+r+1);
for (int i=0;i<=1;i++) t[x].mn[i]=t[x].mx[i]=t[x].d[i];
t[x].mr=t[x].bj;
if (l<x) build(t[x].l,l,mid-1,dd^1);
if (x<r) build(t[x].r,mid+1,r,dd^1);
up(x);
}
inline double getdis(int x)
{
double ans=0;
for (int i=0;i<=1;i++) ans=ans+(t[x].d[i]-now.d[i])*(t[x].d[i]-now.d[i]);
return ans;
}
inline double calc(int x)
{
double ans=0;
for (int i=0;i<=1;i++)
{
double tmp=0;
if (now.d[i]>=t[x].mn[i] && now.d[i]<=t[x].mx[i]) tmp=0;
else tmp=min((t[x].mn[i]-now.d[i])*(t[x].mn[i]-now.d[i]),(t[x].mx[i]-now.d[i])*(t[x].mx[i]-now.d[i]));
ans=ans+tmp;
}
return ans;
}
void query(int x)
{
//cout<<now.num<<endl;
//cout<<x<<" "<<t[x].l<<" "<<t[x].r<<" "<<t[x].mr<<" "<<t[x].d[1]<<endl;;
if (!x) return;
double d = getdis(x);
double d1 = calc(t[x].l);
double d2 = calc(t[x].r);
//cout<<d1<<" "<<(t[t[x].l].mr+now.bj)<<" "<<d2<<endl;
if (!ans[t[x].num] && (t[x].bj+now.bj)*(t[x].bj+now.bj)-d>=-eps) ans[t[x].num]=now.num;
if ((t[t[x].l].mr+now.bj)*(t[t[x].l].mr+now.bj)-d1>=-eps) query(t[x].l);
if ((t[t[x].r].mr+now.bj)*(t[t[x].r].mr+now.bj)-d2>=-eps) query(t[x].r);
}
signed main()
{
n=read();
for (int i=1;i<=n;i++)
{
double x=read(),y=read(),r=read();
double tmp = x;
x=cos(phi)*x-sin(phi)*y;
y=sin(phi)*tmp+cos(phi)*y;
t[i].num=i;
t[i].d[0]=x;
t[i].d[1]=y;
t[i].bj=r;
a[i].x=x;
a[i].y=y;
a[i].num=i;
a[i].r=r;
//printf("%.2lf %.2lf %.2lf\n",x,y,r);
//cout<<endl;
}
build(root,1,n,0);
//cout<<"********"<<endl;
sort(a+1,a+1+n,cmp);
for (int i=1;i<=n;i++)
{
if (ans[a[i].num]) continue;
now.d[0]=a[i].x;
now.d[1]=a[i].y;
now.bj=a[i].r;
now.num=a[i].num;
query(root);
//out<<"*******"<<endl;
}
for (int i=1;i<=n;i++) cout<<ans[i]<<" ";
return 0;
}
洛谷4631 [APIO2018] Circle selection 选圆圈 (KD树)的更多相关文章
- 【LG4631】[APIO2018]Circle selection 选圆圈
[LG4631][APIO2018]Circle selection 选圆圈 题面 洛谷 题解 用\(kdt\)乱搞剪枝. 维护每个圆在\(x.y\)轴的坐标范围 相当于维护一个矩形的坐标范围为\([ ...
- [APIO2018] Circle selection 选圆圈(假题解)
题面 自己去\(LOJ\)上找 Sol 直接排序然后\(KDTree\)查询 然后发现\(TLE\)了 然后把点旋转一下,就过了.. # include <bits/stdc++.h> # ...
- [Luogu4631][APIO2018] Circle selection 选圆圈
Luogu 题目描述 在平面上,有 \(n\) 个圆,记为 \(c_1, c_2,...,c_n\) .我们尝试对这些圆运行这个算法: \(1\).找到这些圆中半径最大的.如果有多个半径最大的圆,选择 ...
- [APIO2018] Circle selection 选圆圈
Description 给出 \(n\) 个圆 \((x_i,y_i,r_i)\) 每次重复以下步骤: 找出半径最大的圆,并删除与这个圆相交的圆 求出每一个圆是被哪个圆删除的 Solution \(k ...
- luogu P4631 [APIO2018] Circle selection 选圆圈
传送门 那个当前半径最大的圆可以用堆维护.这道题一个想法就是优化找和当前圆有交的圆的过程.考虑对于所有圆心建KD-tree,然后在树上遍历的找这样的点.只要某个点子树内的点构成的矩形区域到当前圆心的最 ...
- LOJ 2586 「APIO2018」选圆圈——KD树
题目:https://loj.ac/problem/2586 只会 19 分的暴力. y 都相等,仍然按直径从大到小做.如果当前圆没有被删除,那么用线段树把 [ x-r , x+r ] 都打上它的标记 ...
- 【洛谷5439】【XR-2】永恒(树链剖分,线段树)
[洛谷5439][XR-2]永恒(树链剖分,线段树) 题面 洛谷 题解 首先两个点的\(LCP\)就是\(Trie\)树上的\(LCA\)的深度. 考虑一对点的贡献,如果这两个点不具有祖先关系,那么这 ...
- 洛谷P4630 [APIO2018] Duathlon 铁人两项 【圆方树】
题目链接 洛谷P4630 题解 看了一下部分分,觉得树的部分很可做,就相当于求一个点对路径长之和的东西,考虑一下能不能转化到一般图来? 一般图要转为树,就使用圆方树呗 思考一下发现,两点之间经过的点双 ...
- [APIO2018]Circle selection
https://www.zybuluo.com/ysner/note/1257597 题面 在平面上,有\(n\)个圆,记为\(c_1,c_2,...,c_n\).我们尝试对这些圆运行这个算法: 找到 ...
随机推荐
- rollup 使用babel7版本的插件rollup-plugin-babel,rollup-plugin-babel使用报错解决办法。
最近在研究rollup,想吐槽下rollup的官方文档写的真的太简单了,而且照着文档一步步来还报错,说明文档年代有点久远啊... 照着文档使用rollup-plugin-babel报错,首先打开rol ...
- java深度复制
索要克隆的类必须实现:Serializable,Cloneable接口import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...
- Win10安装gcc
使用MinGW安装gcc 1.下载MinGW,地址 https://sourceforge.net/projects/mingw/files/ ,选择Download mingw-get-setup. ...
- PXC 5.7.14 安装部署
http://www.dbhelp.net/2017/01/06/pxc-5-7-14-%E5%AE%89%E8%A3%85%E9%83%A8%E7%BD%B2-pxc-install.html PX ...
- Lambda@edge 实现负载均衡器功能
一般的业务实现流程为CDN->ELB->EC2,但OTT业务往往会产生很高的流量费用,如果使用常规的架构,流量费用会成倍增加,为了降低费用,我们对架构做了一些优化. AWS Cloudfr ...
- Kubernetes的安装部署
前言:简述kubernetes(k8s)集群 k8s集群基本功能组件由master和node组成. master节点上主要有kube-apiserver.kube-scheduler.kube-con ...
- 源码解析.Net中Host主机的构建过程
前言 本篇文章着重讲一下在.Net中Host主机的构建过程,依旧延续之前文章的思路,着重讲解其源码,如果有不知道有哪些用法的同学可以点击这里,废话不多说,咱们直接进入正题 Host构建过程 下图是我自 ...
- openswan协商流程之(七):main_inR3
主模式第六包(收包):main_inR3 1. 序言 main_inR3()函数是ISAKMP协商过程中第一阶段的最后一个报文的接收处理函数,它的作用同main_inI3_outR3()部分功能相同: ...
- Identity角色管理五(添加用户到角色组)
因需要在用户列表中点详情按钮来到当前页,所以需要展示分组详情,并展示当前所属角色组的用户 public async Task<ActionResult> Details(string id ...
- .net Core 基于EF Core 实现数据库上下文
在做项目时,需要将某一些功能的实体建立在另一个数据库中,连接不同的数据库用以存储记录.通过查找资料,实现EF Core上下文. 下面是实现上下文后的解决方案的目录: 1.UpAndDownDbCont ...