fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5. #define int long long
  6.  
  7. const int N = 2e5, oo = 2e18, MOD = 1e9+7;
  8.  
  9.  
  10. void solve() {
  11. int n; cin >> n;
  12. vector<int> a(n), b(n);
  13. for (int i = 0; i < n; i++) {
  14. cin >> a[i];
  15. }
  16. for (int i = 0; i < n; i++) {
  17. cin >> b[i];
  18. }
  19.  
  20. vector<int> last(32, -1);
  21. vector<pair<int, int>> ans;
  22. for (int i = 0; i < n; i++) {
  23. for (int j = 0; j < 32; j++) {
  24. if (((b[i] >> j) & 1) && !((a[i] >> j) & 1)) {
  25. if (last[j] == -1) {
  26. cout << -1 << endl;
  27. return;
  28. }
  29. ans.push_back({last[j], i});
  30. }
  31. if ((a[i] >> j) & 1) last[j] = i;
  32. }
  33. }
  34. sort(ans.begin(), ans.end());
  35. if (ans.empty()) {
  36. cout << 0 << endl;
  37. return;
  38. }
  39. int res = 0;
  40. int l = ans[0].first, r = ans[0].second;
  41. for (int i = 1; i < ans.size(); i++) {
  42. auto [cl, cr] = ans[i];
  43.  
  44. if (cl > r) {
  45. res += (r - l);
  46. l = cl, r = cr;
  47. } else {
  48. r = cr;
  49. }
  50. }
  51. res += (r - l);
  52. cout << res << endl;
  53. // cout << (max(0ll, r - l)) << endl;
  54. }
  55.  
  56.  
  57. signed main() {
  58. ios_base::sync_with_stdio(false);
  59. cin.tie(NULL); cout.tie(NULL);
  60. // #ifndef ONLINE_JUDGE
  61. // freopen("input.txt", "r", stdin);
  62. // freopen("output.txt", "w", stdout);
  63. // #endif
  64. int t; t = 1;
  65. cin >> t;
  66. while (t--) solve();
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
0