Ankit_132

D

Jun 27th, 2024
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ff first
  5. #define ss second
  6.  
  7. int main() {
  8.     int t;
  9.     cin>>t;
  10.     while(t--){
  11.         int n, m; cin >> n >> m;
  12.         vector<int> a(n), b(n);
  13.         for(int &x:  a)
  14.             cin >> x;
  15.         map<int, pair<int, int>> diffs;
  16.         map<int, int>  c;
  17.         for(int i = 0; i < n; i++){
  18.             cin >> b[i];
  19.             if(diffs.find(a[i] - b[i]) != diffs.end())
  20.             {
  21.                 if(b[i] < (diffs[a[i] - b[i]]).ff)
  22.                     diffs[a[i] - b[i]] = {b[i], i};
  23.             }
  24.             else
  25.                 diffs[a[i] - b[i]] = {b[i], i};
  26.         }
  27.         vector<int> cc(m);
  28.         for(int &x: cc){
  29.             cin >> x;
  30.             c[x]++;
  31.         }
  32.  
  33.         int ans = 0;
  34.         for(auto &[diff, temp] : diffs)
  35.         {
  36.             auto [bi, i] = temp;
  37.             auto it = c.lower_bound(a[i]);
  38.  
  39.             while(it != c.end())
  40.             {
  41.                 int cnt = (*it).second;
  42.                 int ci = (*it).first;
  43.                 int curr = (ci - bi)/(diff);            ans += 2*curr*cnt;
  44.                 c.erase(it);
  45.                 c[ci - curr*diff] += cnt;
  46.                 it = c.lower_bound(a[i]);
  47.             }
  48.         }
  49.         cout << ans << endl;
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment