SJY摆棋子&&[Violet 3]天使玩偶
SJY摆棋子
https://www.lydsy.com/JudgeOnline/problem.php?id=2648
[Violet 3]天使玩偶
https://www.lydsy.com/JudgeOnline/problem.php?id=2716
参考博客:https://blog.csdn.net/Littlewhite520/article/details/78284697
KDtree模板题,带插入
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdio>
#include<cmath>
#define MAXN 1000005
#define INF 0x3f3f3f3f
using namespace std; int id;
int L,R,ans,root;
int n,m;
struct sair{
int Max[],Min[],p[],l,r;
bool operator<(const sair &b)const{
if(p[id]==b.p[id]) return p[id^]<b.p[id^];
return p[id]<b.p[id];
}
}tree[MAXN]; int dist(int now){
int dis=;;
if(L<tree[now].Min[]) dis+=tree[now].Min[]-L;
if(L>tree[now].Max[]) dis+=L-tree[now].Max[];
if(R<tree[now].Min[]) dis+=tree[now].Min[]-R;
if(R>tree[now].Max[]) dis+=R-tree[now].Max[];
return dis;
} void pushup(int now){
if(tree[now].l){
if(tree[tree[now].l].Max[]>tree[now].Max[]) tree[now].Max[]=tree[tree[now].l].Max[];
if(tree[tree[now].l].Min[]<tree[now].Min[]) tree[now].Min[]=tree[tree[now].l].Min[];
if(tree[tree[now].l].Max[]>tree[now].Max[]) tree[now].Max[]=tree[tree[now].l].Max[];
if(tree[tree[now].l].Min[]<tree[now].Min[]) tree[now].Min[]=tree[tree[now].l].Min[];
}
if(tree[now].r){
if(tree[tree[now].r].Max[]>tree[now].Max[]) tree[now].Max[]=tree[tree[now].r].Max[];
if(tree[tree[now].r].Min[]<tree[now].Min[]) tree[now].Min[]=tree[tree[now].r].Min[];
if(tree[tree[now].r].Max[]>tree[now].Max[]) tree[now].Max[]=tree[tree[now].r].Max[];
if(tree[tree[now].r].Min[]<tree[now].Min[]) tree[now].Min[]=tree[tree[now].r].Min[];
}
} int build(int l,int r,int D){
int mid=(l+r)>>;
id=D;
nth_element(tree+l,tree+mid,tree+r+);
if(l!=mid) tree[mid].l=build(l,mid-,D^);
if(r!=mid) tree[mid].r=build(mid+,r,D^);
tree[mid].Max[]=tree[mid].Min[]=tree[mid].p[];
tree[mid].Max[]=tree[mid].Min[]=tree[mid].p[];
pushup(mid);
return mid;
} void Insert(int rt){
int now=root,D=;//now从头开始往下查询
while(){
///比较新加进来的点,更新最大最小值
if(tree[rt].Max[]>tree[now].Max[]) tree[now].Max[]=tree[rt].Max[];
if(tree[rt].Max[]>tree[now].Max[]) tree[now].Max[]=tree[rt].Max[];
if(tree[rt].Min[]<tree[now].Min[]) tree[now].Min[]=tree[rt].Min[];
if(tree[rt].Min[]<tree[now].Min[]) tree[now].Min[]=tree[rt].Min[]; if(tree[rt].p[D]>=tree[now].p[D]){
if(!tree[now].r){
tree[now].r=rt;
return;
}
now=tree[now].r;
}
else{
if(!tree[now].l){
tree[now].l=rt;
return;
}
now=tree[now].l;
}
D^=;
}
} void query(int now){
int dl,dr,dis;
dis=abs(tree[now].p[]-L)+abs(tree[now].p[]-R);
if(dis<ans) ans=dis;
if(tree[now].l) dl=dist(tree[now].l);
else dl=INF;
if(tree[now].r) dr=dist(tree[now].r);
else dr=INF;
if(dl<dr){
if(dl<ans) query(tree[now].l);
if(dr<ans) query(tree[now].r);
}
else{
if(dr<ans) query(tree[now].r);
if(dl<ans) query(tree[now].l);
}
} int main(){
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d %d",&tree[i].p[],&tree[i].p[]);
}
root=build(,n,);
int x,y,z;
for(int i=;i<=m;i++){
scanf("%d %d %d",&x,&y,&z);
if(x==){
n++;
tree[n].Max[]=tree[n].Min[]=tree[n].p[]=y;
tree[n].Max[]=tree[n].Min[]=tree[n].p[]=z;
Insert(n);
}
else{
ans=INF;
L=y,R=z;
query(root);
printf("%d\n",ans);
}
}
}
/*
2 3
1 1
2 3
2 1 2
1 3 3
2 4 2
*/
SJY摆棋子&&[Violet 3]天使玩偶的更多相关文章
- BZOJ2648/2716:SJY摆棋子/[Violet]天使玩偶(K-D Tree)
Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子. ...
- bzoj2716/2648 / P4169 [Violet]天使玩偶/SJY摆棋子
P4169 [Violet]天使玩偶/SJY摆棋子 k-d tree 模板 找了好几天才发现输出优化错了....真是zz...... 当子树非常不平衡时,就用替罪羊树的思想,拍扁重建. luogu有个 ...
- bzoj 2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 --kdtree
2648: SJY摆棋子&&2716: [Violet 3]天使玩偶 Time Limit: 20 Sec Memory Limit: 128 MB Description 这天,S ...
- 【LG4169】[Violet]天使玩偶/SJY摆棋子
[LG4169][Violet]天使玩偶/SJY摆棋子 题面 洛谷 题解 至于\(cdq\)分治的解法,以前写过 \(kdTree\)的解法好像还\(sb\)一些 就是记一下子树的横.纵坐标最值然后求 ...
- 洛谷 P4169 [Violet]天使玩偶/SJY摆棋子 解题报告
P4169 [Violet]天使玩偶/SJY摆棋子 题目描述 \(Ayu\)在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,\(Ayu\) 却忘了她把天使玩偶埋在了哪 ...
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...
- luoguP4169 [Violet]天使玩偶/SJY摆棋子 K-Dtree
P4169 [Violet]天使玩偶/SJY摆棋子 链接 luogu 思路 luogu以前用CDQ一直过不去. bzoj还是卡时过去的. 今天终于用k-dtree给过了. 代码 #include &l ...
- 洛谷P4169 [Violet]天使玩偶/SJY摆棋子(CDQ分治)
[Violet]天使玩偶/SJY摆棋子 题目传送门 解题思路 用CDQ分治开了氧气跑过. 将输入给的顺序作为第一维的时间,x为第二维,y为第三维.对于距离一个询问(ax,ay),将询问分为四块,左上, ...
- [Violet]天使玩偶/SJY摆棋子 [cdq分治]
P4169 [Violet]天使玩偶/SJY摆棋子 求离 \((x,y)\) 最近点的距离 距离的定义是 \(|x1-x2|+|y1-y2|\) 直接cdq 4次 考虑左上右上左下右下就可以了-略微卡 ...
随机推荐
- java ssl https 连接详解 生成证书 tomcat keystone
java ssl https 连接详解 生成证书 我们先来了解一下什么理HTTPS 1. HTTPS概念 1)简介 HTTPS(全称:Hypertext Transfer Protocol over ...
- 查找占用CPU高线程
1.根据进程号查看线程 ps -mp pid -o THREAD,tid,time 2 把tid值转成16进制 printf "%x\n" tid 3.根据上面获取到的16进制数据 ...
- 详解UE4静态库与动态库的导入与使用
转自:http://blog.csdn.net/u012999985/article/details/71554628 一.基本内容概述 最近做项目时经常看到build.cs文件,就想研究一下UE ...
- 图片尺寸批量resize的matlab并行代码
在caffe ImageNet例子中有对图片进行resize的部分,文中使用的是linux shell脚本命令: for name in /path/to/imagenet/val/*.JPEG; d ...
- HTML|CSS之前端入门
知识内容: 1.计算机网络综述 2.web基础 3.HTML与CSS介绍 4.JavaScript与jQuery介绍 一.计算机网络综述 1.什么是计算机网络 计算机网络是指将地理位置不同.具有独立功 ...
- linux操作系统5 shell编程
知识内容: 1.shell编程预备知识 2.shell变量 3.表达式与运算符 4.分支循环语句 5.函数 一.shell编程预备知识 1.什么是shell编程 shell是与linux交互的基本工具 ...
- sklearn的estimator
estimator的工作流程 在sklearn中,估计器(estimator)是一个重要的角色,分类器和回归器都属于estimator.在估计器中有有两个重要的方法是fit和transform. fi ...
- jmeter-noguimodel
jmeter -Dthreads= -n -t ~/Desktop/image-controller.jmx -l myimage/out -e -o myimage/log -j myimage/r ...
- JAVA servlet 上传文件(commons-fileupload, commons-io)
<1>获取二进制文件流并输出 InputStream inputStream = request.getInputStream(); BufferedReader reader = new ...
- HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...