[BZOJ2716]天使玩偶

题目大意:

一个平面直角坐标系,坐标\(1\le x,y\le10^6\)。\(n(n\le10^6)\)次操作,操作包含以下两种:

  1. 新增一个点\((x,y)\);
  2. 询问离\((x,y)\)最近的点的距离。

思路:

分别统计左下、左上、右上、右下的最近的点,每次使用CDQ分治。树状数组维护最小值。

有一个一样的题是SJY摆棋子,不过CDQ会被卡,需要用KD树才能过。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e6+1,M=5e5;
struct Node {
int t,x,y,type;
};
Node a[N];
int ans[M],lim;
inline bool cmp1(const Node &p1,const Node &p2) {
if(p1.t==p2.t) {
if(p1.type==p2.type) {
if(p1.x==p2.x) {
return p1.y<p2.y;
}
return p1.x<p2.x;
}
return p1.type>p2.type;
}
return p1.t<p2.t;
}
inline bool cmp2(const Node &p1,const Node &p2) {
if(p1.x==p2.x) {
return p1.y<p2.y;
}
return p1.x<p2.x;
}
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void reset() {
std::fill(&val[1],&val[N],INT_MAX);
}
void modify(int p,const int &x) {
for(;p<N;p+=lowbit(p)) val[p]=std::min(val[p],x);
}
int query(int p) const {
int ret=INT_MAX;
for(;p;p-=lowbit(p)) ret=std::min(ret,val[p]);
return ret;
}
void clear(int p) {
for(;p<N;p+=lowbit(p)) val[p]=INT_MAX;
}
};
FenwickTree t;
void cdq(const int &b,const int &e) {
if(b==e) return;
const int mid=(b+e)>>1;
cdq(b,mid);
cdq(mid+1,e);
int p=b,q=mid+1;
for(;q<=e;q++) {
if(a[q].type==1) continue;
for(;p<=mid&&a[p].x<=a[q].x;p++) {
if(a[p].type==1) t.modify(a[p].y,N-a[p].y-a[p].x);
}
const int tmp=t.query(a[q].y);
if(tmp==INT_MAX) continue;
ans[a[q].t]=std::min(ans[a[q].t],tmp-(N-a[q].y-a[q].x));
}
while(--p>=b) {
if(a[p].type==1) t.clear(a[p].y);
}
std::inplace_merge(&a[b],&a[mid]+1,&a[e]+1,cmp2);
}
int main() {
const int n=getint(),m=getint();
int cnt=-1;
for(register int i=1;i<=n;i++) {
a[i].type=1;
a[i].t=cnt;
a[i].x=getint();
a[i].y=getint();
}
for(register int i=1;i<=m;i++) {
a[n+i].type=getint();
if(a[n+i].type==2) cnt++;
a[n+i].t=cnt;
a[n+i].x=getint();
a[n+i].y=getint();
ans[cnt]=INT_MAX;
}
t.reset();
std::sort(&a[1],&a[n+m]+1,cmp1);
cdq(1,n+m);
for(register int i=1;i<=n+m;i++) {
a[i].x=N-a[i].x;
}
std::sort(&a[1],&a[n+m]+1,cmp1);
cdq(1,n+m);
for(register int i=1;i<=n+m;i++) {
a[i].y=N-a[i].y;
}
std::sort(&a[1],&a[n+m]+1,cmp1);
cdq(1,n+m);
for(register int i=1;i<=n+m;i++) {
a[i].x=N-a[i].x;
}
std::sort(&a[1],&a[n+m]+1,cmp1);
cdq(1,n+m);
for(register int i=0;i<=cnt;i++) {
printf("%d\n",ans[i]);
}
return 0;
}

