讲道理第四组为什么会错

P1429 平面最近点对(加强版)

__shiina @ 2018-06-22 19:46:43

洛谷日常90分(一脸懵逼) cpp...

include<bits/stdc++.h>

#define ls T[x].son[0]
#define rs T[x].son[1]
#define inf 10e9+505
#define eps 1e-10
using namespace std;

int Getint(){
    char c=getchar();int ret=0,f=1;
    while(!isdigit(c)){if(c=='-')f=-f;c=getchar();}
    while(isdigit(c)) ret=(ret<<1)+(ret<<3)+c-'0',c=getchar();
return ret*f;
}
const int Maxn=200005;
bool type;
  //0:按x排序
  //1:按y排序
int n,tpos;
double tx,ty;
double MinDis;
int root;
struct Point{
    double x,y;
    bool operator <(const Point &a)const{
        return (type)?(a.x<x):(a.y<y);
  }
}P[Maxn];
struct node{double maxx,minx,maxy,miny,x,y;int son[2];}T[Maxn];

namespace KD_Tree{

  void pushup(int x){
      T[x].maxx=T[x].minx=T[x].x;
      T[x].maxy=T[x].miny=T[x].y;
      if(ls){
          T[x].maxx=max(T[x].maxx,T[ls].maxx);
          T[x].minx=min(T[x].minx,T[ls].minx);
          T[x].maxy=max(T[x].maxy,T[ls].maxy);
          T[x].miny=min(T[x].miny,T[ls].miny);
      }
      if(rs){
          T[x].maxx=max(T[x].maxx,T[rs].maxx);
          T[x].minx=min(T[x].minx,T[rs].minx);
          T[x].maxy=max(T[x].maxy,T[rs].maxy);
          T[x].miny=min(T[x].miny,T[rs].miny);
      }
  }

  int Build(int l,int r,int type){
      int mid=l+r>>1;::type=type;
      nth_element(P+l,P+mid+1,P+r+1);
      T[mid].x=P[mid].x;
      T[mid].y=P[mid].y;
      if(mid>l) T[mid].son[0]=Build(l,mid-1,type^1);    
      if(mid<r) T[mid].son[1]=Build(mid+1,r,type^1);
      pushup(mid);
      return mid; 
  }

  inline double Probe_Min(int x){
      double mx1=max(tx-T[x].maxx,0.0);
      double mx2=max(T[x].minx-tx,0.0);
      double my1=max(ty-T[x].maxy,0.0);
      double my2=max(T[x].miny-ty,0.0);
      return mx1*mx1+mx2*mx2+my1*my1+my2*my2;
  }

  inline void Query_Min(int x){
      double mx=T[x].x-tx;
      double my=T[x].y-ty;
      if(tpos^x) MinDis=min(MinDis,mx*mx+my*my);
      if(ls&&Probe_Min(ls)<MinDis) Query_Min(ls);
      if(rs&&Probe_Min(rs)<MinDis) Query_Min(rs);
  }

}using namespace KD_Tree;

int main(){
  n=Getint();
  for(int i=1;i<=n;++i) scanf("%lf%lf",&P[i].x,&P[i].y);
  root=Build(1,n,0);
  MinDis=inf;
  for(int i=1;i<=n;++i){
      tx=P[i].x;
      ty=P[i].y;
      tpos=i;
      Query_Min(root);
  }
  cout<<fixed<<setprecision(4)<<sqrt(MinDis);
  return 0;
}

...


by __shiina @ 2018-06-26 09:20:45

inf开小了慌得一匹。。


|