Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

namespace __gnu_pbds{
          typedef tree<long long,
                       null_type,
                       less<long long>,
                       rb_tree_tag,
                       tree_order_statistics_node_update> ordered_set;
}
//multiset:-
//greater_equal<long long>
//less_equal<long long>

//set:-
//greater<long long>
//less<long long>
using namespace __gnu_pbds;
//A.order_of_key number of elements strictly less than k
//*A.find_by_order

// Miscellanous
#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()

// Loops
#define loop(i,s,e) for (int (i)=(s);(i)<(e);++(i))
#define loope(i,s,e) for (int (i)=(s);(i)<=(e);++(i))

// Constants
#define PI 3.1415926535897932384626
const int mod = 1000000007;

// Containers
typedef pair<int, int>	    pii;
typedef pair<ll, ll>	    pll;
typedef vector<int>		    vi;
typedef vector<ll>		    vl;
typedef vector<pii>		    vpii;
typedef vector<pll>		    vpll;
typedef vector<vi>		    vvi;
typedef vector<vl>		    vvl;
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
#define out(a) for(int i = 0; i<a.size(); i++) cout<<a[i]<<" ";

// Debug
void _print(int t) {cerr << t;}
void _print(ll t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);

template <class T> void _print(unordered_set <T> v);
template <class T> void _print(unordered_multiset <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(unordered_map <T, V> v);
template <class T, class V> void _print(unordered_multimap <T, V> v);
template <class T, class V> void _print(map <T, V> v);
template <class T, class V> void _print(multimap <T, V> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(unordered_multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

#ifndef ONLINE_JUDGE
#define deb(x) cerr << #x << " = ";  _print(x); cerr << endl;
#else
#define deb(x)
#endif
//stoi to_string
#define py	    cout<<"YES\n";
#define pn	    cout<<"NO\n";
#define pnl	    cout<<"\n";




ll  ith_set_bit_ct_0_r(ll r,ll i){
    ll period = (1LL<<(i+1));
    ll freq = (1LL<<i);
    ll ct1 = ((r+1)/period)*freq+max(0LL,(r+1)%period-freq)  ;
    return ct1;
}
ll  ith_set_bit_ct_l_r(ll l  ,ll r,ll i){
  if(l==0)return ith_set_bit_ct_0_r(r,i);  
  return ith_set_bit_ct_0_r(r,i)-ith_set_bit_ct_0_r(l-1,i);
}

ll xor1_to_n(ll n ){
  if(n%4==0)return n;
  else if(n%4==1)return 1;
  else if(n%4==2)return n+1;
  else return 0;
}

class DSU_by_rank{
  public:
  vector<int>rank;
  vector<int>par;
  DSU_by_rank(int n){
    rank.resize(n+1,0);
    par.resize(n+1,0);
    for(int i = 0;i<=n ;i++)par[i] =i;
  }
  int find(int node){
    if(par[node] == node)return node;
    else return par[node]  = find(par[node]);
  }
  void unionr(int x, int y){
    int rx = find(x);
    int ry = find(y);

    if(rank[rx]<rank[ry])swap(rx,ry);
    par[ry]  = rx;
    if(rank[rx]==rank[ry])rank[rx]++;
  }
};

class DSU_by_size{
  public:
  vector<int>size;
  vector<int>par;
  DSU_by_size(int n){
      size.resize(n+1,1);
      par.resize(n+1,0);
      for (int  i = 0; i < n+1; i++)
      {
        /* code */
        par[i]=i;
      }
  }
  int find(int x){
    if(par[x] == x)return x;
    else return par[x] = find(par[x]);
  }
  void unions(int x , int y){
    int rx = find(x);
    int ry = find(y);
    if(size[rx]<size[ry])swap(rx,ry);
    par[ry] = rx;
    size[rx]+= size[ry];
  }
};
void permute(string str)
{
    sort(str.begin(), str.end());

    do {
       cout << str << endl;
    } while (next_permutation(str.begin(), str.end()));
}




bool func(ll x , multiset<ll>&ms1){
    if(ms1.size()==0)return false;

    if(ms1.find(x)!=ms1.end()){
        return true;
    }

    // deb(x);
    // deb(ms1);

    if(x==1){
        if(ms1.find(x)==ms1.end())return false;
        else{
            return true;
        }
    }
    bool f1 = func(x/2,ms1);
    if(f1)ms1.erase(ms1.find(x/2));
    bool f2 = func((x+1)/2,ms1);
    if(f2) ms1.erase(ms1.find((x+1)/2));

    if(f1 && f2){
        ms1.insert(x);
        return true;
    }else{
        return false;
    }    

}


void solve()
{
    ll n,m;
    cin>>n>>m;
    multiset<ll>s1;
    multiset<ll>s2;

    loop(i,0,n){
        ll x;
        cin>>x;
        s1.insert(x);
    }

    loop(i,0,m){
        ll x;
        cin>>x;
        s2.insert(x);
    }
    vl temp;
    for(auto it : s2){
        if(s1.count(it)){
            temp.push_back(it);
            s1.erase(s1.find(it));
        }
    }   
    for(auto it : temp){
        s2.erase(s2.find(it));
    }

    for(auto it : s2){
        // deb(it);
        if(s1.size()<2){
            cout<<"No\n";
            return;
        }
        if((func(it,s1))==false){
            cout<<"No\n";
            return;
        }else{
            s1.erase(s1.find(it));
        }
    }

    if(s1.size()==0)cout<<"Yes\n";
    else cout<<"No\n";

}


#define N 300005
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.precision(10); cout.setf(ios::fixed);
  int t;
  cin>>t;    
  while(t--){
    solve();
  }
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/*
*/

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

namespace __gnu_pbds{
          typedef tree<long long,
                       null_type,
                       less<long long>,
                       rb_tree_tag,
                       tree_order_statistics_node_update> ordered_set;
}
//multiset:-
//greater_equal<long long>
//less_equal<long long>

//set:-
//greater<long long>
//less<long long>
using namespace __gnu_pbds;
//A.order_of_key number of elements strictly less than k
//*A.find_by_order

// Miscellanous
#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()

// Loops
#define loop(i,s,e) for (int (i)=(s);(i)<(e);++(i))
#define loope(i,s,e) for (int (i)=(s);(i)<=(e);++(i))

// Constants
#define PI 3.1415926535897932384626
const int mod = 1000000007;

// Containers
typedef pair<int, int>	    pii;
typedef pair<ll, ll>	    pll;
typedef vector<int>		    vi;
typedef vector<ll>		    vl;
typedef vector<pii>		    vpii;
typedef vector<pll>		    vpll;
typedef vector<vi>		    vvi;
typedef vector<vl>		    vvl;
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
#define out(a) for(int i = 0; i<a.size(); i++) cout<<a[i]<<" ";

// Debug
void _print(int t) {cerr << t;}
void _print(ll t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);

template <class T> void _print(unordered_set <T> v);
template <class T> void _print(unordered_multiset <T> v);
template <class T> void _print(set <T> v);
template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(unordered_map <T, V> v);
template <class T, class V> void _print(unordered_multimap <T, V> v);
template <class T, class V> void _print(map <T, V> v);
template <class T, class V> void _print(multimap <T, V> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(unordered_set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(unordered_multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(multimap <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

#ifndef ONLINE_JUDGE
#define deb(x) cerr << #x << " = ";  _print(x); cerr << endl;
#else
#define deb(x)
#endif
//stoi to_string
#define py	    cout<<"YES\n";
#define pn	    cout<<"NO\n";
#define pnl	    cout<<"\n";




ll  ith_set_bit_ct_0_r(ll r,ll i){
    ll period = (1LL<<(i+1));
    ll freq = (1LL<<i);
    ll ct1 = ((r+1)/period)*freq+max(0LL,(r+1)%period-freq)  ;
    return ct1;
}
ll  ith_set_bit_ct_l_r(ll l  ,ll r,ll i){
  if(l==0)return ith_set_bit_ct_0_r(r,i);  
  return ith_set_bit_ct_0_r(r,i)-ith_set_bit_ct_0_r(l-1,i);
}

ll xor1_to_n(ll n ){
  if(n%4==0)return n;
  else if(n%4==1)return 1;
  else if(n%4==2)return n+1;
  else return 0;
}

class DSU_by_rank{
  public:
  vector<int>rank;
  vector<int>par;
  DSU_by_rank(int n){
    rank.resize(n+1,0);
    par.resize(n+1,0);
    for(int i = 0;i<=n ;i++)par[i] =i;
  }
  int find(int node){
    if(par[node] == node)return node;
    else return par[node]  = find(par[node]);
  }
  void unionr(int x, int y){
    int rx = find(x);
    int ry = find(y);

    if(rank[rx]<rank[ry])swap(rx,ry);
    par[ry]  = rx;
    if(rank[rx]==rank[ry])rank[rx]++;
  }
};

class DSU_by_size{
  public:
  vector<int>size;
  vector<int>par;
  DSU_by_size(int n){
      size.resize(n+1,1);
      par.resize(n+1,0);
      for (int  i = 0; i < n+1; i++)
      {
        /* code */
        par[i]=i;
      }
  }
  int find(int x){
    if(par[x] == x)return x;
    else return par[x] = find(par[x]);
  }
  void unions(int x , int y){
    int rx = find(x);
    int ry = find(y);
    if(size[rx]<size[ry])swap(rx,ry);
    par[ry] = rx;
    size[rx]+= size[ry];
  }
};
void permute(string str)
{
    sort(str.begin(), str.end());

    do {
       cout << str << endl;
    } while (next_permutation(str.begin(), str.end()));
}




bool func(ll x , multiset<ll>&ms1){
    if(ms1.size()==0)return false;

    if(ms1.find(x)!=ms1.end()){
        return true;
    }

    if(x==1){
        if(ms1.find(x)==ms1.end())return false;
        else{
            return true;
        }
    }

    bool f1 = func(x/2,ms1);
    if(f1)ms1.erase(ms1.find(x/2));
    else return false;
    bool f2 = func((x+1)/2,ms1);
    if(f2) ms1.erase(ms1.find((x+1)/2));
    else return false;
    
    if(f1 && f2){
        ms1.insert(x);
        return true;
    }else{
        return false;
    }    

}


void solve()
{
    ll n,m;
    cin>>n>>m;
    multiset<ll>s1;
    multiset<ll>s2;

    loop(i,0,n){
        ll x;
        cin>>x;
        s1.insert(x);
    }

    loop(i,0,m){
        ll x;
        cin>>x;
        s2.insert(x);
    }
    vl temp;
    for(auto it : s2){
        if(s1.count(it)){
            temp.push_back(it);
            s1.erase(s1.find(it));
        }
    }   
    for(auto it : temp){
        s2.erase(s2.find(it));
    }

    for(auto it : s2){
        if(s1.size()<2){
            cout<<"No\n";
            return;
        }
        if((func(it,s1))==false){
            cout<<"No\n";
            return;
        }else{
            s1.erase(s1.find(it));
        }
    }

    if(s1.size()==0)cout<<"Yes\n";
    else cout<<"No\n";

}


#define N 300005
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout.precision(10); cout.setf(ios::fixed);
  int t;
  cin>>t;    
  while(t--){
    solve();
  }
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


/*
*/