考虑每个数的贡献,即满足区间第k大为该数的区间个数。其实就是对于两边比起大的数,两边卡出比该数大的个数为k - 1.
用链表维护,考虑从小到大枚举,枚举完成后删除该点,保证每次枚举链表中都是比其大的数。
O(n*k)
写的挫
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 5e5 + 10;
typedef long long ll;
struct Node
{
int next, pre;
int pos;
Node(){
next = -1;
pre = -1;
}
}ls[MAXN];
int n, a[MAXN], k;
void init()
{
for (int i = 0; i < MAXN; i++){
ls[i].pre = ls[i].next = -1;
}
}
int main()
{
int T;
scanf("%d", &T);
while (T--){
init();
scanf("%d%d", &n, &k);
a[0] = 0;
a[n + 1] = n + 1;
for (int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++){
ls[a[i]].pre = a[i - 1];
ls[a[i]].next = a[i &#