[BZOJ2716]天使玩偶的更多相关文章

  1. BZOJ2716天使玩偶

    不会KD-tree怎么办?CQD硬搞. 建立正常的平面直角坐标系,首先我们只考虑在目标点左下角的点对目标点的贡献,由于左下点的横纵坐标都小于目标点,那么曼哈顿距离就可以化简了,绝对值去掉后,得到$x2 ...

  2. [BZOJ2716] [Violet 3]天使玩偶(CDQ分治)

    [BZOJ2716] [Violet 3]天使玩偶(CDQ分治) 题面 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里, ...

  3. bzoj2648SJY摆棋子&&bzoj2716[Violet 3]天使玩偶*

    bzoj2648SJY摆棋子 bzoj2716[Violet 3]天使玩偶 题意: 棋盘上有n个棋子,现在有m个操作,一种是加棋子,一种是查询离某个点最近的棋子.n,m≤500000. 题解: 先将已 ...

  4. bzoj2716/2648 / P4169 [Violet]天使玩偶/SJY摆棋子

    P4169 [Violet]天使玩偶/SJY摆棋子 k-d tree 模板 找了好几天才发现输出优化错了....真是zz...... 当子树非常不平衡时,就用替罪羊树的思想,拍扁重建. luogu有个 ...

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

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

  6. BZOJ 2716: [Violet 3]天使玩偶

    2716: [Violet 3]天使玩偶 Time Limit: 80 Sec  Memory Limit: 128 MBSubmit: 1473  Solved: 621[Submit][Statu ...

  7. 【BZOJ-2648&2716】SJY摆棋子&天使玩偶 KD Tree

    2648: SJY摆棋子 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2459  Solved: 834[Submit][Status][Discu ...

  8. BZOJ 2716: [Violet 3]天使玩偶( CDQ分治 + 树状数组 )

    先cdq分治, 然后要处理点对答案的贡献, 可以以询问点为中心分成4个区域, 然后去掉绝对值(4种情况讨论), 用BIT维护就行了. --------------------------------- ...

  9. CH4701 天使玩偶

    题意 4701 天使玩偶 0x40「数据结构进阶」例题 描述 题目PDF 样例输入 2 3 1 1 2 3 2 1 2 1 3 3 2 4 2 样例输出 1 2 来源 石家庄二中Violet 3杯省选 ...

随机推荐

  1. macOS 安装 pcl 1.8.0

    Mac 上的 pcl 一直有问题. 找不到 pcl_viewer 查看 pcd 文件.写个程序用 pcl::visualization::CloudViewer 查看点云,遇到 Runtime Exc ...

  2. 高级C#信使(译) - Unity维基百科

    高级C#信使 作者:Ilya Suzdalnitski 译自:http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger 描述 前言 Mis ...

  3. sleep命令

    sleep支持睡眠(分,小时) sleep 1 睡眠1秒 sleep 1s 睡眠1秒 sleep 1m 睡眠1分 sleep 1h 睡眠1小时

  4. Linux中断处理驱动程序编写【转】

    转自:http://blog.163.com/baosongliang@126/blog/static/1949357020132585316912/ 本章节我们一起来探讨一下Linux中的中断 中断 ...

  5. nginx配置location总结及rewrite规则写法【转】

    转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...

  6. python使用twisted搭建的一个socket服务

    服务端 # -*- coding: utf-8 -*- # @Time : 2018/9/19 21:41 # @Author : cxa # @File : tsTservTW.py # @Soft ...

  7. .net开源框架开源类库(整理)

    源:http://www.cnblogs.com/chinanetwind/p/3715809.html 常用库 Json.NET https://github.com/JamesNK/Newtons ...

  8. idea中使用tomcat 方式启动spring boot项目

    Spring boot 的main 入口启动方式相信都会用,直接运行main直接就启动了,但是往往这种方式并不是最佳的启动方式,比如运维的层面更希望调整tomcat的调优参数,而只使用嵌入启动方式很难 ...

  9. Python操作Excle

    python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库.可从这里下载https://pypi.python.org/pypi.下面分别记录p ...

  10. mariadb/mysql使用Navicat连接报错

    [问题1] 使用Navicat连接服务器的mariadb/mysql时报错 access denied for user root@192.168.xx.xx(using password:yes) ...