码迷,mamicode.com
首页 > 其他好文 > 详细

...

时间:2015-06-17 23:14:12      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 #include <cstdio>
  2 #include <fstream>
  3 #include <iostream>
  4  
  5 #include <cstdlib>
  6 #include <cstring>
  7 #include <algorithm>
  8 #include <cmath>
  9  
 10 #include <queue>
 11 #include <vector>
 12 #include <map>
 13 #include <set>
 14 #include <stack>
 15 #include <list>
 16  
 17 typedef unsigned int uint;
 18 typedef long long int ll;
 19 typedef unsigned long long int ull;
 20 typedef double db;
 21 typedef long double ldb;
 22  
 23 using namespace std;
 24  
 25 inline int getint()
 26 {
 27     int res=0;
 28     char c=getchar();
 29     bool mi=false;
 30     while(c<0 || c>9) mi=(c==-),c=getchar();
 31     while(0<=c && c<=9) res=res*10+c-0,c=getchar();
 32     return mi ? -res : res;
 33 }
 34 inline ll getll()
 35 {
 36     ll res=0;
 37     char c=getchar();
 38     bool mi=false;
 39     while(c<0 || c>9) mi=(c==-),c=getchar();
 40     while(0<=c && c<=9) res=res*10+c-0,c=getchar();
 41     return mi ? -res : res;
 42 }
 43 
 44 //==============================================================================
 45 //==============================================================================
 46 //==============================================================================
 47 //==============================================================================
 48 
 49 
 50 const int INF=(1<<30)-1;
 51 
 52 struct node*nil;
 53 struct node
 54 {
 55     int p[2]; //location
 56     node*s[2]; //sub trees
 57     node*f; //father node
 58     
 59     int l[2][2]; //first D:x/y second d:left(bottom)/right(top)
 60     
 61     node(){ }
 62     
 63     bool leftof(node*x,int d)
 64     { return p[d]<x->p[d]; }
 65     
 66     int dist(const int&x,const int&y)
 67     {
 68         int xl=max(l[0][0]-x,0);
 69         int xr=max(x-l[0][1],0);
 70         int yl=max(l[1][0]-y,0);
 71         int yr=max(y-l[1][1],0);
 72         //printf("cal: (%d,%d)\n",x,y);
 73         //printf("limit: (%d,%d)->(%d,%d)\n",l[0][0],l[1][0],l[0][1],l[1][1]);
 74         //printf("res: %d\n",max(xl,xr)+max(yl,yr));
 75         return max(xl,xr)+max(yl,yr);
 76     }
 77 };
 78 
 79 int ncnt=5005;
 80 node*nt;
 81 node*newnode(int x,int y)
 82 {
 83     if(ncnt==5005) { ncnt=0; nt=new node[5005]; }
 84     nt->p[0]=x; nt->p[1]=y;
 85      nt->f=nt->s[0]=nt->s[1]=nil;
 86     ncnt++; return nt++;
 87 }
 88 
 89 node*root;
 90 
 91 void Insert(node*v)
 92 {
 93     if(root==nil)
 94     { root=v; v->l[0][0]=v->l[1][0]=-INF; v->l[0][1]=v->l[1][1]=INF; return ; }
 95     
 96     node*x=root;
 97     node*t=nil;
 98     bool d=0; //d=0:compare x axis. d=1:compare y axis.
 99     while(x!=nil)
100     {
101         d^=1;
102         t=x;
103         x=x->s[x->leftof(v,d)];
104     }
105     bool k=t->leftof(v,d);
106     t->s[k]=v;
107     v->f=t;
108     v->l[0][0]=t->l[0][0]; v->l[0][1]=t->l[0][1];
109     v->l[1][0]=t->l[1][0]; v->l[1][1]=t->l[1][1];
110     v->l[d][!k]=t->p[d];
111 }
112 
113 
114 void DFS(node*x=root)
115 {
116     if(x==nil) return ;
117     DFS(x->s[0]);
118     printf("node:(%d,%d) lb:(%d,%d) rt:(%d,%d)\n",
119         x->p[0],x->p[1],x->l[0][0],x->l[1][0],x->l[0][1],x->l[1][1]);
120     DFS(x->s[1]);
121 }
122 
123 
124 int n,m;
125 
126 int abs(int p){ return p<0 ? -p : p; }
127 int mad(node*x,int a,int b){ return abs(x->p[0]-a)+abs(x->p[1]-b); }
128 int res;
129 void Query(node*a,const int&x,const int&y)
130 {
131     res=min(res,mad(a,x,y)); 
132     if(a->s[0]==a->s[1]) return ;
133     int d[]={ a->s[0]==nil ? INF : a->s[0]->dist(x,y),
134               a->s[1]==nil ? INF : a->s[1]->dist(x,y) };
135     int k=d[1]<d[0]; if(d[k]==INF) return ;
136     Query(a->s[k],x,y);
137     if(d[!k]<res) Query(a->s[!k],x,y);
138 }
139 
140 
141 int main()
142 {
143     freopen("in.txt","r",stdin);
144     freopen("out.txt","w",stdout);
145     
146     nil=newnode(0,0);
147     nil->s[0]=nil->s[1]=nil->f=nil;
148     nil->l[0][0]=nil->l[1][0]=-INF;
149     nil->l[0][1]=nil->l[1][1]=INF;
150     root=nil;
151     
152     n=getint();
153     m=getint();
154     for(int i=0;i<n;i++)
155     { int x=getint(); int y=getint(); Insert(newnode(x,y)); }
156     //DFS();
157     
158     for(int d=0;d<m;d++)
159     {
160         int t=getint();
161         int x=getint();
162         int y=getint();
163         if(t==1) Insert(newnode(x,y));
164         else { res=INF; Query(root,x,y); printf("%d\n",res); }
165     }
166     
167     //DFS();
168     
169     return 0;
170 }
View Code

 

...

标签:

原文地址:http://www.cnblogs.com/DragoonKiller/p/4584472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!