Ankit_132

D2

Oct 22nd, 2023
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         ll int n, m;
  14.         cin>>n>>m;
  15.  
  16.         vector<int> a(n), b(n);
  17.  
  18.         a[0] = 1;
  19.         for(int i=1; i<n; i++)
  20.             cin>>a[i];
  21.  
  22.         for(auto &e: b)     cin>>e;
  23.  
  24.         multiset<int> msta, mstb;
  25.         for(auto e: a)      msta.insert(e);
  26.         for(auto e: b)      mstb.insert(e);
  27.  
  28.         ll int cnt = 0;
  29.         int sz = n;
  30.  
  31.         ll int ans = 0;
  32.  
  33.         while(msta.size())
  34.         {
  35.             if(sz == 1)
  36.             {
  37.                 if(*mstb.rbegin() > *msta.begin())
  38.                 {
  39.                     ll int x = *mstb.rbegin();
  40.                     cnt++;
  41.  
  42.                     cnt = n - cnt;
  43.  
  44.                     if(m >= x)
  45.                         ans = cnt*1ll*(x-1) + (cnt+1)*1ll*(m-x+1);
  46.                     else
  47.                         ans = cnt*1ll*m;
  48.                 }
  49.                 else
  50.                 {
  51.                     cnt = n - cnt;
  52.                     ans = cnt*1ll*m;
  53.                 }
  54.  
  55.                 break;
  56.             }
  57.  
  58.             if(mstb.upper_bound(*msta.rbegin()) != mstb.end())
  59.             {
  60.                 cnt++;
  61.                 mstb.erase(mstb.upper_bound(*msta.rbegin()));
  62.             }
  63.  
  64.             msta.erase(--msta.end());
  65.             sz--;
  66.         }
  67.  
  68.         cout<<ans<<"\n";
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment