{"type": "session", "harness": "opencode", "id": "cf-1005-E1", "name": "Codeforces: Median on Segments (Permutations 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 (Permutations Edition) (Rating: 1800)\nTags: sortings\n\nYou 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.\n\nFind 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$.\n\nThe 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.\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 the median of $p_l, p_{l+1}, \\dots, p_r$ is exactly the given number $m$.\n\nInput: 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.\n\nThe 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.\n\nOutput: Print the required number.\n\nExamples:\nInput: 5 4\n2 4 5 3 1\nOutput: 4\n\nInput: 5 5\n1 2 3 4 5\nOutput: 1\n\nInput: 15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9\nOutput: 48\n"}} {"type": "message", "message": {"role": "assistant", "content": "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$$$).\nIn other words, $$$m$$$ belongs $$$p[l \\dots r]$$$ and the value $$$greater-less$$$ equals $$$0$$$ or $$$1$$$.\nCalculate 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$$$.\nLet'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$$$.\nSo 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$$$.\nTime 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)."}}