<p>You are given an array <code>nums</code> of size <code>n</code> consisting of <strong>distinct </strong>integers from <code>1</code> to <code>n</code> and a positive integer <code>k</code>.</p>

<p>Return <em>the number of non-empty subarrays in </em><code>nums</code><em> that have a <strong>median</strong> equal to </em><code>k</code>.</p>

<p><strong>Note</strong>:</p>

<ul>
	<li>The median of an array is the <strong>middle </strong>element after sorting the array in <strong>ascending </strong>order. If the array is of even length, the median is the <strong>left </strong>middle element.

	<ul>
		<li>For example, the median of <code>[2,3,1,4]</code> is <code>2</code>, and the median of <code>[8,4,3,5,1]</code> is <code>4</code>.</li>
	</ul>
	</li>
	<li>A subarray is a contiguous part of an array.</li>
</ul>

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

<pre>
<strong>Input:</strong> nums = [3,2,1,4,5], k = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,3,1], k = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> [3] is the only subarray that has a median equal to 3.
</pre>

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

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], k &lt;= n</code></li>
	<li>The integers in <code>nums</code> are distinct.</li>
</ul>

class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        idx = -1
        for i, x in enumerate(nums):
            if x == k:
                idx = i
                break
        cnt = 1
        # 用 2 sum的办法来遍历左边，然后在右边遍历的时候直接用dict来判定
        # arr 以k为界，左右都统计当前index下比k大的数（可以为负）
        # 比如 arr[idx - 2], 就是k左边两位比k大的情况，如果两个都比k大，那么left_arr[idx - 2] = 2
        arr = [0] * len(nums)
        odd_left = defaultdict(int)
        even_left = defaultdict(int)
        for i in range(idx - 1, -1, -1):
            if nums[i] > k:
                arr[i] = arr[i + 1] + 1
            else:
                arr[i] = arr[i + 1] - 1
            if (idx - i) % 2 == 0:
                even_left[arr[i]] += 1
            else:
                odd_left[arr[i]] += 1
        for i in range(idx + 1, len(nums), 1):
            if nums[i] > k:
                arr[i] = arr[i - 1] + 1
            else:
                arr[i] = arr[i - 1] - 1
        print(arr, odd_left, even_left)
        # 1. fist check if we don't engage right
        for i in range(idx - 1, -1, -1):
            if (idx - i) % 2 == 0:
                if arr[i] == 0:
                    cnt += 1
            else:
                if arr[i] == 1:
                    cnt +=1
        print("check 1: ", cnt)
        # 2. second check if we don't engage left
        for i in range(idx + 1, len(nums), 1):
            if (idx - i) % 2 == 0:
                if arr[i] == 0:
                    cnt += 1
            else:
                if arr[i] == 1:
                    cnt +=1
        print("check 2: ", cnt)
        # 3. both left and right, check for odd length of subarry scenario
        for i in range(idx + 1, len(nums), 1):
            if (i - idx) % 2 == 0:
                # this is even on right, so we check even on left
                cnt += even_left[-arr[i]]
            else:
                # check odd on left
                cnt += odd_left[-arr[i]]
        print("check 3: ", cnt)
        # 4. both left and right, check for even length of subarry scenario
        for i in range(idx + 1, len(nums), 1):
            if (i - idx) % 2 == 0:
                # this is even on right, so we check odd on left
                # plus 1 because in the even length scenario, k is consider as the small one
                # e.g., if on the right, there are 2 greater than k, then we just need one less than k on the left
                cnt += odd_left[-arr[i] + 1]
            else:
                # check odd on left
                cnt += even_left[-arr[i] + 1]
        print("check 4: ", cnt)
        return cnt

408