题目描述
给定一个数列 a1,a2,⋯,an,对每个二元组 (l,r),1≤l≤r≤n,按以下方法定义一个偏序关系.
记
对于 (l1,r1),(l2,r2)
- 若 S(l1,r1)<S(l2,r2),则 (l1,r1)<(l2,r2)
- 若 S(l1,r1)=S(l2,r2), 且 l1>l2,则 (l1,r1)<(l2,r2)
- 若 S(l1,r1)=S(l2,r2),且 l1=l2, 且 r1>r2, 则 (l1,r1)<(l2,r2)
把每个二元组 (l,r),1≤l≤r≤n,按以上定义从小到大排序, 询问其中第 大的二元组 (从 1 开始数, 第 1 大的就是最大的).
5≤n≤10^5
1≤k≤10
−10^5≤ai≤10^5
输入格式
第一行读入 n,k
第二行读入 n 个整数表示 ai
样例
样例输入
5 10
0 1 0 1 0
样例输出
3 5
First, we calculate the prefix sum S[i]. And We use i as the rightmost endpoint of the subsequence and make decision on the leftmost endpoint j(1<=j<i). If S[j] is the p-th smallest of S[1...i-1], then S[i]-S[j] should be the p-th largest of S[i]-S[1...i-1]. The only thing we need to do is to find out the former k smallest S[j] and keep records of them including three values--- S[i]-S[j], i and j. Organize them as a class and sort them according to the rule stated in the problem. And find out the k-th largest subsequece.
And now we only have one more problem to tackle. How cdo we find out the former k smallest S[j] for each rightmost endpoint i in constant amount of time, so that we can find out all the former k smallest S[j] for all i in O(n)?
Suppose we heve just discussed i as the rightmost endpoint and have successfully found out the former k smallest S[j]. Now we dive into the situation---i+1. We have a new element to consider, namely S[i+1]. We have to compare S[i+1] with each of the former k smallest S[j], and replace one of them as the new small S[j'] if possible.