fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define ll long long
  5. #define nl '\n'
  6. //#define int long long
  7.  
  8. void file()
  9. {
  10. #ifndef ONLINE_JUDGE
  11. freopen("in.txt", "r", stdin);
  12. freopen("out.txt", "w", stdout);
  13. #endif
  14. }
  15.  
  16. bool getBit(ll num, int idx)
  17. {
  18. return ((num >> idx) & 1);
  19. }
  20.  
  21. signed main()
  22. {
  23. ios::sync_with_stdio(false);
  24. cin.tie(nullptr);
  25. file();
  26.  
  27. int n;
  28. cin >> n;
  29.  
  30.  
  31. vector<vector<ll>>pr(n+1,vector<ll> (32,0));
  32.  
  33. for (int i = 0;i<n;i++)
  34. {
  35. int x;
  36. cin >> x;
  37.  
  38. for (int j = 0;j<32;j++)
  39. {
  40. pr[i+1][j]+= getBit(x,j);
  41. }
  42.  
  43. }
  44.  
  45. for (int i = 1;i<=n;i++)
  46. {
  47. for (int j = 0;j<32;j++)
  48. {
  49. pr[i][j]+=pr[i-1][j];
  50. }
  51. }
  52.  
  53.  
  54. int q;
  55. cin >> q;
  56.  
  57.  
  58. while (q--)
  59. {
  60. int l,r;
  61. cin >> l >> r;
  62.  
  63. ll res = 0;
  64. for (int i = 0;i<32;i++)
  65. {
  66. ll prf = pr[r][i]-pr[l-1][i];
  67.  
  68. ll x= prf,y = (r-l+1)-prf;
  69. res+=(1LL<<i) * x*y;
  70. }
  71. cout << res<<nl;
  72. }
  73.  
  74.  
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty