题目链接

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
 
 
题意:输入n,k  然后输入n个数(1~n的排列),求所有子区间的第k大数之和(长度小于k的区间值为0)。
 
思路:考虑每个数的贡献值,对于每个数求有多少个区间的第k大数是它,所以我们需要求出每个数向左比它大的k个数的位置,向右比它大的k个数的位置,根据这些位置就可以算出有多少区间它是第k大(这个就不详细说了),那么怎么求向左向右比它大的k数的位置呢,直接求复杂度比较高,我们可以按照1~n的数值大小顺序求向左向右的比它大的k个数的位置,用链表维护,算完i之后,将i从链表里面删除,那么剩余的都是比i+1大的数了,所以直接向左右遍历k个数即可。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=5e5+;
struct Node
{
int x;
int l,r;
}t[N];
int a[N], L[], R[]; int main()
{
int T,n,k; cin>>T;
while(T--)
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&t[i].x);
t[i].l=i-; t[i].r=i+;
a[t[i].x]=i;
}
t[].l=-; t[n].r=-; LL ans=;
for(int i=;i<=n;i++)
{
//ans=0;
int pos=a[i];
int tot=;///右
for(int j=t[pos].r;j!=-;j=t[j].r)
{
R[++tot]=j;
if(tot>=k) break;
}
R[]=tot;
if(tot<k) R[tot+]=n+; tot=; ///左
for(int j=t[pos].l;j!=-;j=t[j].l)
{
L[++tot]=j;
if(tot>=k) break;
}
L[]=tot;
if(tot<k) L[tot+]=; int l=t[pos].l;
int r=t[pos].r;
if(l>) t[l].r=r;
if(r>) t[r].l=l; for(int j=L[]; j>=; j--)
{
if(j>=k) continue;
int x=k--j;
if(x>R[]) continue;
int l=L[j]-L[j+];
if(j==) l=pos-L[];
int r=R[x+]-R[x];
if(x==) r=R[]-pos;
ans+=(LL)l*(LL)r*(LL)t[pos].x;
}
}
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 二分,链表

    Kanade's sum Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th larg ...

  4. 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 ...

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

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

  6. HDU - 6058 Kanade's sum

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

  7. 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 ...

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

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

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

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

  10. 2017 Multi-University Training Contest - Team 3 Kanade's sum hd6058

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6058 题目: Kanade's sum Time Limit: 4000/2000 MS (J ...

随机推荐

  1. yum源解释。。。。。

    主要说明下如何配置linux上的本地yum源,主要关于一些原理上的说明. 1.yum是什么,yum源又是什么       在windows上安装一个软件,我们可以通过360管家.因为360管家提供了软 ...

  2. nginx命令启动及选项

    [root@ke]# nginx -h  #this help [root@ke]# nginx -t  #检查配置文件的语法 [root@ke]# nginx -T  #检查配置文件的语法并输出 [ ...

  3. MySQL误删数据

    首先看下mysql误删数据排名最前的几种是: 1.误删文件 2.误删库.表 3.错误全表删除 / 更新 4.升级操作失误 都来看看你命中过几个,hoho. 简单说下我亲手造的一个大事故吧. 那大概是一 ...

  4. linux查询公网ip

    查询公网ip[出口IP] 1.curl icanhazip.com 2.curl ipecho.net/plain 3.curl www.trackip.net/i

  5. redis应用实践

    晚些时候自己总结,先贴出看到的一篇文章. https://www.cnblogs.com/NiceCui/p/7794659.html

  6. Redhat/Centos6.x安装Chrome

    由于Chrome对rhel6.x不在支持发布版本,只能安装chromium版本! 01.下载地址 http://people.centos.org/hughesjr/chromium/6/x86_64 ...

  7. 必须知道的Spring Boot中的一些Controller注解

    这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...

  8. 2018上IEC计算机高级语言(C)作业 第3次作业

    2018上IEC计算机高级语言(C)作业 第3次作业 一.例程调试(20分) 调试下面2个例程,各位同学调试用自己的学号模3(即除以3取余数)加1序号及该序号乘以2的题.写明调试过程,如错误现象(如给 ...

  9. 20175316盛茂淞 2018-2019-2 《Java程序设计》第3周学习总结

    20175316盛茂淞 2018-2019-2 <Java程序设计>第3周学习总结 教材学习内容总结 1.1.编程语言的几个发展阶段: 面向机器语言 面向过程语言 面向对象语言 2.1.类 ...

  10. 2019.02.16 spoj Query on a tree IV(链分治)

    传送门 题意简述: 捉迷藏强化版(带有边权,可以为负数) 思路:好吧这次我们不用点分树,我们用听起来更屌的链分治. 直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog ...