tinycodinglm / cf /cf-1005-E2.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame
3.92 kB
opencode trace
System You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code.

User

Problem: Median on Segments (General Case Edition) (Rating: 2400)
Tags: sortings

You are given an integer sequence $a_1, a_2, \dots, a_n$.

Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$.

The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.

For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence.

Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$.

Input: The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) — the length of the given sequence and the required value of the median.

The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$).

Output: Print the required number.

Examples:
Input: 5 4
1 4 5 60 4
Output: 8

Input: 3 1
1 1 1
Output: 6

Input: 15 2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Output: 97

Assistant

Let's define a function greaterCount($$$m$$$) — number of subarrays with median greater or equal than $$$m$$$. In this case, the answer on the problem is greaterCount($$$m$$$) $$$-$$$ greaterCount($$$m + 1$$$).
The subarray $$$a[l \dots r]$$$ has median greater or equal than $$$m$$$, if and only if $$$notLess>less$$$, where $$$notLess$$$ is the number equal or greater than $$$m$$$ elements, and $$$less$$$ is the number of less than $$$m$$$ elements.
In other words, instead of processing $$$a[l \dots r]$$$ you can use the sequence $$$x[l \dots r]$$$ containing $$$-1$$$ or/and $$$+1$$$. An element $$$x[i]=-1$$$, if $$$a[i]<m$$$. An element $$$x[i]=+1$$$, if $$$a[i] \ge m$$$. Now, the median of $$$a[l \dots r]$$$ is greater or equal than $$$m$$$ if and only if $$$x[l] + x[l+1] + \dots + x[r] > 0$$$.
Let's iterate over $$$a$$$ from left to right. Maintain the current partial sum $$$sum=x[0]+x[1]+\dots+x[i]$$$. Additionally, in the array $$$s$$$ let's maintain the number of partial sum for each its value. It means that before increase of $$$i$$$ you should do s[sum]++. So if $$$i$$$ is the index of the right endpoint of a subarray (i.e. $$$r=i$$$), then number of suitable indices $$$l$$$ is number of such $$$j$$$ that $$$x[0]+x[1]+\dots+x[j]<sum$$$. In other words, find sum of all $$$s[w]$$$, where $$$w<sum$$$ — it is exactly number of indices with partial sum less than $$$sum$$$.
Each time partial sum changes on $$$-1$$$ or $$$+1$$$. So the value "sum of all $$$s[w]$$$, where $$$w<sum$$$" is easy to recalculate on each change. If you decrease $$$sum$$$, just subtract the value $$$s[sum]$$$. If you increase $$$sum$$$, before increasing just add $$$s[sum]$$$.
Since indices in $$$s$$$ can be from $$$-n$$$ to $$$n$$$, you can use 0-based indices using an array $$$s[0 \dots 2 \cdot n]$$$. In this case, initialize $$$sum$$$ as $$$n$$$ but not as $$$0$$$ (it makes $$$sum$$$ to be non-negative on each step).
This solution works in $$$O(n)$$$.