fork download
  1.  
  2.  
  3. #ifdef Asaad
  4. #include "cp.h"
  5. #define debug(...) _dbg_many(#__VA_ARGS__, __VA_ARGS__)
  6. #define here cerr << "LINE " << __LINE__ << "\n"
  7. #else
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10. /*
  11.   #include <ext/pb_ds/assoc_container.hpp>
  12.   #include <ext/pb_ds/tree_policy.hpp>
  13.   using namespace __gnu_pbds;
  14.   template<class T>
  15.   using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  16.   */
  17. #define debug(...)
  18. #define here
  19. #endif
  20.  
  21.  
  22.  
  23. #define ll long long
  24. #define int long long
  25. #define all(x) x.begin(), x.end()
  26. #define siz(x) ((int)x.size())
  27. #define yes cout << "YES\n"
  28. #define no cout << "NO\n"
  29. #define f first
  30. #define s second
  31. #define eb emplace_back
  32. #define pb push_back
  33.  
  34.  
  35. const int mod = 1e9+7;
  36. const int N = 200009;
  37. const long long inf = 1e18+12309138;
  38. double eps = 1e-9;
  39.  
  40.  
  41.  
  42.  
  43.  
  44. void tc () {
  45. //nb=
  46. int n; cin >> n;
  47. vector<int> a(n), b(n);
  48. vector<int> mp(100, -1);
  49. for (int& x : a) cin >> x;
  50. for (int& x : b) cin >> x;
  51. int ans = 0;
  52. bool ok = 1;
  53. vector<int> pref(n+2);
  54.  
  55. for (int i=0; i<n; i++) {
  56. int z = inf;
  57. for (int j=0; j<31; j++) {
  58. if ( (a[i]&(1LL<<j)) ) mp[j] = i;
  59. }
  60. for ( int j=0; j<31; j++ ) {
  61. if ( (b[i]&(1LL<<j)) == 0 ) continue;
  62. if ( mp[j] == -1 ) {
  63. ok = 0;
  64. break;
  65. }
  66. z = min(z, mp[j]);
  67. }
  68. if (!ok) break;
  69. if ( z < i ){
  70. pref[z]++;
  71. pref[i]--;
  72. }
  73. }
  74. if (!ok) {
  75. cout << "-1\n";
  76. return;
  77. }
  78. int res = 0;
  79. for(int i=0;i<=n+1;++i){
  80. if(i)
  81. pref[i]+=pref[i-1];
  82.  
  83. if(pref[i])res++;
  84. }
  85. cout << res << "\n";
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. signed main () {
  93. ios::sync_with_stdio(false);
  94. cin.tie(0);
  95. //freopen( "input.txt", "r", stdin );
  96. //freopen( "output.txt", "w", stdout );
  97. //cout << fixed << setprecision(9);
  98. //pre();
  99. int t=1;
  100. cin >> t;
  101.  
  102. for (int i=1; i<=t; i++) {
  103. #ifdef Asaad
  104. auto start = chrono::high_resolution_clock::now();
  105. //cout << "---Case " << i << " Start---\n\n";
  106. #endif
  107.  
  108. tc();
  109.  
  110.  
  111. #ifdef Asaad
  112. auto end = chrono::high_resolution_clock::now();
  113. //cout << "---Case " << i << " End---\n";
  114. cerr << "Time #" << i << ": " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << "ms" << endl;
  115. //cout << "--------------\n";
  116. #endif
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
Success #stdin #stdout 0.01s 5280KB
stdin
C1 nucpa online 2026
stdout
Standard output is empty