<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return the number of <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code> where the bitwise <code>AND</code> of the elements of the subarray equals <code>k</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong></p>

<p>All subarrays contain only 1&#39;s.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays having an <code>AND</code> value of 1 are: <code>[<u><strong>1</strong></u>,1,2]</code>, <code>[1,<u><strong>1</strong></u>,2]</code>, <code>[<u><strong>1,1</strong></u>,2]</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>Subarrays having an <code>AND</code> value of 2 are: <code>[1,<b><u>2</u></b>,3]</code>, <code>[1,<u><strong>2,3</strong></u>]</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i], k &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def countSubarrays(self, a: List[int], K: int) -> int:
        n = len(a)
        R = [0]*n
        
        if a[n-1] & K == K:
            R[n-1] = 1
            
        i = n-2
        while i >= 0:
            if a[i] & K < K:
                R[i] = 0
            else:
                R[i] = 1 + R[i+1]
            
            i -= 1
            
        # print(R)
        
        i = 0
        j = -1
        cur = -1
        cnt = [0] * 32
        
        res = 0
        while i <= n-1:
            if a[i] & K < K:
                cur = -1
                cnt = [0] * 32
                i += 1            
            else:
                if j < i:
                    j = i
                    cur = a[i]
                    for b in range(32):                        
                        if (a[i] >> b) & 1 == 1:
                            cnt[b] = 1
                # print("i = ", i, "; j = ", j, "; cur = ", cur)
                assert(cur >= K)
                if cur > K:                    
                    while j+1 < n and (a[j+1] & K == K) and (a[j+1] & cur) >= K:
                        cur &= a[j+1]
                        j += 1
                        for b in range(32):
                            if (a[j] >> b) & 1 == 1:
                                cnt[b] += 1     
                        if cur == K:
                            break
                    
                # print("after moving j, we have i = ", i, "; j = ", j, "; cur = ", cur)    
                if cur == K:
                    res += R[j]
                    
                cur = 0
                for b in range(32):
                    if (a[i] >> b) & 1 == 1:
                        cnt[b] -= 1
                    
                    if cnt[b] == j - i:
                        cur ^= (1<<b)
                # print("after removing a[i], we have cur = ", cur)
                i += 1
        return res
                    
                
                
            
3077