class="code_img_closed" id="code_img_closed_2b7f3279-4150-4a2e-88a6-03ae8ad01b24"
class="code_img_opened" id="code_img_opened_2b7f3279-4150-4a2e-88a6-03ae8ad01b24"
1 #include
2 #include <string.h>
3 #include
4 #include
5 const int MAXN = 100100;
6 struct Point{
7 double x, y;
8 }x[MAXN];
9 int y[MAXN], z[MAXN];
10 int cmpx(const void* a, const void* b)
11 { return ((Point*)a)->x - ((Point*)b)->x; }
12 int cmpy(const void* a, const void* b)
13 { return x[(*(int*)a)].y - x[(*(int*)b)].y; }
14 inline double min(double a, double b)
15 { return a a : b; }
16 inline double dis(Point a, Point b)
17 { return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); }
18 void Merge(int z[], int y[], int l, int r, int m)
19 {
20 int i = l, j = m+1, k = l;
21 while(i <= m && j <= r)
22 if(x[z[i]].y <= x[z[j]].y) y[k++] = z[i++];
23 else y[k++] = z[j++];
24 while(i <= m) y[k++] = z[i++];
25 while(j <= r) y[k++] = z[j++];
26 }
27
28 double close(int y[], int z[], int l, int r)
29 {
30 if(l+1 == r)
31 return dis(x[l], x[r]);
32 if(l+2 == r)
33 return min(dis(x[l], x[l+1]), min(dis(x[l+1], x[r]), dis(x[l], x[r])));
34 int m = l+r>>1;
35 for(int i=l, j=m+1, k=l ; k<=r ; k++)
36 if(y[k] > m) z[j++] = y[k];
37 else z[i++] = y[k];
38 double res = min(close(z, y, l, m), close(z, y, m+1, r));
39 Merge(z, y, l, r, m);
40 int rt = l;
41 for(int i=l ; i<=r ; i++)
42 if(fabs(x[y[i]].x - x[y[m]].x) < res)
43 z[rt++] = y[i];
44 for(int i=l ; i