2648: SJY摆棋子

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 5421  Solved: 1910
[Submit][Status][Discuss]

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

鸣谢 孙嘉裕

kd-tree裸题

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define maxn 1000001
using namespace std;
struct data {
int mn[],mx[],l,r,d[];
data() {mn[]=mn[]=mx[]=mx[]=l=r=d[]=d[]=;}
}t[maxn*];
int n,m;
int nowd;
bool cmp(data t1,data t2) {return t1.d[nowd]==t2.d[nowd]?t1.d[!nowd]<t2.d[!nowd]:t1.d[nowd]<t2.d[nowd];}
void update(int x) {
if(t[x].l) {
t[x].mx[]=max(t[x].mx[],t[t[x].l].mx[]);
t[x].mn[]=min(t[x].mn[],t[t[x].l].mn[]);
t[x].mx[]=max(t[x].mx[],t[t[x].l].mx[]);
t[x].mn[]=min(t[x].mn[],t[t[x].l].mn[]);
}
if(t[x].r) {
t[x].mx[]=max(t[x].mx[],t[t[x].r].mx[]);
t[x].mn[]=min(t[x].mn[],t[t[x].r].mn[]);
t[x].mx[]=max(t[x].mx[],t[t[x].r].mx[]);
t[x].mn[]=min(t[x].mn[],t[t[x].r].mn[]);
}
}
int build(int l,int r,int D) {
int mid=l+r>>;
nowd=D;
nth_element(t+l+,t+mid+,t+r+,cmp);
if(l!=mid) t[mid].l=build(l,mid-,!D);
if(r!=mid) t[mid].r=build(mid+,r,!D);
t[mid].mx[]=t[mid].mn[]=t[mid].d[];
t[mid].mx[]=t[mid].mn[]=t[mid].d[];
update(mid);
return mid;
}
void insert(int &now,int D,data x) {
if(!now) {now=++n;t[now]=x;t[now].mn[]=t[now].mx[]=x.d[];t[now].mx[]=t[now].mn[]=x.d[];return;}
if(x.d[D]>t[now].d[D]) insert(t[now].r,!D,x);
else insert(t[now].l,!D,x);
update(now);
}
int dis(int now,data x) {
int re=;
if(x.d[]<t[now].mn[]) re+=t[now].mn[]-x.d[];
if(x.d[]>t[now].mx[]) re+=x.d[]-t[now].mx[];
if(x.d[]<t[now].mn[]) re+=t[now].mn[]-x.d[];
if(x.d[]>t[now].mx[]) re+=x.d[]-t[now].mx[];
return re;
}
int ans;
void query(int now,data x) {
ans=min(ans,abs(t[now].d[]-x.d[])+abs(t[now].d[]-x.d[]));
int dl,dr;
if(t[now].l) dl=dis(t[now].l,x); else dl=;
if(t[now].r) dr=dis(t[now].r,x); else dr=;
if(dl<dr) {
if(dl<ans) query(t[now].l,x);
if(dr<ans) query(t[now].r,x);
}
else {
if(dr<ans) query(t[now].r,x);
if(dl<ans) query(t[now].l,x);
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d%d",&t[i].d[],&t[i].d[]);
int mid=build(,n,);
for(int i=;i<=m;i++) {
int tp;data x;
scanf("%d%d%d",&tp,&x.d[],&x.d[]);
if(tp==) insert(mid,,x);
else {ans=;query(mid,x);printf("%d\n",ans);}
}
}

[BZOJ2648] SJY摆棋子 kd-tree的更多相关文章

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

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

  2. BZOJ 2648: SJY摆棋子(K-D Tree)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 6051  Solved: 2113[Submit][Status][Discuss] Descript ...

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

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

  4. Bzoj2648 SJY摆棋子

    Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 3128  Solved: 1067 Description 这天,SJY显得无聊.在家自己玩.在一个 ...

  5. bzoj 2648 SJY摆棋子 kd树

    题目链接 初始的时候有一些棋子, 然后给两种操作, 一种是往上面放棋子. 另一种是给出一个棋子的位置, 问你离它最近的棋子的曼哈顿距离是多少. 写了指针版本的kd树, 感觉这个版本很好理解. #inc ...

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

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

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

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

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

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

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

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

随机推荐

  1. 找jar包的网址

    http://search.maven.org/#search%7Cga%7C1%7Cmybatis http://mvnrepository.com/

  2. [转]dojo/mouse

    dojo/mouse Authors:Kris Zyp Project owner:Kris Zyp since:1.7.0 Contents Usage enter leave mouseButto ...

  3. 关于网站转码(SiteApp转码)

    1.Siteapp页面转码的意义?在百度移动搜索引擎中为更好满足用户信息需求,会同时为用户提供pc网页和mobile网页,但目前大多数PC页在移动终端中直接浏览的体验较差(交互.兼容和流量等).因此为 ...

  4. hdu 1598 find the most comfortable road (并查集)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. P1404 平均数

    题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 输入输出格式 输入格式: N+1行, 第一行两个整数n和m 接下来n行,每行一个整数a[ ...

  6. BZOJ4602: [Sdoi2016]齿轮 DFS 逆元

    这道题就是一个DFS,有一篇奶牛题几乎一样.但是这道题卡精度. 这道题网上的另一篇题解是有问题的.取对数这种方法可以被轻松卡.比如1e18 与 (1e9-1)*(1e9+1)取对数根本无法保证不被卡精 ...

  7. [BZOJ3473][BZOJ3277]字符串

    [BZOJ3473][BZOJ3277]字符串 试题描述 给定 \(n\) 个字符串,询问每个字符串有多少子串(不包括空串)是所有 \(n\) 个字符串中至少 \(k\) 个字符串的子串? 输入 第一 ...

  8. dva的基本用法

    dva是一个状态管理工具,整合了redux,redux-saga,react-router,fetch等框架,目前只能用于react的状态管理 1. dva的models dva的主要作用还是整合了r ...

  9. CentOS 7, 升级python到3.x

    By francis_hao    Apr 11,2017 使用源码安装方式 首先到官网https://www.python.org/downloads/source/ 下载python最新版本.当前 ...

  10. Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XM

    本来就是把fragment写死在activity的xml模板里面,结果报了这个错误, Unable to start activity ComponentInfo{com.example.admini ...