/**
* Author: Stanford
* Date: Unknown
* Source: Stanford Notebook
* Description: KD-tree (2d, can be extended to 3d)
* Status: Tested on excellentengineers
*/#pragma once
#include"src/geometry/Point.h"typedeflonglongT;typedefPoint<T>P;constTINF=numeric_limits<T>::max();boolon_x(constP&a,constP&b){returna.x<b.x;}boolon_y(constP&a,constP&b){returna.y<b.y;}structNode{Ppt;// if this is a leaf, the single point in itTx0=INF,x1=-INF,y0=INF,y1=-INF;// boundsNode*first=0,*second=0;Tdistance(constP&p){// min squared distance to a pointTx=(p.x<x0?x0:p.x>x1?x1:p.x);Ty=(p.y<y0?y0:p.y>y1?y1:p.y);return(P(x,y)-p).dist2();}Node(vector<P>&&vp):pt(vp[0]){for(Pp:vp){x0=min(x0,p.x);x1=max(x1,p.x);y0=min(y0,p.y);y1=max(y1,p.y);}if(vp.size()>1){// split on x if width >= height (not ideal...)sort(all(vp),x1-x0>=y1-y0?on_x:on_y);// divide by taking half the array for each child (not// best performance with many duplicates in the middle)inthalf=sz(vp)/2;first=newNode({vp.begin(),vp.begin()+half});second=newNode({vp.begin()+half,vp.end()});}}};structKDTree{Node*root;KDTree(constvector<P>&vp):root(newNode({all(vp)})){}pair<T,P>search(Node*node,constP&p){if(!node->first){// uncomment if we should not find the point itself:// if (p == node->pt) return {INF, P()};returnmake_pair((p-node->pt).dist2(),node->pt);}Node*f=node->first,*s=node->second;Tbfirst=f->distance(p),bsec=s->distance(p);if(bfirst>bsec)swap(bsec,bfirst),swap(f,s);// search closest side first, other side if neededautobest=search(f,p);if(bsec<best.first)best=min(best,search(s,p));returnbest;}// find nearest point to a point, and its squared distance// (requires an arbitrary operator< for Point)pair<T,P>nearest(constP&p){returnsearch(root,p);}};
Traceback(mostrecentcalllast):File"/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/onlinejudge_verify/documentation/build.py",line71,in_render_source_code_statbundled_code=language.bundle(stat.path,basedir=basedir,options={'include_paths':[basedir]}).decode()~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/onlinejudge_verify/languages/cplusplus.py",line187,inbundlebundler.update(path)~~~~~~~~~~~~~~^^^^^^File"/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py",line312,inupdateraiseBundleErrorAt(path,i+1,"#pragma once found in a non-first line")onlinejudge_verify.languages.cplusplus_bundle.BundleErrorAt:src/geometry/kdTree.h:line8:#pragmaoncefoundinanon-firstline