[BZOJ2716]天使玩偶

[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;
}
posted @ 2018-08-12 16:52  skylee03  阅读(148)  评论(0编辑  收藏  举报