BZOJ 2648 SJY摆箱子

kdtree模板题

我判是否查询时用mid来判没有存极值,然后貌似被卡常了?

hzwer的代码在我本地的随机数据中跑得没我快...难道bzoj上的数据有构造的?

//By Richard
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#define rep(x,y,z) for (int x=(y);(x)<=(z);(x)++)
#define per(x,y,z) for (int x=(y);(x)>=(z);(x)--)
#define log2(x) (31-__builtin_clz(x))
#define mod (int)(1e9+7)
#define inf 0x3f3f3f3f
#define cls(x) memset(x,0,sizeof(x))
#ifdef DEBUG
#define debugdo(X) X
#define debugndo(X)
#define debugout(X) cout<<(#X)<<"="<<(X)<<endl
#else
#define debugdo(X)
#define debugndo(X) X
#define debugout(X)
#endif // debug
#ifdef ONLINE_JUDGE
#define debugdo(X)
#define debugndo(X) X
#define debugout(X)
#endif
#define putarray(x,n) rep(iiii,1,n) printf("%d ",x[iiii])
#define mp make_pair
using namespace std;
typedef pair<int,int> pairs;
typedef long long LL;
/read3.0
template <typename T>
inline void read(T &x){char ch;x=0;bool flag=false;ch=getchar();while (ch>'9'||ch<'0') {ch=getchar();if (ch=='-') flag=true;}while ((ch<='9'&&ch>='0')){x=x*10+ch-'0';ch=getchar();}if (flag) x*=-1;}
template <typename T>
inline void read(T &x,T &y){read(x);read(y);}
/variables&functions
const int maxn=1111111;
int n,doing,m,ans;
bool now;
struct POINT
{
	int x,y;
}point[maxn];
struct Node
{
	Node *l,*r;
	int id;
	inline bool operator<(const Node &other)const
	{
		if (now) return point[id].x<point[other.id].x;
		else return point[id].y<point[other.id].y;
	}
}node[maxn];
struct kd_tree
{
	Node *root;
	// Node node[maxn];
	inline void init(const int &vv)
	{
		rep(i,1,vv) node[i].id=i;
	}
	Node *build(const int &l,const int &r,const bool &d)
	{
		if (l==r) return node+l;
		else if (l>r) return 0;
		now=d;
		int mid=(l+r)>>1;
		nth_element(node+l,node+mid,node+r+1);
		node[mid].l=build(l,mid-1,d^1);
		node[mid].r=build(mid+1,r,d^1);
		return &node[mid];
	} 
	inline void insert(const int &x)
	{
		// doing=x;
		node[x].id=x;
		Node *nowp=root;
		bool now=1;
		while (1)
		{
			if (now)
			{
				if (point[x].x<point[nowp->id].x)
				{
					if (nowp->l) nowp=nowp->l;
					else 
					{
						nowp->l=&node[x];
						break;
					}
				}
				else 
				{
					if (nowp->r) nowp=nowp->r;
					else
					{
						nowp->r=&node[x];
						break;
					}
				}
			}
			else
			{
				if (point[x].y<point[nowp->id].y) 
				{
					if (nowp->l) nowp=nowp->l;
					else 
					{
						nowp->l=&node[x];
						break;
					}
				}
				else 
				{
					if (nowp->r) nowp=nowp->r;
					else
					{
						nowp->r=&node[x];
						break;
					}
				}
			}
			now^=1;
		}
	}
	inline int dis(const int &x)
	{
		return abs(point[x].x-point[doing].x)+abs(point[x].y-point[doing].y);
	}
	void querycore(const Node *x,const bool &d)
	{
		// now=d;
		ans=min(ans,dis(x->id));
		if (d)
		{
			if (point[x->id].x>point[doing].x)
			{
				if (x->l) querycore(x->l,d^1);
				if (point[x->id].x-point[doing].x<ans&&x->r) querycore(x->r,d^1);
			}
			else
			{
				if (x->r) querycore(x->r,d^1);
				if (point[doing].x-point[x->id].x<ans&&x->l) querycore(x->l,d^1);
			}
		}
		else
		{
			if (point[x->id].y>point[doing].y)
			{
				if (x->l) querycore(x->l,d^1);
				if (point[x->id].y-point[doing].y<ans&&x->r) querycore(x->r,d^1);
			}
			else
			{
				if (x->r) querycore(x->r,d^1);
				if (point[doing].y-point[x->id].y<ans&&x->l) querycore(x->l,d^1);
			}
		}
	}
	inline int query(const int &x)
	{
		ans=inf;
		doing=x;
		querycore(root,1);
		return ans;
	}
}kdtree;
int main()
{
	read(n,m);
	kdtree.init(n+m);
	rep(i,1,n) read(point[i].x,point[i].y);
	kdtree.root=kdtree.build(1,n,1);
	int tt;
	rep(i,n+1,n+m)
	{
		read(tt);
		read(point[i].x,point[i].y);
		if (tt==2) printf("%d\n",kdtree.query(i));
		else kdtree.insert(i);
		
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值