fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define gc getchar_unlocked
  4. #define fo(i,n) for(i=0;i<n;i++)
  5. #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  6. #define ll long long
  7. #define si(x) scanf("%d",&x)
  8. #define sl(x) scanf("%lld",&x)
  9. #define ss(s) scanf("%s",s)
  10. #define pi(x) printf("%d\n",x)
  11. #define pl(x) printf("%lld\n",x)
  12. #define ps(s) printf("%s\n",s)
  13. #define pb push_back
  14. #define mp make_pair
  15. #define F first
  16. #define S second
  17. #define all(x) x.begin(), x.end()
  18. #define clr(x) memset(x, 0, sizeof(x))
  19. #define sortall(x) sort(all(x))
  20. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  21. #define PI 3.1415926535897932384626
  22. typedef pair<int, int> pii;
  23. typedef pair<ll, ll> pll;
  24. typedef vector<int> vi;
  25. typedef vector<ll> vll;
  26. typedef vector<pii> vpii;
  27. typedef vector<pll> vpll;
  28. typedef vector<vi> vvi;
  29. typedef vector<vll> vvl;
  30. const int mod = 1000000007;
  31. const int N = 3e5;
  32. const int B = 31;
  33. vi g[N];
  34. int a[B][N], x[N];
  35. ll dp[B][N], pls[B][N];
  36. int agla[B][N];
  37.  
  38. vi fac(int x){
  39. vi ans ;
  40. while(x){
  41. // cout<<x<<" ";
  42. ans.pb(x%2);
  43. x /= 2;
  44. }
  45. x = ans.size();
  46. int i;
  47. Fo(i, x, B) ans.pb(0);
  48. return ans;
  49. }
  50. ll P[B];
  51. int n;
  52. ll solve(int bit, int S){
  53. int i;
  54. ll ans = 0;
  55. int span = S;
  56. // find all possible subarrays starting from ith position
  57. Fo(i, 1, n+1){
  58. span = S;
  59. int j = i + span - 1;
  60. j = min(j, n);
  61. int ni = i;
  62. if(a[bit][i] == 0) {
  63. //agla 1
  64. //since it is 0, we need to find the closest next one
  65. //and decrement the span by that much distance
  66. //and find the possible subarrays from that position
  67. int cum = agla[bit][i] - i + 0;
  68. span -= cum;
  69. if(span <= 0) continue;
  70. ni = agla[bit][i];
  71. j = ni + span - 1;
  72. j = min(j, n);
  73. }
  74. int len = j-ni+1;
  75. int corr = dp[bit][j] - dp[bit][ni-1];
  76. if (!pls[bit][ni]) corr = len - corr;
  77. ans += corr;
  78. if (ans >= mod) ans -= mod;
  79. }
  80. return ans;
  81. }
  82. int main()
  83. {
  84. ios_base::sync_with_stdio(false);
  85. cin.tie(NULL);
  86. int i,k,j,A,b;
  87. cin>>n>>A>>b;
  88. fo(i, n) cin>>x[i+1];
  89.  
  90. P[1] = 1;
  91. Fo(j, 2, B) P[j] = 2*P[j-1];
  92. //build a[i][j]
  93. Fo(i, 1, n+1){
  94. vi bin = fac(x[i]);
  95. Fo(j, 1, B) a[j][i] = bin[j-1];
  96. }
  97. Fo(i, 1, B){
  98. //ith bit
  99. pls[i][0] = 0;
  100. dp[i][0] = 0;
  101. int sahi = 0;
  102. Fo(j, 1, n+1){
  103. if(a[i][j]) sahi = 1-sahi;
  104. pls[i][j] = sahi;
  105. }
  106. Fo(j, 1, n+1) dp[i][j] = pls[i][j] + dp[i][j-1];
  107. agla[i][n+1] = mod;
  108. Fo(j, n, 0){
  109. if (a[i][j]) agla[i][j] = j;
  110. else agla[i][j] = agla[i][j+1];
  111. }
  112. }
  113. ll ans = 0;
  114. //query for subarray length <= b
  115. Fo(i, 1, B){
  116. ans += ( solve(i, b) * P[i] ) % mod;
  117. if (ans >= mod) ans -= mod;
  118. }
  119. //subtract ans for subarray length <= a-1
  120. Fo(i, 1, B){
  121. ans -= ( solve(i, A-1) * P[i] ) % mod;
  122. if (ans >= mod) ans -= mod;
  123. if (ans < 0) ans += mod;
  124. }
  125. cout<<ans<<endl;
  126.  
  127. return 0;
  128. }
Success #stdin #stdout 0s 226112KB
stdin
5 1 1
3 7 100 21 1
stdout
132