Time Limit: 20 Sec  Memory Limit: 128 MB

Submit: 3128  Solved: 1067

Description

这天,SJY显得无聊。在家自己玩。在一个棋盘上,有N个黑色棋子。他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋子。此处的距离是 曼哈顿距离 即(|x1-x2|+|y1-y2|) 。现在给出N<=500000个初始棋子。和M<=500000个操作。对于每个白色棋子,输出距离这个白色棋子最近的黑色棋子的距离。同一个格子可能有多个棋子。
 

Input

第一行两个数 N M
以后M行,每行3个数 t x y
如果t=1 那么放下一个黑色棋子
如果t=2 那么放下一个白色棋子

Output

对于每个T=2 输出一个最小距离
 

Sample Input

2 3
1 1
2 3
2 1 2
1 3 3
2 4 2

Sample Output

1
2

HINT

kdtree可以过

Source

K-Dtree模板题

似乎有种懂了的错觉

(錯覚?それでは、聞いてください....)

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
int nowD;
struct node{
int min[],max[];
int d[];
int l,r;
}t[mxn<<];
int root,nct=;
int cmp(const node a,const node b){
if(a.d[nowD]==b.d[nowD])return a.d[nowD^]<b.d[nowD^];
return a.d[nowD]<b.d[nowD];
}
int qx,qy;//查询参数
void pushup(int rt,int c){//更新结点信息
t[rt].max[]=max(t[rt].max[],t[c].max[]);
t[rt].min[]=min(t[rt].min[],t[c].min[]);
t[rt].max[]=max(t[rt].max[],t[c].max[]);
t[rt].min[]=min(t[rt].min[],t[c].min[]);
return;
}
int Build(int l,int r,int D){
nowD=D;int mid=(l+r)>>;
nth_element(t+l,t+mid,t+r+,cmp);
t[mid].max[]=t[mid].min[]=t[mid].d[];
t[mid].max[]=t[mid].min[]=t[mid].d[];
if(l!=mid) t[mid].l=Build(l,mid-,D^);
if(r!=mid) t[mid].r=Build(mid+,r,D^);
if(t[mid].l)pushup(mid,t[mid].l);
if(t[mid].r)pushup(mid,t[mid].r);
return mid;
}
void insert(int x){//插入新结点
int now=root,D=;
while(){
pushup(now,x);
if(t[x].d[D]>=t[now].d[D]){
if(!t[now].r){t[now].r=x;return;}
else now=t[now].r;
}
else{
if(!t[now].l){t[now].l=x;return;}
else now=t[now].l;
}
D^=;
}
return;
}
int Dist(int k){//估价
int res=;
if(qx<t[k].min[])res+=t[k].min[]-qx;
if(qx>t[k].max[])res+=qx-t[k].max[];
if(qy<t[k].min[])res+=t[k].min[]-qy;
if(qy>t[k].max[])res+=qy-t[k].max[];
return res;
}
int ans;
void query(int rt){
int dx,dy,tmp;
// printf("rt:%d pos:%d %d ans:%d\n",rt,t[rt].d[0],t[rt].d[1],ans);
tmp=abs(t[rt].d[]-qx)+abs(t[rt].d[]-qy);
if(tmp<ans)ans=tmp;
if(t[rt].l) dx=Dist(t[rt].l);else dx=INF;
if(t[rt].r) dy=Dist(t[rt].r);else dy=INF;
if(dx<dy){
if(dx<ans) query(t[rt].l);
if(dy<ans) query(t[rt].r);
}
else{
if(dy<ans) query(t[rt].r);
if(dx<ans) query(t[rt].l);
}
return;
}
int n,m;
int main(){
int i,j;
n=read();m=read();
int T,x,y;
for(i=;i<=n;i++){
t[++nct].d[]=read();
t[nct].d[]=read();
}
root=Build(,n,);
while(m--){
T=read();x=read();y=read();
if(T==){
t[++nct].d[]=x; t[nct].d[]=y;
t[nct].max[]=t[nct].min[]=t[nct].d[];
t[nct].max[]=t[nct].min[]=t[nct].d[];
insert(nct);
}
else{
qx=x;qy=y;ans=INF;
query(root);
printf("%d\n",ans);
}
}
return ;
}

