Guest User

Untitled

a guest
Apr 29th, 2017
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <string>
  11. #include <cstring>
  12. #include <complex>
  13. using namespace std;
  14.  
  15. typedef long long ll;
  16. typedef pair<int, int> pii;
  17. #define mp make_pair
  18.  
  19. const double MAGIC = 3;
  20. const int N = 1 << 17;
  21. const int C = 503;
  22. struct Node
  23. {
  24.     int l, r;
  25.     vector<pii> a;
  26.  
  27.     Node() : l(), r(), a() {}
  28.     Node(int _l, int _r) : l(_l), r(_r), a() {}
  29. };
  30. Node tree[2 * N];
  31.  
  32. void build()
  33. {
  34.     for (int i = 0; i < N; i++)
  35.         tree[N + i] = Node(i, i + 1);
  36.     for (int i = N - 1; i > 0; i--)
  37.         tree[i] = Node(tree[2 * i].l, tree[2 * i + 1].r);
  38.     return;
  39. }
  40.  
  41. int n;
  42. int a[C][C];
  43. int CNT = 0;
  44. int ans[N];
  45. int ANS = 2 * C * C;
  46. pii all[N];
  47.  
  48. void addSegm(int v, int l, int r, pii x)
  49. {
  50.     if (l <= tree[v].l && tree[v].r <= r)
  51.     {
  52.         tree[v].a.push_back(x);
  53.         return;
  54.     }
  55.     if (l >= tree[v].r || tree[v].l >= r)
  56.         return;
  57.     addSegm(2 * v, l, r, x);
  58.     addSegm(2 * v + 1, l, r, x);
  59.     return;
  60. }
  61.  
  62. bool checkCell(int x, int y)
  63. {
  64.     if (x < 0 || x >= C || y < 0 || y >= C) return false;
  65.     return a[x][y];
  66. }
  67. void addAns(int x, int y)
  68. {
  69.     for (int dx = 0; dx * dx < ANS; dx++)
  70.         for (int dy = 0;; dy++)
  71.         {
  72.             int d = dx * dx + dy * dy;
  73.             if (d >= ANS) break;
  74.             if (checkCell(x - dx, y - dy) || checkCell(x - dx, y + dy) || checkCell(x + dx, y - dy) || checkCell(x + dx, y + dy))
  75.             {
  76.                 ANS = d;
  77.                 break;
  78.             }
  79.         }
  80. }
  81.  
  82. int sqr(int x)
  83. {
  84.     return x * x;
  85. }
  86. void addAll(int x, int y)
  87. {
  88.     for (int i = 0; i < CNT; i++)
  89.         ANS = min(ANS, sqr(x - all[i].first) + sqr(y - all[i].second));
  90. }
  91.  
  92. void dfs(int v)
  93. {
  94.     if (tree[v].l >= n) return;
  95.     int oldANS = ANS;
  96.     for (pii x : tree[v].a)
  97.     {
  98.         //printf("in %d adding (%d %d)\n", v, x.first, x.second);
  99.         if (ANS * MAGIC < CNT)
  100.             addAns(x.first, x.second);
  101.         else
  102.             addAll(x.first, x.second);
  103.         all[CNT++] = x;
  104.         a[x.first][x.second] = 1;
  105.     }
  106.     if (v >= N)
  107.     {
  108.         ans[v - N] = (CNT >= 2 ? ANS : 0);
  109.     }
  110.     else
  111.     {
  112.         dfs(2 * v);
  113.         dfs(2 * v + 1);
  114.     }
  115.     ANS = oldANS;
  116.     CNT -= (int)tree[v].a.size();
  117.     for (pii x : tree[v].a)
  118.         a[x.first][x.second] = 0;
  119.     return;
  120. }
  121.  
  122. int main()
  123. {
  124. //    freopen("input.txt", "r", stdin);
  125. //    freopen("output.txt", "w", stdout);
  126.  
  127.     for (int i = 0; i < C; i++)
  128.         for (int j = 0; j < C; j++)
  129.             a[i][j] = -1;
  130.  
  131.     build();
  132.     scanf("%d", &n);
  133.     for (int i = 0; i < n; i++)
  134.     {
  135.         char c;
  136.         int x, y;
  137.         scanf(" %c %d%d", &c, &x, &y);
  138.         if (c == '+')
  139.             a[x][y] = i;
  140.         else
  141.         {
  142.             addSegm(1, a[x][y], i, mp(x, y));
  143.             a[x][y] = -1;
  144.         }
  145.     }
  146.     for (int x = 0; x < C; x++)
  147.         for (int y = 0; y < C; y++)
  148.         {
  149.             if (a[x][y] != -1)
  150.                 addSegm(1, a[x][y], n, mp(x, y));
  151.             a[x][y] = 0;
  152.         }
  153.     for (int i = 1; i < 2 * N; i++)
  154.         random_shuffle(tree[i].a.begin(), tree[i].a.end());
  155.     dfs(1);
  156.     for (int i = 0; i < n; i++)
  157.         printf("%d\n", ans[i]);
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment