Codeforces Round 1039 (Div. 2)
E2
Submedians (Hard Version)
Codeforces
2600
原题
← E1F →
翻译中…

This is the hard version of the problem. The only difference is that in this version, you are asked to find a subarray for all submedians.

翻译中…

You can make hacks only if both versions of the problem are solved.

翻译中…

An integer $v$ is a median of an array $b$ of length $m$ if and only if:

翻译中…
  • $v$ is greater than or equal to at least $\lceil \frac{m}{2} \rceil$ elements of the array, and
  • $v$ is less than or equal to at least $\lceil \frac{m}{2} \rceil$ elements of the array.
翻译中…

For instance:

翻译中…
  • the only median of $[9, 3, 7]$ is $7$,
  • the medians of $[5, 3, 7, 9]$ are $5$, $6$, and $7$, and
  • the only median of $[2, 2, 2]$ is $2$.
翻译中…

You're given an integer $k$ and an array $a_1, \ldots, a_n$ of integers between $1$ and $n$.

翻译中…

An integer $v$ from $1$ to $n$ is said to be a submedian if there exists at least one pair of indices $(l, r)$ such that

翻译中…
  • $1 \leq l \leq r \leq n$,
  • $r - l + 1 \geq k$,
  • $v$ is a median of the subarray $[a_l, \ldots, a_r]$.
翻译中…

Find all submedians and for each of them, find any corresponding pair of indices $(l, r)$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 50\,000$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 300\,000$).

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases doesn't exceed $300\,000$.

翻译中…

Output

翻译中…

For each test case, output your answer in the following format.

翻译中…

On the first line, output $c$, the number of submedians.

翻译中…

On the $i$-th of the following $c$ lines, output three integers $v_i$, $l_i$, and $r_i$ such that

翻译中…
  • $r_i - l_i + 1 \geq k$, and
  • $v_i$ is one of the medians of the subarray $[a_{l_i}, \ldots, a_{r_i}]$.
翻译中…

Each submedian should be reported exactly once, that is, integers $v_1, \ldots, v_c$ must be pairwise distinct. The order in which they are reported does not matter.

翻译中…

If there are many solutions, you can print any of them.

翻译中…

Note

翻译中…

In the first test case, the subarrays of length at least $k = 3$ are

翻译中…
  • $(l = 1, r = 3)$: $[4, 1, 2]$ whose only median is $2$,
  • $(l = 2, r = 4)$: $[1, 2, 4]$ whose only median is $2$, and
  • $(l = 1, r = 4)$: $[4, 1, 2, 4]$ whose medians are $2$, $3$, and $4$.
翻译中…

In the second test case, one possible output is

翻译中…
  • $(l = 4, r = 5)$: $[2, 1]$ whose medians are $1$ and $2$,
  • $(l = 1, r = 5)$: whose only median is $2$,
  • $(l = 3, r = 4)$: $[3, 2]$ whose medians are $2$ and $3$.
翻译中…

All of these subarrays are indeed of length at least $2$. Note that it can be proven that no subarray of length at least $2$ admits $4$ or $5$ as a median.

翻译中…

样例

Input #1
7
4 3
4 1 2 4
5 2
1 2 3 2 1
5 3
1 2 3 2 1
5 3
1 1 2 5 3
1 1
1
2 1
2 1
4 1
1 2 1 3
Output #1
3
2 1 4
3 1 4
4 1 4
3
1 4 5
2 1 5
3 3 4
1
2 2 4
3
1 1 3
2 2 4
3 3 5
1
1 1 1
2
1 1 2
2 1 2
3
1 1 1
2 2 2
3 4 4
InputCopy OutputCopy
题目信息
题号
2128E2
时间限制
3 s
内存限制
512 MB
标签
二分
构造
数据结构
数学
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。