# bzoj2648/2716 kdtree

## SJY摆棋子

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 5199  Solved: 1813
[Submit][Status][Discuss]

2 3
1 1
2 3
2 1 2
1 3 3
2 4 2

1
2

kdtree可以过

## Source

  1 #include<cstring>
2 #include<cmath>
3 #include<iostream>
4 #include<algorithm>
5 #include<cstdio>
6
7 #define inf 1000000007
8 #define N 500007
9 #define ll long long
10 using namespace std;
12 {
13     int x=0,f=1;char ch=getchar();
14     while(ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
15     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
16     return x*f;
17 }
18
19 int n,m,rt,F;
20 struct Node
21 {
22     int d[2],mn[2],mx[2],l,r;
23     int& operator[](int x)
24     {
25         return d[x];
26     }
27     Node(int x=0,int y=0)//无代入的话x,y为0，代入即为代入值。
28     {
29         l=0,r=0;
30         d[0]=x,d[1]=y;
31     }
32 }p[N];
33 bool operator<(Node x,Node y)
34 {
35     return x[F]<y[F];
36 }
37 inline int dis(Node x,Node y)
38 {
39     return abs(x[0]-y[0])+abs(x[1]-y[1]);
40 }
41 struct kdtree
42 {
43     int ans;
44     Node tr[N*2],T;
45     void update(int p)
46     {
47         Node l=tr[tr[p].l],r=tr[tr[p].r];
48         for (int i=0;i<2;i++)
49         {
50             if (tr[p].l)tr[p].mn[i]=min(tr[p].mn[i],l.mn[i]),tr[p].mx[i]=max(tr[p].mx[i],l.mx[i]);
51             if (tr[p].r)tr[p].mn[i]=min(tr[p].mn[i],r.mn[i]),tr[p].mx[i]=max(tr[p].mx[i],r.mx[i]);
52         }
53     }
54     int build(int l,int r,int now)
55     {
56         F=now;int mid=(l+r)>>1;
57         nth_element(p+l,p+mid,p+r+1);
58         tr[mid]=p[mid];
59         for (int i=0;i<2;i++)
60             tr[mid].mn[i]=tr[mid].mx[i]=tr[mid][i];
61         if (l<mid)tr[mid].l=build(l,mid-1,now^1);
62         if (r>mid)tr[mid].r=build(mid+1,r,now^1);
63         update(mid);
64         return mid;
65     }
66     void ins(int p,int now)
67     {
68         if (T[now]>=tr[p][now])
69         {
70             if(tr[p].r)ins(tr[p].r,now^1);
71             else
72             {
73                 tr[p].r=++n,tr[n]=T;
74                 for (int i=0;i<2;i++)
75                     tr[n].mn[i]=tr[n].mx[i]=tr[n][i];
76             }
77         }
78         else
79         {
80             if (tr[p].l)ins(tr[p].l,now^1);
81             else
82             {
83                 tr[p].l=++n,tr[n]=T;
84                 for (int i=0;i<2;i++)
85                     tr[n].mn[i]=tr[n].mx[i]=tr[n][i];
86             }
87         }
88         update(p);
89     }
90     int get(int k,Node p)
91     {
92         int tmp=0;
93         for (int i=0;i<2;i++)
94             tmp+=max(0,tr[k].mn[i]-p[i]);
95         for (int i=0;i<=2;i++)
96              tmp+=max(0,p[i]-tr[k].mx[i]);
97         return tmp;
98     }
99     void query(int p,int now)
100     {
101         int d,dl=inf,dr=inf;
102         d=dis(tr[p],T);
103         ans=min(ans,d);
104         if (tr[p].l)dl=get(tr[p].l,T);
105         if (tr[p].r)dr=get(tr[p].r,T);
106         if (dl<dr)
107         {
108             if (dl<ans)query(tr[p].l,now^1);
109             if (dr<ans)query(tr[p].r,now^1);
110         }
111         else
112         {
113             if (dr<ans)query(tr[p].r,now^1);
114             if (dl<ans)query(tr[p].l,now^1);
115         }
116     }
117     int query(Node p)
118     {
119         ans=inf,T=p,query(rt,0);
120         return ans;
121     }
122     void ins(Node p)
123     {
124         T=p;
125         ins(rt,0);
126     }
127 }kd;
128 int main()
129 {
130     freopen("fzy.in","r",stdin);
131     freopen("fzy.out","w",stdout);
132
134     for (int i=1;i<=n;i++)
143 }