{"problem":{"name":"E1. Median on Segments (Permutations Edition)","description":{"content":"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 ","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":3000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF1005E1"},"statements":[{"statement_type":"Markdown","content":"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.\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\n## Input\n\nThe 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\n## Output\n\nPrint the required number.\n\n[samples]\n\n## Note\n\nIn the first example, the suitable pairs of indices are: $(1, 3)$, $(2, 2)$, $(2, 3)$ and $(2, 4)$.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"你被给定一个排列 $p_1, p_2, dots.h, p_n$。长度为 $n$ 的排列是一个序列，其中每个介于 $1$ 到 $n$ 之间的整数恰好出现一次。\n\n求满足条件的下标对 $(l, r)$（$1 lt.eq l lt.eq r lt.eq n$）的数量，使得子序列 $p_l, p_(l + 1), dots.h, p_r$ 的中位数恰好等于给定的数 $m$。\n\n一个序列的中位数是将序列按非递减顺序排序后位于中间位置的元素的值。如果序列长度为偶数，则使用两个中间元素中靠左的那个。\n\n例如，若 $a = [ 4, 2, 7, 5 ]$，则其中位数为 $4$，因为排序后序列为 $[ 2, 4, 5, 7 ]$，两个中间元素中靠左的是 $4$。序列 $[ 7, 1, 2, 9, 6 ]$ 的中位数为 $6$，因为排序后 $6$ 位于序列中间。\n\n编写一个程序，求满足条件的下标对 $(l, r)$（$1 lt.eq l lt.eq r lt.eq n$）的数量，使得子序列 $p_l, p_(l + 1), dots.h, p_r$ 的中位数恰好等于给定的数 $m$。\n\n第一行包含两个整数 $n$ 和 $m$（$1 lt.eq n lt.eq 2 dot.op 10^5$，$1 lt.eq m lt.eq n$）——给定序列的长度和所需的中位数值。\n\n第二行包含一个排列 $p_1, p_2, dots.h, p_n$（$1 lt.eq p_i lt.eq n$）。每个介于 $1$ 到 $n$ 之间的整数在 $p$ 中恰好出现一次。\n\n请输出所需的数量。\n\n在第一个例子中，符合条件的下标对为：$(1, 3)$、$(2, 2)$、$(2, 3)$ 和 $(2, 4)$。\n\n## Input\n\n第一行包含两个整数 $n$ 和 $m$（$1 lt.eq n lt.eq 2 dot.op 10^5$，$1 lt.eq m lt.eq n$）——给定序列的长度和所需的中位数值。第二行包含一个排列 $p_1, p_2, dots.h, p_n$（$1 lt.eq p_i lt.eq n$）。每个介于 $1$ 到 $n$ 之间的整数在 $p$ 中恰好出现一次。\n\n## Output\n\n请输出所需的数量。\n\n[samples]\n\n## Note\n\n在第一个例子中，符合条件的下标对为：$(1, 3)$、$(2, 2)$、$(2, 3)$ 和 $(2, 4)$。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the length of the permutation.  \nLet $ m \\in \\{1, 2, \\dots, n\\} $ be the target median value.  \nLet $ P = (p_1, p_2, \\dots, p_n) $ be a permutation of $ \\{1, 2, \\dots, n\\} $.  \n\nFor any contiguous subsequence $ P[l:r] = (p_l, p_{l+1}, \\dots, p_r) $ with $ 1 \\leq l \\leq r \\leq n $, define its **median** as the $ \\left\\lceil \\frac{r - l + 1}{2} \\right\\rceil $-th smallest element in the sorted version of $ P[l:r] $.\n\n**Constraints**  \n1. $ 1 \\leq n \\leq 2 \\cdot 10^5 $  \n2. $ 1 \\leq m \\leq n $  \n3. $ P $ is a permutation of $ \\{1, 2, \\dots, n\\} $\n\n**Objective**  \nCount the number of pairs $ (l, r) $ with $ 1 \\leq l \\leq r \\leq n $ such that the median of $ P[l:r] $ is exactly $ m $.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF1005E1","tags":["sortings"],"sample_group":[["5 4\n2 4 5 3 1","4"],["5 5\n1 2 3 4 5","1"],["15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9","48"]],"created_at":"2026-03-03 11:00:39"}}