fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25. vector<int> b;
  26.  
  27.  
  28. bool isPossible(int n, int m, int mid){
  29.  
  30. for(int i=0; i<n; i++){
  31. int val = a[i];
  32. int minR = a[i]-mid;
  33. int maxR = a[i]+mid;
  34.  
  35. auto maxCt = lower_bound(begin(b), end(b), maxR) - begin(b);
  36. auto minCt = lower_bound(begin(b), end(b), minR) - begin(b);
  37.  
  38. if((maxCt<m && b[maxCt] == maxR) || (minCt<m && b[minCt] == minR)) {
  39. continue;
  40. }
  41.  
  42. if(maxCt - minCt <= 0) return false;
  43.  
  44. }
  45.  
  46. return true;
  47. }
  48.  
  49. int consistency(int n, int m){
  50.  
  51. int s = 0, e = INF;
  52. int ans = INF;
  53. while(s<=e){
  54. int mid = s + (e-s)/2;
  55.  
  56. if(isPossible(n, m, mid)){
  57. ans = min(ans, mid);
  58. e = mid-1;
  59. }
  60. else s = mid+1;
  61. }
  62.  
  63. return ans;
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. //* a => Cities, b => towers
  75.  
  76. bool isPoss(int n, int m, int mid){
  77.  
  78.  
  79. int x = -1, y = -1;
  80. vector<int> intervals;
  81.  
  82. for(int i=0; i<m; i++){
  83. int p = b[i]-mid;
  84. int q = b[i]+mid;
  85.  
  86. if(x == -1 && y==-1){
  87. x = p;
  88. y = q;
  89. }
  90. else{
  91. if(y >= p){
  92. y = max(y,q);
  93. }
  94. else{
  95. intervals.push_back(x);
  96. intervals.push_back(y);
  97. }
  98. }
  99. }
  100. intervals.push_back(x);
  101. intervals.push_back(y);
  102.  
  103. int ct = 0;
  104. for(int i=0; i<n; i++){
  105. int j = lower_bound(begin(intervals), end(intervals), a[i]) - begin(intervals);
  106. if(j==intervals.size()) continue;
  107.  
  108. if(j==a[i]) ct++;
  109. if(j&1 && a[i] <= j){
  110. ct++;
  111. }
  112. }
  113. return ct == n;
  114. }
  115.  
  116. int practice(int n, int m){
  117.  
  118. int s = 0, e = INF;
  119.  
  120. while(s<e){
  121. int mid = s + (e-s)/2;
  122. if(isPoss(n, m, mid)){
  123. e = mid;
  124. }
  125. else s = mid+1;
  126. }
  127.  
  128. return e;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. void solve() {
  138.  
  139. int n, m;
  140. cin>>n>>m;
  141. a.resize(n);
  142. b.resize(m);
  143.  
  144. for(int i=0; i<n; i++) cin >> a[i];
  145. for(int i=0; i<m; i++) cin >> b[i];
  146. // cout << consistency(n, m) << endl;
  147.  
  148. cout << practice(n, m) << endl;
  149.  
  150.  
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157. int32_t main() {
  158. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  159.  
  160. int t = 1;
  161. cin >> t;
  162. while (t--) {
  163. solve();
  164. }
  165.  
  166. return 0;
  167. }
Success #stdin #stdout 0s 5320KB
stdin
2
3 2
-2 2 4
-3 0
5 3
1 5 10 14 17
4 11 15
stdout
2000000001
2000000001