nrk009

sum-of-bitwise-and-of-all-subarrays

Jul 14th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  4. #define ll long long
  5. #define ld long double
  6. #define pb push_back
  7. #define fe first
  8. #define se second
  9. #define st(x) sort(x.begin(),x.end())
  10. #define rst(x) sort(x.rbegin(), x.rend())
  11. #define all(x) x.begin(),x.end()
  12. long double pi = 3.14159265358979323;
  13.  
  14. const double EPS = 1e-12;
  15. const int N = 1e6 + 5;
  16. const int mod = 1e9 + 7;
  17.  
  18.  
  19.  
  20.  
  21. int main()
  22. {
  23.     fast;
  24.     ll n;
  25.     cin >> n ;
  26.     ll id[60] = {};
  27.     ll val ;
  28.     ll ans = 0 ;
  29.     for(int i = 1 ;  i <= n ;i++ )
  30.     {
  31.         cin >> val;
  32.         for(int j = 0 ; j < 60 ; j++ )
  33.         {
  34.             ll num = 1LL << j;
  35.             if(num & val)
  36.             {
  37.                 ans += (ll)pow(2,j)*i;
  38.                 id[j] = i ;
  39.             }
  40.             else if(id[j])
  41.             {
  42.                 ans += (ll)pow(2,j)*id[j];
  43.             }
  44.         }
  45.     }
  46.     cout << ans;
  47.    
  48.     //time complexity O(n*60);
  49.     //space complexity O(60);
  50.    
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.    
  74.     return 0;
  75.    
  76. }
Add Comment
Please, Sign In to add comment