Advertisement
a53

PunctInPoligonSimplu

a53
May 9th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. #define nmax 805
  6. #define y first
  7. #define x second
  8. #define y1 first
  9. #define y2 second
  10. #define mp make_pair
  11.  
  12. using namespace std;
  13.  
  14. typedef pair <int, int> ii;
  15. typedef pair <float, int> interval;
  16.  
  17. int n, m, nintr, nrsegm [nmax];
  18. ii p [nmax], p2 [nmax];
  19. ii intr [nmax];
  20. interval segm [nmax] [nmax];
  21.  
  22. void scan ()
  23. {
  24. int i;
  25. scanf ("%d%d", &n, &m);
  26. for (i=1; i <= n; ++i)
  27. {
  28. scanf ("%d%d", &p [i].x, &p [i].y);
  29. p2 [i]=p [i];
  30. }
  31. }
  32.  
  33. inline float xmijloc (int p1, int p2, int k)
  34. {
  35. float mx, p1x, p2x, p1y, p2y;
  36. int aux;
  37. if (p [p1].y > p [p2].y)
  38. {
  39. aux=p1;
  40. p1=p2;
  41. p2=aux;
  42. }
  43. p1y=p [p1].y;
  44. p2y=p [p2].y;
  45. if (p1y < intr [k].y1)
  46. p1y=intr [k].y1;
  47. if (p2y > intr [k].y2)
  48. p2y=intr [k].y2;
  49. int a=p [p1].y-p [p2].y, b=p [p2].x-p [p1].x, c=p [p1].x*p [p2].y - p [p1].y*p [p2].x;
  50. //long long a=p [p1].y-p [p2].y, b=p [p2].x-p [p1].x, c=(long long)p [p1].x*p [p2].y - (long long)p [p1].y*p [p2].x;
  51. p1x=(float)(-b*p1y-c)/a;
  52. p2x=(float)(-b*p2y-c)/a;
  53. mx=(p1x + p2x)/2;
  54. return mx;
  55. }
  56.  
  57. void bucatele ()
  58. {
  59. int i, j;
  60. ii a1, a2, aux;
  61. sort (p2+1, p2+1+n);
  62. p [n+1]=p [1];
  63. i=2;
  64. for (; i < n && p2 [i].y == p2 [i-1].y; ++i);
  65. --i;
  66. for (; i < n; ++i)
  67. {
  68. j=i+1;
  69. while (j < n && p2 [j+1].y == p2 [j].y)
  70. ++j;
  71. intr [++nintr]=ii (p2 [i].y, p2 [j].y);
  72. i=j-1;
  73. }
  74. for (i=1; i <= nintr; ++i)
  75. {
  76. for (j=1; j <= n; ++j)
  77. {
  78. a1=p [j];
  79. a2=p [j+1];
  80. if ((a1.y > a2.y) || (a1.y == a2.y && a1.x > a2.x))
  81. {
  82. aux=a1;
  83. a1=a2;
  84. a2=aux;
  85. }
  86. if (a1.y >= intr [i].y2) continue;
  87. if (a2.y <= intr [i].y1) continue;
  88. segm [i] [++nrsegm [i]]= (mp (xmijloc (j, j+1, i), j));
  89. }
  90. sort (segm [i]+1, segm [i]+1+nrsegm [i]);
  91. }
  92. }
  93.  
  94. inline bool exista (ii punct)
  95. {
  96. int s=1<<10, i;
  97. for (i=0; s; s >>= 1)
  98. {
  99. if (n < i+s) continue;
  100. if (punct.y < p2 [i+s].y) continue;
  101. if (punct.y == p2 [i+s].y && punct.x < p2 [i+s].x) continue;
  102. i+=s;
  103. }
  104. if (i == 0) return false;
  105. if (p2 [i] == punct) return true;
  106. return false;
  107. }
  108.  
  109. inline int semn (ii punct, int ind)
  110. {
  111. ii p1, p2;
  112. if (p [ind].y > p [ind+1].y)
  113. {
  114. p1=p [ind+1];
  115. p2=p [ind];
  116. }
  117. else
  118. {
  119. p1=p [ind];
  120. p2=p [ind+1];
  121. }
  122. //long long a=p1.y-p2.y, b=p2.x-p1.x, c=(long long)p1.x*p2.y - (long long)p1.y*p2.x;
  123. int a=p1.y-p2.y, b=p2.x-p1.x, c=p1.x*p2.y - p1.y*p2.x;
  124. return a*punct.x+b*punct.y+c;
  125. }
  126.  
  127. int cb1 (int yy)
  128. {
  129. int s=1<<10, i;
  130. for (i=0; s; s >>= 1)
  131. if (nintr >= i+s && intr [i+s].y1 <= yy)
  132. i+=s;
  133. return i;
  134. }
  135.  
  136. int cb2 (ii punct, int k)
  137. {
  138. if (semn (punct, segm [k] [1].second) > 0) return 0;
  139. if (semn (punct, segm [k] [nrsegm [k]].second) < 0) return 0;
  140. int s=1<<10, i;
  141. for (i=0; s; s >>= 1)
  142. {
  143. if (nrsegm [k] < i+s) continue;
  144. if (semn (punct, segm [k] [i+s].second) <= 0)
  145. i+=s;
  146. }
  147. if (semn (punct, segm [k] [i+s].second) == 0) return 1;
  148. return i;
  149. }
  150.  
  151. inline bool interior (ii punct)
  152. {
  153. if (punct.y < intr [1].y1 || punct.y > intr [nintr].y2) return false;
  154. if (exista (punct))
  155. return true;
  156. int k=cb1 (punct.y);
  157. if (cb2 (punct, k) % 2 == 1)
  158. return true;
  159. if (punct.y == intr [k].y1 && k != 1)
  160. if (cb2 (punct, k-1) % 2 == 1)
  161. return true;
  162. return false;
  163. }
  164.  
  165. int rez ()
  166. {
  167. int i, num=0;
  168. ii punct;
  169. for (i=1; i <= m; ++i)
  170. {
  171. scanf ("%d%d", &punct.x, &punct.y);
  172. if (interior (punct))
  173. printf("DA\n");
  174. else
  175. printf("NU\n");
  176. }
  177. return num;
  178. }
  179.  
  180. int main ()
  181. {
  182. freopen ("punctinpoligonsimplu.in", "r", stdin);
  183. freopen ("punctinpoligonsimplu.out", "w", stdout);
  184. scan ();
  185. bucatele ();
  186. rez ();
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement