PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000133. Đếm đoạn có trung vị bằng m (Median on Segments)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm đoạn có trung vị bằng m (Median on Segments)

Median on Segments — General Case

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a sequence of positive integers a1,a2,…,ana_1,a_2,\ldots,a_n and a target value mm. A contiguous subarray is identified by indices 1≤l≤r≤n1\le l\le r\le n and consists of the elements al,al+1,…,ara_l,a_{l+1},\ldots,a_r.

The median of a subarray is determined by sorting its values in non-decreasing order and choosing the middle value. When its length is even, choose the left of the two middle values, not their arithmetic mean.

For instance, sorting [4,2,7,5][4,2,7,5] gives [2,4,5,7][2,4,5,7], so its median equals 44. The median of a one-element subarray equals that element.

Count the pairs (l,r)(l,r) for which the median of [l,r][l,r] is exactly mm. Different index pairs represent different subarrays even if their values coincide. The answer may exceed the 32-bit signed integer range.

Input

The first line contains n,mn,m. The second line contains exactly nn integers a1,…,ana_1,\ldots,a_n.

Output

Print one integer: the number of contiguous subarrays whose median equals mm.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤m≤2000001\le m\le 200000; 1≤ai≤2000001\le a_i\le 200000 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤25001\le n\le 2500; 1≤m≤2000001\le m\le 200000; 1≤ai≤2000001\le a_i\le 200000 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤m≤2000001\le m\le 200000; 1≤ai≤2000001\le a_i\le 200000 for every 1≤i≤n1\le i\le n.

Examples

Example 1

Input

5 4
1 4 5 60 4

Output

8

Explanation

Exactly eight index pairs qualify: (1,3)(1,3), (1,4)(1,4), (1,5)(1,5), (2,2)(2,2), (2,3)(2,3), (2,5)(2,5), (4,5)(4,5), (5,5)(5,5). For instance, [1,4][1,4] contains 1,4,5,601,4,5,60 and has left median 44. The subarray [1,2][1,2] contains 1,41,4 and has median 11, so it is excluded.

Example 2

Input

3 1
1 1 1

Output

6

Explanation

All three values are 11, so every subarray has median 11. There are three length-1 subarrays, two length-2 subarrays, and one length-3 subarray, giving 3+2+1=63+2+1=6.