[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. ORB_SLAM2 源码阅读 ORB_SLAM2::ORBextractor

    整体架构 构造函数进行初始化,传入设定几个重要的成员变量.nfeatures(特征点的个数).nlevels(构造金字塔的层数).scaleFactor(金字塔中相邻层图像的比例系数).iniThFA ...

  2. sql_injection之post注入

    1.代码篇 </html> <center> <form action="#" method="post"> 姓名:< ...

  3. Once you eliminate all the other factors,the only thing remaining must be the truth.

    Once you eliminate all the other factors,the only thing remaining must be the truth. 一旦你排除了杂因,剩下的一定是 ...

  4. Visual Studio 2017 for Mac

    Visual Studio 2017 for Mac Last Update: 2017/6/16 我们非常荣幸地宣布 Visual Studio 2017 for Mac 现已推出. Visual ...

  5. 洛谷P2341受欢迎的牛

    传送门啦 这是一个tarjan强连通分量与出度结合的例题. 先明确一下题意,如果这个点(缩点之后的)没有出度,这个点才能成为明星牛(明星牛的定义是:所有牛都喜欢他才可以). 由于我们进行了缩点,所以我 ...

  6. Jdk 和 Tomcat的 安装。

    1.再分发服务器上下载JDK,然后利用xftp上传到聚石塔等 2. 解压: tar -zxvf jdk-8u121-linux-x64.tar.gz 3.配置环境变量: export JAVA_HOM ...

  7. Unix IPC之FIFO

    #include "unpipc.h" #define FIFO1 "/tmp/fifo.1" #define FIFO2 "/tmp/fifo.2& ...

  8. C# 图片和二进制之间的转换

    1> 图片转二进制  public byte[] GetPictureData(string imagepath){/**/////根据图片文件的路径使用文件流打开,并保存为byte[] Fil ...

  9. 个人理解的Windows漏洞利用技术发展史

    大概四.五年前,看过陈皓的酷壳上面的一篇文章,上面有一句话我一直记得,是关于学习技术的心得和态度的. 要了解技术就一定需要了解整个计算机的技术历史发展和进化路线.因为,你要朝着球运动的轨迹去,而不是朝 ...

  10. mysql热数据加载管理

    5.6版本之后,提供了一个新特性来快速预热buffer_pool缓冲池.在my.cnf里面加入几个参数: innodb_buffer_pool_dump_at_shutdown = 1   --在关闭 ...