Guest User

Closest point

a guest
Sep 28th, 2015
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. #define fs first
  6. #define sc second
  7. #define mp make_pair
  8.  
  9. using namespace std;
  10.  
  11. typedef pair<int, int> ii;
  12.  
  13. const int N = 100001;
  14. const int inf = (int) 2e9;
  15.  
  16. struct point {
  17.   int x, y, i;
  18.   bool friend operator< (point a, point b) {
  19.     if (a.x == b.x) {
  20.       return a.y < b.y;
  21.     } else {
  22.       return a.x < b.x;
  23.     }
  24.   }
  25. };
  26.  
  27. int n;
  28. int cl[N];
  29. int cld[N];
  30. int ans[N];
  31.  
  32. point a[N];
  33.  
  34. ii tp[4 * N];
  35.  
  36. ii c (ii a, ii b) {
  37.   if (a.fs == b.fs) {
  38.     if (a.sc < b.sc) {
  39.       return a;
  40.     } else {
  41.       return b;
  42.     }
  43.   } else {
  44.     if (a.fs > b.fs) {
  45.       return a;
  46.     } else {
  47.       return b;
  48.     }
  49.   }
  50. }
  51. void build (int v, int tl, int tr) {
  52.   if (tl == tr) {
  53.     tp[v].fs = -inf;
  54.     tp[v].sc = inf;
  55.   } else {
  56.     int tm = (tl + tr) >> 1;
  57.     build (v + v, tl, tm);
  58.     build (v + v + 1, tm + 1, tr);
  59.     tp[v] = c (tp[v + v], tp[v + v + 1]);
  60.   }
  61. }
  62. void up (int v, int tl, int tr, int y, int x, int i) {
  63.   if (tl == tr) {
  64.     if (tp[v].fs < x + y) {
  65.       tp[v].fs = x + y;
  66.       tp[v].sc = i;
  67.     }
  68.   } else {
  69.     int tm = (tl + tr) >> 1;
  70.     if (y <= tm) {
  71.       up (v + v, tl, tm, y, x, i);
  72.     } else {
  73.       up (v + v + 1, tm + 1, tr, y, x, i);
  74.     }
  75.     tp[v] = c (tp[v + v], tp[v + v + 1]);
  76.   }
  77. }
  78. ii gp (int v, int tl, int tr, int l, int r) {
  79.   if (l == tl && r == tr) {
  80.     return tp[v];
  81.   } else {
  82.     int tm = (tl + tr) >> 1;
  83.     if (r <= tm) {
  84.       return gp (v + v, tl, tm, l, r);
  85.     } else if (l > tm) {
  86.       return gp (v + v + 1, tm + 1, tr, l, r);
  87.     } else {
  88.       return c (gp (v + v, tl, tm, l, tm), gp (v + v + 1, tm + 1, tr, tm + 1, r));
  89.     }
  90.   }
  91. }
  92. void solve () {
  93.   sort (a, a + n);
  94.   build (1, -10000, 10000);
  95.   for (int i = 0; i < n; i++) {
  96.     if (i) {
  97.       ii below = gp (1, -10000, 10000, -10000, a[i].y);
  98.       int p = (a[i].x + a[i].y) - below.fs;
  99.       if (cld[a[i].i] > p || (cld[a[i].i] == p && cl[a[i].i] > below.sc)) {
  100.         cld[a[i].i] = p;
  101.         cl[a[i].i] = below.sc;
  102.       }
  103.     }
  104.     up (1, -10000, 10000, a[i].y, a[i].x, a[i].i);
  105.   }
  106. }
  107. void rotat () {
  108.   for (int i = 0; i < n; i++) {
  109.     swap (a[i].x, a[i].y);
  110.     a[i].x = -a[i].x;
  111.   }
  112. }
  113. int main () {
  114.   scanf ("%d", &n);
  115.   for (int i = 0; i < n; i++) {
  116.     cl[i] = -1;
  117.     cld[i] = inf;
  118.   }
  119.   for (int i = 0; i < n; i++) {
  120.     scanf ("%d %d", &a[i].x, &a[i].y);
  121.     a[i].i = i;
  122.   }
  123.   solve ();
  124.   for (int i = 0; i < 3; i++) {
  125.     rotat ();
  126.     solve ();
  127.   }
  128.   for (int i = 0; i < n; i++) {
  129.     printf ("%d%c", 1 + cl[i], " \n"[i + 1 == n]);
  130.   }
  131.   return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment