Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define _test int _TEST; cin>>_TEST; while(_TEST--)
- int main()
- {
- _test
- {
- ll int n, m;
- cin>>n>>m;
- vector<int> a(n), b(n);
- a[0] = 1;
- for(int i=1; i<n; i++)
- cin>>a[i];
- for(auto &e: b) cin>>e;
- multiset<int> msta, mstb;
- for(auto e: a) msta.insert(e);
- for(auto e: b) mstb.insert(e);
- ll int cnt = 0;
- int sz = n;
- ll int ans = 0;
- while(msta.size())
- {
- if(sz == 1)
- {
- if(*mstb.rbegin() > *msta.begin())
- {
- ll int x = *mstb.rbegin();
- cnt++;
- cnt = n - cnt;
- if(m >= x)
- ans = cnt*1ll*(x-1) + (cnt+1)*1ll*(m-x+1);
- else
- ans = cnt*1ll*m;
- }
- else
- {
- cnt = n - cnt;
- ans = cnt*1ll*m;
- }
- break;
- }
- if(mstb.upper_bound(*msta.rbegin()) != mstb.end())
- {
- cnt++;
- mstb.erase(mstb.upper_bound(*msta.rbegin()));
- }
- msta.erase(--msta.end());
- sz--;
- }
- cout<<ans<<"\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment