1. 程式人生 > >BZOJ 2648 SJY擺棋子(KD Tree)

BZOJ 2648 SJY擺棋子(KD Tree)

tor air math space min void php class sta

http://www.lydsy.com/JudgeOnline/problem.php?id=2648

題意:
技術分享

思路:

KDtree模板題。

參考自http://www.cnblogs.com/rayrayrainrain/p/6349899.html。

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<vector>
  6 #include<stack>
  7
#include<queue> 8 #include<cmath> 9 #include<map> 10 #include<set> 11 using namespace std; 12 typedef long long ll; 13 typedef pair<int,int> pll; 14 const int INF = 0x3f3f3f3f; 15 const int maxn = 300000+5; 16 17 int x,y; 18 int n,m; 19 int ans; 20
int cmp_d,root; 21 22 struct node 23 { 24 int d[2],MAX[2],MIN[2]; 25 int l,r; 26 }t[1000005]; 27 28 bool cmp(node a, node b) 29 { 30 return a.d[cmp_d]<b.d[cmp_d]||a.d[cmp_d]==b.d[cmp_d] && a.d[cmp_d^1] < b.d[cmp_d^1]; 31 } 32 33 void PushUp(int p,int
k) 34 { 35 t[p].MAX[0]=max(t[p].MAX[0],t[k].MAX[0]); 36 t[p].MAX[1]=max(t[p].MAX[1],t[k].MAX[1]); 37 t[p].MIN[0]=min(t[p].MIN[0],t[k].MIN[0]); 38 t[p].MIN[1]=min(t[p].MIN[1],t[k].MIN[1]); 39 } 40 41 42 int build(int l,int r, int D) 43 { 44 int mid = (l+r) >> 1; 45 cmp_d = D; 46 nth_element(t+l+1,t+mid+1,t+r+1,cmp) ; 47 t[mid].MAX[0] = t[mid].MIN[0] = t[mid].d[0]; 48 t[mid].MAX[1] = t[mid].MIN[1] = t[mid].d[1]; 49 if(l!=mid) t[mid].l = build(l,mid-1,D^1) ; 50 else t[mid].l = 0; 51 if(r!=mid) t[mid].r = build(mid+1,r,D^1); 52 else t[mid].r = 0; 53 if(t[mid].l) PushUp(mid,t[mid].l); 54 if(t[mid].r) PushUp(mid,t[mid].r); 55 return mid ; 56 } 57 58 59 void update(int k) 60 { 61 int p = root ; 62 int D = 0 ; 63 while(true) 64 { 65 PushUp(p,k); 66 if(t[k].d[D] <= t[p].d[D]) 67 { 68 if(!t[p].l) 69 { 70 t[p].l = k ; 71 return; 72 } 73 p = t[p].l ; 74 } 75 else 76 { 77 if(!t[p].r){ 78 t[p].r = k ; 79 return; 80 } 81 p = t[p].r ; 82 } 83 D ^= 1; 84 } 85 } 86 87 int getdis(int p,int x,int y) 88 { 89 int res = 0; 90 if(x > t[p].MAX[0])res += x - t[p].MAX[0]; 91 if(x < t[p].MIN[0])res += t[p].MIN[0] - x; 92 if(y > t[p].MAX[1])res += y - t[p].MAX[1]; 93 if(y < t[p].MIN[1])res += t[p].MIN[1] - y; 94 return res ; 95 } 96 97 98 void query(int p) 99 { 100 int d0 = abs(x - t[p].d[0]) + abs(y - t[p].d[1]) ; 101 if(d0<ans) ans = d0 ; 102 int dl , dr ; 103 if(t[p].l) dl=getdis(t[p].l,x,y) ; else dl = INF ; 104 if(t[p].r) dr=getdis(t[p].r,x,y) ; else dr = INF ; 105 if(dl < dr) 106 { 107 if(dl < ans) query(t[p].l) ; 108 if(dr < ans) query(t[p].r) ; 109 } 110 else 111 { 112 if(dr < ans) query(t[p].r) ; 113 if(dl < ans) query(t[p].l) ; 114 } 115 } 116 117 int main() 118 { 119 //freopen("in.txt","r",stdin); 120 scanf("%d%d",&n,&m); 121 for(int i = 1; i <= n ; i ++ ) 122 scanf("%d%d",&t[i].d[0] , &t[i].d[1]); 123 if(n) root = build(1,n,0) ; 124 for(int i = 1; i <= m ; i ++ ) 125 { 126 int q; scanf("%d%d%d",&q,&x,&y); 127 if(q == 1) 128 { 129 n++ ; 130 t[n].d[0]=t[n].MAX[0]=t[n].MIN[0]=x; 131 t[n].d[1]=t[n].MAX[1]=t[n].MIN[1]=y; 132 if(n>1) update(n); 133 else root = build(1,n,0); 134 } 135 else 136 { 137 ans = INF; 138 query(root); 139 printf("%d\n",ans); 140 } 141 } 142 return 0; 143 }

BZOJ 2648 SJY擺棋子(KD Tree)