<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, nums: List[int], k: int) -> int:
        c=-1
        q=-1
        pos=[]
        n=len(bin(max(nums))[2:])
        # print(n)
        for i in range(len(nums)):
            if(nums[i] & k == k):
                q=i
                if(c==-1):
                    c=i
            else:
                if(c!=-1):
                    pos.append((c,q))
                    c=-1
                    q=-1
        if(c!=-1):
            pos.append((c,q))
        # print(pos)
        ans=0
        for i,j in pos:
            cur=[{} for _ in range(n)]
            pre=i
            for p in range(i,j+1):
                # print('nnn')
                q=bin(nums[p])[2:].zfill(n)
                fi=''
                for oo in range(n):
                    if(q[oo]=='1'):
                        cur[oo][1]=cur[oo].get(1,0)+1
                    else:
                        cur[oo][0]=cur[oo].get(0,0)+1
                    if(cur[oo].get(0,0)>0):
                        fi+='0'
                    else:
                        fi+='1'
                pp=pre
                # print(cur)
                while(pp<=p and fi!='' and int(fi,2)==k):
                    # print(pp,p,'combo ')
                    ans+=(j-p+1)
                    fi=''
                    q=bin(nums[pp])[2:].zfill(n)
                    for oo in range(n):
                        if(q[oo]=='1'):
                            cur[oo][1]-=1
                        else:
                            cur[oo][0]-=1
                        if(cur[oo].get(0,0)>0):
                            fi+='0'
                        elif(cur[oo].get(1,0)>0):
                            fi+='1'
                    pp+=1
                    # print('mm',fi)
                pre=pp
                # print(cur)
                
        # print(pos)
        return ans
3265