tinycodinglm / cf /cf-1005-E2.jsonl
winapiadmin's picture
Upload folder using huggingface_hub
1f3a244 verified
|
Raw History Blame
3.92 kB
{"type": "session", "harness": "opencode", "id": "cf-1005-E2", "name": "Codeforces: Median on Segments (General Case Edition)"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Median on Segments (General Case Edition) (Rating: 2400)\nTags: sortings\n\nYou are given an integer sequence $a_1, a_2, \\dots, a_n$.\n\nFind 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$.\n\nThe 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.\n\nFor 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.\n\nWrite 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$.\n\nInput: 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.\n\nThe second line contains an integer sequence $a_1, a_2, \\dots, a_n$ ($1 \\le a_i \\le 2\\cdot10^5$).\n\nOutput: Print the required number.\n\nExamples:\nInput: 5 4\n1 4 5 60 4\nOutput: 8\n\nInput: 3 1\n1 1 1\nOutput: 6\n\nInput: 15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3\nOutput: 97\n"}}
{"type": "message", "message": {"role": "assistant", "content": "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$$$).\nThe 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.\nIn 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$$$.\nLet'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$$$.\nEach 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]$$$.\nSince 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).\nThis solution works in $$$O(n)$$$."}}