标签:
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2626
题解:裸K-Dtree,最大值?自己yy一下估价函数就好了。
两题居然是同一个错误,真是too naive。。。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1ll<<62 13 #define maxn 200000+5 14 #define maxm 100000+5 15 #define eps 1e-10 16 #define ll double 17 #define pa pair<ll,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 23 #define mod 1000000007 24 #define sqr(x) (x)*(x) 25 using namespace std; 26 inline int read() 27 { 28 int x=0,f=1;char ch=getchar(); 29 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 30 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 31 return x*f; 32 } 33 int n,m,cur; 34 priority_queue<pa,vector<pa>,greater<pa> >q; 35 struct rec 36 { 37 int mi[2],mx[2],d[2],l,r,id; 38 int& operator [](int i){return d[i];} 39 }p[maxn],t[maxn],now; 40 bool operator <(rec a,rec b){return a[cur]<b[cur];} 41 inline void pushup(int k) 42 { 43 int l=t[k].l,r=t[k].r; 44 for0(i,1) 45 { 46 t[k].mi[i]=min(t[k][i],min(t[l].mi[i],t[r].mi[i])); 47 t[k].mx[i]=max(t[k][i],max(t[l].mx[i],t[r].mx[i])); 48 } 49 } 50 inline int build(int l,int r,int dir) 51 { 52 int mid=(l+r)>>1; 53 cur=dir; 54 nth_element(p+l,p+mid,p+r+1); 55 t[mid]=p[mid]; 56 for0(i,1)t[mid].mi[i]=t[mid].mx[i]=t[mid][i]; 57 t[mid].l=l>mid-1?0:build(l,mid-1,dir^1); 58 t[mid].r=mid+1>r?0:build(mid+1,r,dir^1); 59 pushup(mid); 60 return mid; 61 } 62 inline ll calc(int k) 63 { 64 if(!k)return -inf-1; 65 ll ret=0; 66 for0(i,1)ret+=max((ll)sqr(now[i]-t[k].mi[i]),(ll)sqr(t[k].mx[i]-now[i])); 67 return ret; 68 } 69 inline ll dist(rec a,rec b){return (ll)sqr(a[0]-b[0])+(ll)sqr(a[1]-b[1]);} 70 inline void query(int k) 71 { 72 if(!k)return; 73 ll dl=calc(t[k].l),dr=calc(t[k].r),d=dist(t[k],now); 74 if(d>q.top().first||(d==q.top().first&&t[k].id<-q.top().second))q.pop(),q.push(pa(d,-t[k].id)); 75 if(dl>dr) 76 { 77 if(dl>=q.top().first)query(t[k].l); 78 if(dr>=q.top().first)query(t[k].r); 79 }else 80 { 81 if(dr>=q.top().first)query(t[k].r); 82 if(dl>=q.top().first)query(t[k].l); 83 } 84 } 85 86 int main() 87 { 88 freopen("input.txt","r",stdin); 89 freopen("output.txt","w",stdout); 90 n=read(); 91 for1(i,n)p[i][0]=read(),p[i][1]=read(),p[i].id=i; 92 for0(i,1)t[0].mi[i]=1000000000,t[0].mx[i]=-1000000000; 93 int rt=build(1,n,0); 94 m=read(); 95 while(m--) 96 { 97 now[0]=read();now[1]=read();int k=read(); 98 while(!q.empty())q.pop(); 99 for1(i,k)q.push(pa(-inf,0)); 100 query(rt); 101 printf("%d\n",-q.top().second); 102 } 103 return 0; 104 }
标签:
原文地址:http://www.cnblogs.com/zyfzyf/p/4266132.html