Bzoj2648 SJY摆棋子的更多相关文章

  1. BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶

    BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ...

  2. [BZOJ2648] SJY摆棋子 kd-tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 5421  Solved: 1910[Submit][Status][Disc ...

  3. BZOJ2648 SJY摆棋子(KD-Tree)

    板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  4. 【kd-tree】bzoj2648 SJY摆棋子

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...

  5. 2019.01.14 bzoj2648: SJY摆棋子(kd-tree)

    传送门 kd−treekd-treekd−tree模板题. 题意简述:支持在平面上插入一个点,求对于一个点的最近点对. 思路:cdqcdqcdq是一种很不错的分治方法 只是好像码量有点窒息 所以我用了 ...

  6. KDTree(Bzoj2648: SJY摆棋子)

    题面 传送门 KDTree 大概就是一个分割\(k\)维空间的数据结构,二叉树 建立:每层选取一维为关键字,把中间的点拿出来,递归左右,有个\(STL\)函数nth_element可以用一下 维护:维 ...

  7. [bzoj2648]SJY摆棋子(带插入kd-tree)

    解题关键:带插入kdtree模板题. #include<iostream> #include<cstdio> #include<cstring> #include& ...

  8. luogu4169 [Violet]天使玩偶/SJY摆棋子 / bzoj2648 SJY摆棋子 k-d tree

    k-d tree + 重构的思想,就能卡过luogu和bzoj啦orz #include <algorithm> #include <iostream> #include &l ...

  9. 【BZOJ2648】SJY摆棋子(KD-Tree)

    [BZOJ2648]SJY摆棋子(KD-Tree) 题面 BZOJ Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一 ...

随机推荐

  1. Display HTML in WPF and CefSharp

    https://www.codeproject.com/articles/881315/display-html-in-wpf-and-cefsharp-tutorial-part Download ...

  2. 关于Microsoft Visual Studio 2010系统自带的数据库

    转自:http://blog.sina.com.cn/s/blog_a570cca601012x5w.html 1.Visual studio Tools>命令提示 2.aspnet_regsq ...

  3. Android的媒体管理框架:Glide

    Glide是一个高效.开源. Android设备上的媒体管理框架,它遵循BSD.MIT以及Apache 2.0协议发布.Glide具有获取.解码和展示视频剧照.图片.动画等功能,它还有灵活的API,这 ...

  4. vbs http

    get请求 模拟发送http请求到百度Dim httpSet http = CreateObject("Msxml2.ServerXMLHTTP")http.open " ...

  5. JavaScript的一些知识碎片(2)-反射-全局变量-回调

    JavaScript中的反射:编程语言中的反射原理都一样,就是通过操作metadata(描述语言的语言)来完成一些不具备反射功能的语言很难实现的功能.在静态语言中,反射是一个高大上的东西,比如在运行时 ...

  6. 转一篇关于Unity的PlayMaker

    这篇文章转自http://va.lent.in/should-you-use-playmaker-in-production/ 此文作者大概深受其苦,吐槽了playmaker的多个蛋疼的地方,这其实说 ...

  7. [BZOJ1299]巧克力棒(博弈论)

    题目:http://hzwer.com/1976.html 分析:先Orz hzwer 对于盒子外面的巧克力棒,就是Nim游戏. 所以就很容易想到先手第一步最好从盒子中取出m根巧克力棒,使得这些巧克力 ...

  8. [POJ2104]K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 34048   Accepted: 10810 Ca ...

  9. morris的用法

    參數選項說明: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...

  10. java 计算地球上两点间距离

    /** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...