当前位置: 代码迷 >> 综合 >> Finding Hotels(kd树)
  详细解决方案

Finding Hotels(kd树)

热度:13   发布时间:2023-11-06 17:13:47.0

调了一下午的bug,突然绿了的感觉真好

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ff(a,n) for(int i = 0 ; i < n; i++) scanf("%d",a+i)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}
//int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
const ll inf = 0x3f3f3f3f;
const int mx = 2e5+10;int n,dim,m,k;
struct no{int v[3],pri,mi,id;bool operator < (const no &a)const{return v[k] < a.v[k];}no(){mi=inf;}
}p[mx*4],ob,data[mx];
struct {int x,y,pri,id;ll d;
}cur;bool f[mx*4];
//priority_queue< pair<double,no> > que;ll dis(no a, no b){ll ans = 0;for(int i = 0;i < dim; i++){ans+=1LL*(a.v[i]-b.v[i])*(a.v[i]-b.v[i]);}return ans;
}
void build(int l, int r,int rt,int dir){k = dir; if( l>r){								//不能写l==r终止 return;	}int mid = (l+r)/2;f[rt] = 1;nth_element(data+l,data+mid,data+r+1);build(l,mid-1,rt*2,(dir+1)%dim);build(mid+1,r,rt*2+1,(dir+1)%dim);p[rt] = data[mid];p[rt].mi = min(min(p[ls].mi,p[rs].mi),p[rt].mi);  //要跟自己比较 }
void query(int rt,int dir){k = dir; if(!f[rt])return;ll d = dis(ob,p[rt]);if(p[rt].pri <= ob.pri){  if(d == cur.d && p[rt].id < cur.id){cur.x = p[rt].v[0];cur.y = p[rt].v[1];cur.pri = p[rt].pri;cur.id = p[rt].id;cur.d = d;}else if(d < cur.d){cur.x = p[rt].v[0];cur.y = p[rt].v[1];cur.pri = p[rt].pri;cur.id = p[rt].id;cur.d = d;}}	 int son;if(ob.v[dir] >= p[rt].v[dir]){son = rs;}elseson = ls;if(f[son])query(son,(dir+1)%dim);ll te = 1LL*(ob.v[dir]-p[rt].v[dir])*(ob.v[dir]-p[rt].v[dir]);if(f[son^1]&&te<=cur.d && p[son^1].mi <= ob.pri){    //严谨一点要加=		query(son^1,(dir+1)%dim);}}int main(){//   freopen("F:\\in.txt","r",stdin);int T=10;    scanf("%d",&T);dim = 2;while(T--){s2(n,m); mem(f,0);for(int i = 1; i <= n ;i++){s3(data[i].v[0],data[i].v[1],data[i].pri);data[i].id = i;data[i].mi = data[i].pri;}build(1, n, 1, 0);	while(m--){s3(ob.v[0],ob.v[1],ob.pri);cur.d=5e11;         //刚开始这里设了inf 一直wa,在上面函数瞎检查很久 cur.id = inf;query(1, 0);printf("%d %d %d\n",cur.x,cur.y,cur.pri);}} return 0;
}

 

  相关解决方案