| Codeforces Round 1039 (Div. 2) |
|---|
| Finished |
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:
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
Find all submedians and for each of them, find any corresponding pair of indices $$$(l, r)$$$.
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$$$.
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
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.
74 34 1 2 45 21 2 3 2 15 31 2 3 2 15 31 1 2 5 31 112 12 14 11 2 1 3
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
In the first test case, the subarrays of length at least $$$k = 3$$$ are
In the second test case, one possible output is
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.
| Name |
|---|


