tinycodinglm / cf /cf-1005-E1.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame Contribute Delete
3.73 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 (Permutations Edition) (Rating: 1800)
Tags: sortings

You are given a permutation $p_1, p_2, \dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence.

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

The median of a sequence is the value of the 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 the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$.

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

The second line contains a permutation $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once.

Output: Print the required number.

Examples:
Input: 5 4
2 4 5 3 1
Output: 4

Input: 5 5
1 2 3 4 5
Output: 1

Input: 15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
Output: 48

Assistant

The segment $$$p[l \dots r]$$$ has median equals $$$m$$$ if and only if $$$m$$$ belongs to it and $$$less=greater$$$ or $$$less=greater-1$$$, where $$$less$$$ is number of elements in $$$p[l \dots r]$$$ that strictly less than $$$m$$$ and $$$greater$$$ is number of elements in $$$p[l \dots r]$$$ that strictly greater than $$$m$$$. Here we've used a fact that $$$p$$$ is a permutation (on $$$p[l \dots r]$$$ there is exactly one occurrence of $$$m$$$).
In other words, $$$m$$$ belongs $$$p[l \dots r]$$$ and the value $$$greater-less$$$ equals $$$0$$$ or $$$1$$$.
Calculate prefix sums $$$sum[0 \dots n]$$$, where $$$sum[i]$$$ the value $$$greater-less$$$ on the prefix of the length $$$i$$$ (i.e. on the subarray $$$p[0 \dots i-1]$$$). For fixed value $$$r$$$ it is easy to calculate number of such $$$l$$$ that $$$p[l \dots r]$$$ is suitable. At first, check that $$$m$$$ met on $$$[0 \dots r]$$$. Valid values $$$l$$$ are such indices that: no $$$m$$$ on $$$[0 \dots l-1]$$$ and $$$sum[l]=sum[r]$$$ or $$$sum[r]=sum[l]+1$$$.
Let's maintain number of prefix sums $$$sum[i]$$$ to the left of $$$m$$$ for each value. We can use just a map $$$c$$$, where $$$c[s]$$$ is number of such indices $$$l$$$ that $$$sum[l]=s$$$ and $$$l$$$ is to the left of $$$m$$$.
So for each $$$r$$$ that $$$p[0 \dots r]$$$ contains $$$m$$$ do ans += c[sum] + c[sum - 1], where $$$sum$$$ is the current value $$$greater-less$$$.
Time complexity is $$$O(n \log n)$$$ if a standard map is used or $$$O(n)$$$ if classical array for $$$c$$$ is used (remember about possible negative indices, just use an offset).