Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <utility>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <assert.h>
  8. #include <stack>
  9. using namespace std;
  10.  
  11. const int MAXN = 100005;
  12.  
  13. struct Vec {
  14. long long x, y;
  15. int id;
  16.  
  17. explicit Vec(long long _x = 0, long long _y = 0) {
  18. x = _x;
  19. y = _y;
  20. }
  21.  
  22. inline void scan() {
  23. long long a, b;
  24. //scanf("%lli %lli", &a, &b);
  25. cin >> a >> b;
  26. x = a + 1000;
  27. y = b + 1000;
  28. }
  29.  
  30. void print() {
  31. cout << x << " " << y << "\n";
  32. }
  33.  
  34. inline long long len2() {
  35. return x * x + y * y;
  36. }
  37.  
  38. long long operator *(Vec const &a) const {
  39. return x * a.x + y * a.y;
  40. }
  41.  
  42. Vec operator +(Vec &a) {
  43. return Vec(x + a.x, y + a.y);
  44. }
  45.  
  46. inline Vec operator -(Vec &a) {
  47. return Vec(x - a.x, y - a.y);
  48. }
  49. };
  50.  
  51. //vector<Points>
  52. vector<Vec> v[400][400];
  53.  
  54. int main() {
  55. std::ios::sync_with_stdio(false);
  56. ios_base::sync_with_stdio(false);
  57.  
  58. //freopen("zzz.in", "r", stdin);
  59. //freopen("zzz.out", "w", stdout);
  60.  
  61. int n, m;
  62. cin >> n >> m;
  63.  
  64. //int N = (int)sqrt(n) + 1;
  65. int N = 320;
  66. //cout << N << "\n";
  67.  
  68. for (int i = 0; i < n; i++) {
  69. Vec point;
  70. point.scan();
  71. point.id = i;
  72.  
  73. int num_x = point.x / N;
  74. int num_y = point.y / N;
  75.  
  76. v[num_x][num_y].push_back(point);
  77.  
  78. //cout << num_x << " " << num_y << "\n";
  79. }
  80.  
  81. for (int i = 0; i < m; i++) {
  82. Vec point;
  83. point.scan();
  84.  
  85. int num_x = point.x / N;
  86. int num_y = point.y / N;
  87.  
  88. long long min_dist = 1e9;
  89. int best_id = -1;
  90.  
  91. for (int j = max(0, num_x); j <= min(399, num_x + 1); j++)
  92. for (int k = max(0, num_y); k <= min(399, num_y + 1); k++)
  93. for (int q = 0; q < (int)v[j][k].size(); q++)
  94. if ((point - v[j][k][q]).len2() < min_dist) {
  95. min_dist = (point - v[j][k][q]).len2();
  96. best_id = v[j][k][q].id;
  97. }
  98.  
  99. assert(best_id != -1);
  100. cout << best_id + 1 << "\n";
  101. }
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement