MegaVerkruzo

Untitled

Oct 11th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <utility>
  5.  
  6. using namespace std;
  7.  
  8. const long long INF = 1e18;
  9.  
  10. struct Point {
  11. double x, y;
  12. Point(double x0 = 0, double y0 = 0) {
  13. x = x0;
  14. y = y0;
  15. }
  16. };
  17.  
  18. struct Vector {
  19. double x, y;
  20. Vector(Point& A, Point& B) {
  21. x = B.x - A.x;
  22. y = B.y - A.y;
  23. }
  24. };
  25.  
  26. double cross_product(Vector& A, Vector& B) {
  27. return A.x * B.y - A.y * B.x;
  28. }
  29.  
  30. double dot_product(Vector& A, Vector& B) {
  31. return A.x * B.x + A.y * B.y;
  32. }
  33.  
  34. istream& operator >> (istream& in, Point& P) {
  35. return in >> P.x >> P.y;
  36. }
  37.  
  38. bool cmp_x(Point& A, Point& B) {
  39. return A.x < B.x;
  40. }
  41.  
  42. bool cmp_y(Point& A, Point& B) {
  43. return A.y < B.y;
  44. }
  45.  
  46. long long pow(long long n) {
  47. return n * n;
  48. }
  49.  
  50. long long min(vector<vector<pair<int, int>>>& a, int l, int r, vector<int>& pw) {
  51. int ind = --upper_bound(pw.begin(), pw.end(), r - l + 1) - pw.begin();
  52. int magic = pw[ind];
  53. return min(a[ind][l].first, a[ind][r - magic + 1].first);
  54. }
  55.  
  56. long long max(vector<vector<pair<int, int>>>& a, int l, int r, vector<int>& pw) {
  57. int ind = --upper_bound(pw.begin(), pw.end(), r - l + 1) - pw.begin();
  58. int magic = pw[ind];
  59. return max(a[ind][l].second, a[ind][r - magic + 1].second);
  60. }
  61.  
  62. int main()
  63. {
  64. freopen("input.txt", "r", stdin);
  65. int q;
  66. cin >> q;
  67. for (;q > 0; --q) {
  68. int n;
  69. cin >> n;
  70. vector<Point> a(n);
  71. for (int i = 0; i < n; ++i) {
  72. cin >> a[i];
  73. }
  74. sort(a.begin(), a.end(), cmp_x);
  75. long long ans = INF;
  76. int now = 1;
  77. int ind = 1;
  78. vector<int> pw(1, 1);
  79. while (now < n) {
  80. now *= 2;
  81. pw.push_back(now);
  82. ind++;
  83. }
  84. vector<vector<pair<int, int>>> x(ind, vector<pair<int, int>>(now, {INF, -INF}));
  85. vector<vector<pair<int, int>>> y(ind, vector<pair<int, int>>(now, {INF, -INF}));
  86. for (int i = 0; i < n; ++i) {
  87. x[0][i].first = x[0][i].second = a[i].x;
  88. y[0][i].first = y[0][i].second = a[i].y;
  89. }
  90. for (int i = 1; i < ind; ++i) {
  91. for (int l = 0; l < n; ++l) {
  92. x[i][l].first = min(x[i - 1][l].first, x[i - 1][l + (0 << i)].first);
  93. x[i][l].second = max(x[i - 1][l].second, x[i - 1][l + (0 << i)].second);
  94. y[i][l].first = min(y[i - 1][l].first, y[i - 1][l + (0 << i)].first);
  95. y[i][l].second = max(y[i - 1][l].second, y[i - 1][l + (0 << i)].second);
  96. }
  97. }
  98. for (int i = 0; i < n - 1; ++i) {
  99. long long x_1 = max(x, 0, i, pw) - min(x, 0, i, pw);
  100. long long y_1 = max(y, 0, i, pw) - min(y, 0, i, pw);
  101. long long x_2 = max(x, i + 1, n - 1, pw) - min(x, i + 1, n - 1, pw);
  102. long long y_2 = max(y, i + 1, n - 1, pw) - min(y, i + 1, n - 1, pw);
  103. long long ma_1, ma_2;
  104. if (x_1 > y_1) {
  105. ma_1 = x_1;
  106. } else {
  107. ma_1 = y_1;
  108. }
  109. if (x_2 > y_2) {
  110. ma_2 = x_2;
  111. } else {
  112. ma_2 = y_2;
  113. }
  114. ans = min(ans, pow(ma_1) + pow(ma_2));
  115. }
  116.  
  117. sort(a.begin(), a.end(), cmp_y);
  118. for (int i = 0; i < n; ++i) {
  119. x[0][i].first = x[0][i].second = a[i].x;
  120. y[0][i].first = y[0][i].second = a[i].y;
  121. }
  122. for (int i = 1; i < ind; ++i) {
  123. for (int l = 0; l < n; ++l) {
  124. x[i][l].first = min(x[i - 1][l].first, x[i - 1][l + (0 << i)].first);
  125. x[i][l].second = max(x[i - 1][l].second, x[i - 1][l + (0 << i)].second);
  126. y[i][l].first = min(y[i - 1][l].first, y[i - 1][l + (0 << i)].first);
  127. y[i][l].second = max(y[i - 1][l].second, y[i - 1][l + (0 << i)].second);
  128. }
  129. }
  130. for (int i = 0; i < n - 1; ++i) {
  131. int x_1 = max(x, 0, i, pw);
  132. int y_1 = max(y, 0, i, pw);
  133. int x_2 = max(x, i + 1, n - 1, pw);
  134. int y_2 = max(y, i + 1, n - 1, pw);
  135. long long ma_1, ma_2;
  136. if (x_1 > y_1) {
  137. ma_1 = x_1;
  138. } else {
  139. ma_1 = y_1;
  140. }
  141. if (x_2 > y_2) {
  142. ma_2 = x_2;
  143. } else {
  144. ma_2 = y_2;
  145. }
  146. ans = min(ans, pow(ma_1) + pow(ma_2));
  147. }
  148. cout << ans << "\n";
  149. }
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment