[BZOJ2716]天使玩偶
[BZOJ2716]天使玩偶
题目大意:
一个平面直角坐标系,坐标\(1\le x,y\le10^6\)。\(n(n\le10^6)\)次操作,操作包含以下两种:
- 新增一个点\((x,y)\);
- 询问离\((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]天使玩偶的更多相关文章
- BZOJ2716天使玩偶
		不会KD-tree怎么办?CQD硬搞. 建立正常的平面直角坐标系,首先我们只考虑在目标点左下角的点对目标点的贡献,由于左下点的横纵坐标都小于目标点,那么曼哈顿距离就可以化简了,绝对值去掉后,得到$x2 ... 
- [BZOJ2716] [Violet 3]天使玩偶(CDQ分治)
		[BZOJ2716] [Violet 3]天使玩偶(CDQ分治) 题面 Ayu 在七年前曾经收到过一个天使玩偶,当时她把它当作时间囊埋在了地下.而七年后 的今天,Ayu 却忘了她把天使玩偶埋在了哪里, ... 
- bzoj2648SJY摆棋子&&bzoj2716[Violet 3]天使玩偶*
		bzoj2648SJY摆棋子 bzoj2716[Violet 3]天使玩偶 题意: 棋盘上有n个棋子,现在有m个操作,一种是加棋子,一种是查询离某个点最近的棋子.n,m≤500000. 题解: 先将已 ... 
- bzoj2716/2648 / P4169 [Violet]天使玩偶/SJY摆棋子
		P4169 [Violet]天使玩偶/SJY摆棋子 k-d tree 模板 找了好几天才发现输出优化错了....真是zz...... 当子树非常不平衡时,就用替罪羊树的思想,拍扁重建. luogu有个 ... 
- BZOJ2648: SJY摆棋子&&2716: [Violet 3]天使玩偶
		BZOJ2648: SJY摆棋子 BZOJ2716: [Violet 3]天使玩偶 BZOJ氪金无极限... 其实这两道是同一题. 附上2648的题面: Description 这天,SJY显得无聊. ... 
- BZOJ 2716: [Violet 3]天使玩偶
		2716: [Violet 3]天使玩偶 Time Limit: 80 Sec Memory Limit: 128 MBSubmit: 1473 Solved: 621[Submit][Statu ... 
- 【BZOJ-2648&2716】SJY摆棋子&天使玩偶     KD Tree
		2648: SJY摆棋子 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2459 Solved: 834[Submit][Status][Discu ... 
- BZOJ 2716: [Violet 3]天使玩偶( CDQ分治 + 树状数组 )
		先cdq分治, 然后要处理点对答案的贡献, 可以以询问点为中心分成4个区域, 然后去掉绝对值(4种情况讨论), 用BIT维护就行了. --------------------------------- ... 
- CH4701 天使玩偶
		题意 4701 天使玩偶 0x40「数据结构进阶」例题 描述 题目PDF 样例输入 2 3 1 1 2 3 2 1 2 1 3 3 2 4 2 样例输出 1 2 来源 石家庄二中Violet 3杯省选 ... 
随机推荐
- 2016.5.18——Excel Sheet Column Number
			Excel Sheet Column Number 本题收获: 1.对于字符串中字母转为ASIIC码:string s ;res = s[i]-'A'; 这个res就是数字s[i]-'A'是对ASII ... 
- springcloud中eureka集群unavailable-replicas
			unavailable-replicas 配置了集群,但是在注册中心显示另外的几个集群地址是不可用的: 1 首先需要再host中添加服务名映射,如果应映射了再看是否在yml中配置了prefer-ip- ... 
- crond检查服务状态
			代码如下: * */1 * * * /etc/init.d/ntpd status;if [ $? -ne 0 ];then /etc/init.d/ntpd start; fi 
- 【内核】linux内核启动流程详细分析【转】
			转自:http://www.cnblogs.com/lcw/p/3337937.html Linux内核启动流程 arch/arm/kernel/head-armv.S 该文件是内核最先执行的一个文件 ... 
- 关于mysql的wait_timeout参数 设置不生效的问题【转】
			关于wait_timeout 有一次去online set wait_timeout 的时候发现改了不生效,如下: mysql> show variables like 'wait_timeou ... 
- cocos2dx中调用TinyXml读取xml配置文件  || cocos2d-x 中跨平台tinyxml读取xml文件方式
			TiXmlDocument *doc = newTiXmlDocument; #if (CC_TARGET_PLATFORM ==CC_PLATFORM_ANDROID) //Android平台tin ... 
- Go 1 Release Notes
			Go 1 Release Notes Introduction to Go 1 Changes to the language Append Close Composite literals Goro ... 
- 08  Packages 包
			Packages Standard library Other packages Sub-repositories Community Standard library Name Synopsis ... 
- [android] The_connection_to_adb_is_down__and_a_severe_error_has_occured解决方案
			初学安卓,这是我碰到的第一个问题,从网上找了些解决方法,同时也把问题解决了. 方案一 1.先把eclipse关闭. 2.在管理器转到你的android SDK 的platform-tools下, 如图 ... 
- 《精通Python设计模式》学习结构型之MVC模式
			这个就不需要多评论了, 哪个主流的PYTHON的WEB框架都有这些模式实现哈. quotes = ('A man is not complete until he is married. Then h ... 
