HDU 6058 Kanade's sum 二分,链表
Kanade's sum
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
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]
5 2
1 2 3 4 5
题解:
我们只要求出对于一个数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 二分,链表的更多相关文章
- HDU 6058 - Kanade's sum | 2017 Multi-University Training Contest 3
/* HDU 6058 - Kanade's sum [ 思维,链表 ] | 2017 Multi-University Training Contest 3 题意: 给出排列 a[N],求所有区间的 ...
- hdu 6058 Kanade's sum(模拟链表)
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
- 【链表】2017多校训练三 HDU 6058 Kanade's sum
acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...
- HDU - 6058 Kanade's sum
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...
- 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 ...
- hdu 6058 Kanade's sum (计算贡献,思维)
题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...
- HDU6058 Kanade's sum(思维 链表)
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu6058 Kanade's sum 区间第k大
/** 题目:Kanade's sum 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题意:给定[1,n]的排列,定义f(l,r,k)表示区间[l ...
随机推荐
- PL/SQL配置访问多个不同IP的oracle
第一步:打开Oracle的tnsnames.ora文件.添加 test = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = ...
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...
- MHA脚本master_ip_failover.pl(三)
#!/usr/bin/env perl use strict;use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh ...
- 解开Future的神秘面纱之任务执行
此文承接之前的博文 解开Future的神秘面纱之取消任务 补充一些任务执行的一些细节,并从全局介绍程序的运行情况. 任务提交到执行的流程 前文我们已经了解到一些Future的实现细节,这里我们来梳理一 ...
- 刷题总结——art2(ssoj)
题目: 题解: o(n)复杂度扫一遍再用一个stack维护就可以了·····mdzz这道题都不会做·· 代码: #include<iostream> #include<cstdio& ...
- UVa10491 Cows and Cars
#include<iostream> #include<cstdio> #include<algorithm> int main(){ double a,b,c; ...
- SGU 106 在区间范围内的线性方程解个数
题意:求解方程ax+by+c=0,在区间x1->x2和y1->y2的解的个数. 看似简单,真心a的不容易啊! 开始跪于第8组数据,原因是没用long long !后来改了,跪于12组,超时 ...
- SGU 105 数学找规律
观察一下序列,每3个数一组,第一个数余1,不能,加第二个数后整除(第二个数本身余2),第三数恰整除.一行代码的事.011011011.... #include<iostream> usin ...
- Codeforces 713D Animals and Puzzle(二维ST表+二分答案)
题目链接 Animals and Puzzle 题意 给出一个1e3 * 1e3的01矩阵,给出t个询问,每个询问形如x1,y1,x2,y2 你需要回答在以$(x1, y1)$为左上角,$(x1, ...
- windows symbol server调试
linux下gdb强大的调试功能让人印象深刻,一直以为windows下调试可执行程序非常让人头痛.经一些高人指点后知道原来windows下还有symbol server这种调试工具 参见下面两个文档 ...