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. 搭建springmvc项目没扫描到mapper和service

    严重: Servlet.service() for servlet [spring] in context with path [/springmvc-demo] threw exceptionorg ...

  2. [C/C++] “箭头(->)”和“点号(.)”的区别

    转自:http://blog.csdn.net/gyymen/article/details/4962873 首先介绍一下C++中的结构.对于一个结构: struct MyStruct { int m ...

  3. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \ ...

  4. Codeforces数据结构(水题)小结

    最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...

  5. 【题解】CQOI2015选数

    这题做的时候接连想错了好多次……但是回到正轨上之后依然是一个套路题.(不过这题好像有比莫比乌斯反演更好的做法,莫比乌斯反演貌似是某种能过的暴力ヽ(´ー`)┌)不过能过也就行了吧哈哈. 首先我们把数字的 ...

  6. BZOJ1876 [SDOI2009]SuperGCD 【高精 + GCD优化】

    题目 Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比 赛计算GCD.有一天Sheng bill很嚣张地找到了你,并要求和你比 赛,但 ...

  7. #define、const、typedef的区别

    #define.const.typedef的区别 #define 并不是定义变量, 只是用来做文本替换 例如: #define PI 3.1415926 float angel; angel=30*P ...

  8. Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)

    D. Game with modulo 题目链接:https://codeforces.com/contest/1104/problem/D 题意: 这题是一个交互题,首先一开始会有一个数a,你最终的 ...

  9. java的哈希遍历 hashmap

    Map<String,String> map = new HashMap<String, String>(); map.put("title"," ...

  10. Different Integers 牛客多校第一场只会签到题

    Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, r ...