<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 SparseTable:
    def __init__(self, nums):
        n = len(nums)
        self.st = [[0] * (int(log2(n)) + 1) for _ in range(n)]

        for i in range(n):
            self.st[i][0] = nums[i]

        p = 1
        while (1 << p) <= n:
            i = 0
            while i + (1 << p) <= n:
                self.st[i][p] = self.st[i][p - 1] & self.st[i + (1 << p - 1)][p - 1]
                i += 1
            p += 1
        print (len(self.st), len(self.st[0]))
    def query(self, l, r):
        p = int(log2(r - l + 1))
        return self.st[l][p] & self.st[r - (1 << p) + 1][p]


class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        n = len(nums)

        ST = SparseTable(nums)

        def bs_left(i, l, k):
            r = n
            while l < r:
                mid = (l + r) >> 1
                if ST.query(i, mid) > k:
                    l = mid + 1
                else:
                    r = mid

            return l

        cnt = 0
        for i in range(n):
            r1 = bs_left(i, i, k)
            if r1 < n and ST.query(i, r1) == k:
                r2 = bs_left(i, r1, k - 1) - 1
                cnt += r2 - r1 + 1

        return cnt
42200