#FT0000133. Đếm đoạn có trung vị bằng m (Median on Segments)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đế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 and a target value . A contiguous subarray is identified by indices and consists of the elements .
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 gives , so its median equals . The median of a one-element subarray equals that element.
Count the pairs for which the median of is exactly . 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 . The second line contains exactly integers .
Output
Print one integer: the number of contiguous subarrays whose median equals .
Subtasks
- Subtask 1 (20 points): ; ; for every .
- Subtask 2 (30 points): ; ; for every .
- Subtask 3 (50 points): ; ; for every .
Examples
Example 1
Input
5 4
1 4 5 60 4
Output
8
Explanation
Exactly eight index pairs qualify: , , , , , , , . For instance, contains and has left median . The subarray contains and has median , so it is excluded.
Example 2
Input
3 1
1 1 1
Output
6
Explanation
All three values are , so every subarray has median . There are three length-1 subarrays, two length-2 subarrays, and one length-3 subarray, giving .