a53

Links

a53
May 31st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define maxN 1000002
  3. #define inf 1000000002
  4. using namespace std;
  5. FILE *fin = freopen("links.in", "r", stdin);
  6. FILE *fout = freopen("links.out", "w", stdout);
  7. int n;
  8. struct Point
  9. {
  10. int x,y;
  11. bool operator < (const Point &ot) const
  12. {
  13. if (x==ot.x)
  14. return y<ot.y;
  15. return x < ot.x;
  16. }
  17. } p[maxN];
  18.  
  19. struct Rect//angle
  20. {
  21. Point a, b;
  22. } v[maxN * 4];
  23. int N,par[maxN * 4];
  24.  
  25. void getRect()
  26. {
  27. sort(p + 1, p + n + 1);
  28. p[0].x = -1;
  29. for (int i = 1; i <= n; ++ i)
  30. {
  31. if (p[i - 1].x + 1 != p[i].x)
  32. v[++ N] = Rect{Point{p[i - 1].x + 1, -1}, Point{p[i].x - 1, inf}};
  33. v[++ N] = Rect{Point{p[i].x, -1}, Point{p[i].x, p[i].y - 1}};
  34. while (i < n && p[i].x == p[i + 1].x)
  35. {
  36. if (p[i].y + 1 != p[i + 1].y)
  37. v[++ N] = Rect{Point{p[i].x, p[i].y + 1}, Point{p[i].x, p[i + 1].y - 1}};
  38. ++ i;
  39. }
  40. v[++ N] = Rect{Point{p[i].x, p[i].y + 1}, Point{p[i].x, inf}};
  41. }
  42. v[++ N] = Rect{Point{p[n].x + 1, -1}, Point{inf, inf}};
  43. }
  44.  
  45. int root(int x)
  46. {
  47. if (par[x] == x) return x;
  48. return par[x] = root(par[x]);
  49. }
  50.  
  51. bool Inters(int l1, int r1, int l2, int r2)
  52. {
  53. if (l1 < l2)
  54. return r1 >= l2;
  55. return r2 >= l1;
  56. }
  57.  
  58. void findNbrs()
  59. {
  60. int it1 = 1;
  61. for (int i = 1; i <= N; ++ i) par[i] = i;
  62. for (int it2 = 2; it2 <= N; ++ it2)
  63. {
  64. while (it1 < it2 && (v[it1].b.x + 1 < v[it2].a.x ||
  65. (v[it1].b.x + 1 == v[it2].a.x && v[it1].b.y < v[it2].a.y)))
  66. ++ it1;
  67. if (v[it1].b.x + 1 == v[it2].a.x &&
  68. Inters(v[it1].a.y, v[it1].b.y, v[it2].a.y, v[it2].b.y))
  69. {
  70. int rx = root(it1),
  71. ry = root(it2);
  72. if (rx != ry)
  73. par[rx] = ry;
  74. }
  75. }
  76.  
  77. it1 = N;
  78. for (int it2 = N - 1; it2 >= 1; -- it2)
  79. {
  80. while (it1 > it2 && (v[it1].a.x - 1 > v[it2].b.x ||
  81. (v[it1].a.x - 1 == v[it2].b.x && v[it1].a.y > v[it2].b.y)))
  82. -- it1;
  83. if (v[it1].a.x - 1 == v[it2].b.x &&
  84. Inters(v[it1].a.y, v[it1].b.y, v[it2].a.y, v[it2].b.y))
  85. {
  86. int rx = root(it1),
  87. ry = root(it2);
  88. if (rx != ry)
  89. par[rx] = ry;
  90. }
  91. }
  92. }
  93.  
  94. int RectId(Point P)
  95. {
  96. int i = 0, p = 1;
  97. while (p < N) p <<= 1;
  98. while (p)
  99. {
  100. if (i + p <= N && (v[i + p].a.x < P.x || (v[i + p].a.x == P.x && v[i + p].b.y < P.y)))
  101. i += p;
  102. p >>= 1;
  103. }
  104. ++ i;
  105. return i;
  106. }
  107.  
  108. int main()
  109. {
  110. int q;
  111. scanf("%d%d", &n, &q);
  112. for (int i = 1; i <= n; ++ i)
  113. scanf("%d%d", &p[i].x, &p[i].y);
  114. getRect();
  115. findNbrs();
  116. while (q --)
  117. {
  118. Point A, B;
  119. scanf("%d%d%d%d", &A.x, &A.y, &B.x, &B.y);
  120. if (root(RectId(A)) == root(RectId(B)))
  121. printf("1\n");
  122. else
  123. printf("0\n");
  124. }
  125. return 0;
  126. }
Add Comment
Please, Sign In to add comment