Kanade's sum

Problem Description
Give you an array A[1..n]of length n.

Let f(l,r,k) be the k-th largest element of A[l..r].

Specially , f(l,r,k)=0 if r−l+1<k.

Give you k , you need to calculate ∑nl=1∑nr=lf(l,r,k)

There are T test cases.

1≤T≤10

k≤min(n,80)

A[1..n] is a permutation of [1..n]

∑n≤5∗105

 
Input
There is only one integer T on first line.

For each test case,there are only two integers n,k on first line,and the second line consists of n integers which means the array A[1..n]

 
Output
For each test case,output an integer, which means the answer.
 
Sample Input
1

5 2

1 2 3 4 5

 
Sample Output
30
 

题解:

  我们只要求出对于一个数x左边最近的k个比他大的和右边最近k个比他大的,扫一下就可以知道有几个区间的k大值是x.

  我们考虑从大到小插入空位,每次维护一个链表,链表里只有>=x的数,那么往左往右找只要暴力跳k次,删除也是O(1)的。

  时间复杂度:O(nk)

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e6+, M = 1e3+,inf = 2e9,mod = 1e9 + ;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int n,k,a[N],pos[N],lef[N],righ[N],L[N],R[N];
set<int > G;
set<int >:: iterator it,itt;
void insers(int x,int y) {
int tmp = L[x];
L[y] = tmp;
R[y] = x; R[tmp] = y;
L[x] = y;
}
int main() {
int T;
T = read();
while(T--) {
G.clear();
n = read();
k = read();
for(int i = ; i <= n; ++i) {
a[i] = read();
pos[a[i]] = i;
}
if(k == ) {
puts("");
continue;
}
G.insert();
G.insert(n+);
L[] = -;
R[] = n+;
L[n+] = ;
R[n+] = -;
for(int i = n; i > n - k + ; --i) { it = (G.lower_bound(pos[i]));
insers(*it,pos[i]);
G.insert(pos[i]);
} LL ans = ;
for(int i = n-k+; i >= ; --i) { it = (G.lower_bound(pos[i]));
int po = *it;
for(int j = ; j <= k; ++j) lef[j] = -; lef[] = pos[i];
for(int j = ,h = L[po]; j <= k && h != -; ++j, h = L[h]) {
lef[j] = h;
}
po = *it;
righ[] = pos[i];
for(int j = ,h = po; j <= k && h != -; ++j, h = R[h]) {
righ[j] = h;
if(lef[k-j+] != -)
ans += 1LL*i*(lef[k-j] - lef[k - j + ]) * (righ[j] - righ[j-]);
}
insers(*it,pos[i]);
G.insert(pos[i]);
}
printf("%lld\n",ans);
}
return ;
}

HDU 6058 Kanade's sum 二分,链表的更多相关文章

  1. HDU 6058 - Kanade's sum | 2017 Multi-University Training Contest 3

    /* HDU 6058 - Kanade's sum [ 思维,链表 ] | 2017 Multi-University Training Contest 3 题意: 给出排列 a[N],求所有区间的 ...

  2. hdu 6058 Kanade's sum(模拟链表)

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. HDU 6058 Kanade's sum —— 2017 Multi-University Training 3

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. 【链表】2017多校训练三 HDU 6058 Kanade's sum

    acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...

  5. HDU - 6058 Kanade's sum

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...

  6. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  7. hdu 6058 Kanade's sum (计算贡献,思维)

    题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...

  8. HDU6058 Kanade's sum(思维 链表)

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  9. hdu6058 Kanade's sum 区间第k大

    /** 题目:Kanade's sum 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题意:给定[1,n]的排列,定义f(l,r,k)表示区间[l ...

随机推荐

  1. hdu2084

    老题目了 #include <stdio.h> int main(){ ][]; int i,j,max; int c,n; scanf("%d",&c); w ...

  2. 有大神告诉我为什么pymysql导入失败

    import json import requests import pymysql url = 'https://xueqiu.com/v4/statuses/public_timeline_by_ ...

  3. 【二叉搜索树】hdu 3791

    http://acm.hdu.edu.cn/showproblem.php?pid=3791 [注意] 是看树的形态是否一样,而不是中序遍历的结果 [Accepted] #include<ios ...

  4. 在echars上发布的半圆环形图

    http://gallery.echartsjs.com/editor.html?c=xBJvoMcPfz&v=1

  5. hdu 4965 矩阵快速幂 矩阵相乘性质

    Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Jav ...

  6. 2017-10-28-morning-清北模拟赛

    T1 立方数(cubic) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是 ...

  7. tar [options] [list of file]

    打包:zcvf 解压:zxvf -c 创建新档案文件 -x 从档案文件中解出文件(释放文件) -v (verbose)显示tar命令执行的详细过程 -f 指定目标为一个文件而不是一个设备 -z 调用g ...

  8. 《深入理解mybatis原理》 Mybatis初始化机制详解

    对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XML配置 ...

  9. linux 每天备份mysql数据

    原文:http://www.open-open.com/code/view/1433587311890 前言: 如果你的数据库文件较大,可能备份的时候需要占用很多服务器资源,所以站长要尽量选择访客最少 ...

  10. 开源天气预报api整理

    高德天气:https://lbs.amap.com/api/webservice/guide/api/weatherinfo/? github上对开源api的整理:https://github.com ...