fork download
  1. #include <bits/stdc++.h>
  2. #include <fstream>
  3. const double pi = acos(-1.0);
  4. const long long MOD = 1e9+7;
  5. #define ll long long
  6. #define ld long double
  7. #define sortx(X) sort(X.begin(),X.end());
  8. #define el "\n"
  9. #define yes cout<<"YES"<<el;
  10. #define no cout<<"NO"<<el;
  11. #define imp cout<<"-1"<<el;
  12. #define Y_N if(cond){yes}else{no}
  13. #define MW_YH ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  14. using namespace std;
  15. // int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; // 4 directions
  16. // int dx[]={0,0,1,-1,-1,-1,1,1}; int dy[]={1,-1,0,0,-1,1,-1,1}; // 8 directions
  17. // ----------------------------Functions---------------------------- //
  18. void Mohammed_Waleed(){
  19. MW_YH
  20. // #ifndef ONLINE_JUDGE
  21. // freopen("moocast.in", "r", stdin);
  22. // freopen("moocast.out", "w", stdout);
  23. // #endif
  24. }
  25. bool knowBit(long long val, long long ind){
  26. return (val>>ind)&1;
  27. }
  28. // ----------------------------------------------------------------- //
  29. void solution(){
  30. ll n;
  31. cin >> n;
  32. vector<ll>a(n),b(n);
  33. for(int i = 0; i < n; i++) cin >> a[i];
  34. for(int i = 0; i < n; i++) cin >> b[i];
  35. vector<ll>last(31,-1);
  36. deque<pair<ll,ll>>p;
  37. ll res = 0;
  38. bool cond = true;
  39. for(int i = 0; i < n; i++)
  40. {
  41. for(int j = 0; j <= 30; j++) if(knowBit(a[i],j)) last[j]=i;
  42. if((a[i]&b[i])!=b[i])
  43. {
  44. for(int j = 0; j <= 30; j++)
  45. {
  46. if(knowBit(b[i],j)&& (!knowBit(a[i],j)))
  47. {
  48. if(last[j]==-1)
  49. {
  50. imp
  51. return;
  52. }
  53. else
  54. {
  55. p.push_back({last[j],i});
  56. }
  57. }
  58. }
  59. }
  60. }
  61. sortx(p);
  62. ll l = 0, r = 0;
  63. if(p.size())
  64. {
  65. ll l = p.front().first, r = p.front().second;
  66. p.pop_front();
  67. while (!p.empty())
  68. {
  69. if(p.front().first<=r)
  70. {
  71. r = max(r,p.front().second);
  72. }
  73. else
  74. {
  75. res += r-l;
  76. l = p.front().first, r = p.front().second;
  77. }
  78. p.pop_front();
  79. }
  80. res += r-l;
  81. }
  82. cout << res << el;
  83. }
  84.  
  85. int main(){
  86. Mohammed_Waleed();
  87. int ntimes = 1;
  88. cin >> ntimes;
  89. while (ntimes--)
  90. {
  91. solution();
  92. }
  93. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0