hdu6058 Kanade's sum 区间第k大
/**
题目:Kanade's sum
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058
题意:给定[1,n]的排列,定义f(l,r,k)表示区间[l,r]内的第k(k <= min(n,80))大的数,如果[l,r]没有k个数,那么为0,。给定一个k,求所有f(l,r,k)的和。
思路:从小到大枚举数x,维护一个>=x的链表,跳k个查询左边>x的k个,右边>x的k个。计算之后,O(1)删除x。
比赛的时候,,刚好反过来了,用的是从大到小用set,二分位置,再迭代器枚举,然后超时了。
eg:
x=1,那么其他数都>x,所以直接x的左边右边各找k个,删除1之后,最小数为2,那么仍然ta的左边和右边各找k个,继续删除,依次操作。 */ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<cmath>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = 1e9+;
const int INF = 0x3f3f3f3f;
const int maxn = 5e5+;
int num[maxn];
int posl[maxn], posr[maxn];
int l[], r[];
int li, ri;
int a[maxn];
void read(int &x){
x = ;
char c = getchar();
while(c < '' || c > '') c = getchar();
while(c >='' && c <= '') x = x * + c - ,c = getchar();
}
int main()
{
int T,cas = ;
cin>>T;
int n, k;
while(T--)
{
read(n),read(k);
int x;
for(int i = ; i <= n; i++){
read(x);
a[i] = x;
num[x] = i;
}
posl[] = -, posr[] = ;///虚拟节点
posl[n+] = n, posr[n+] = -;
for(int i = ; i <= n; i++){
posl[i] = i-;
posr[i] = i+;
}
LL ans = ;
for(int i = ; i <= n; i++){
li = ri = ;
int cnt = k+, pos = num[i];
while(cnt&&pos!=-){
l[li++] = pos;
pos = posl[pos];
cnt--;
}
cnt = k+;
pos = num[i];
while(cnt&&pos!=-){
r[ri++] = pos;
pos = posr[pos];
cnt--;
}
LL cn = ;
for(int j = li-; j >= ; j--){
if(ri<k+-j) break;
int lans = l[j-]-l[j];
int rans = r[k+-j]-r[k-j];
LL tmp = (LL)lans*rans;
if(j==&&k-j==&&lans>&&rans>){
tmp--;
}
cn += tmp;
}
ans += cn*i;
posr[posl[num[i]]] = posr[num[i]];
posl[posr[num[i]]] = posl[num[i]];
}
printf("%lld\n",ans);
}
return ;
}
hdu6058 Kanade's sum 区间第k大的更多相关文章
- HDU3473--Minimum Sum(静态区间第k大)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【ZOJ2112】【整体二分+树状数组】带修改区间第k大
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...
- POJ2104-- K-th Number(主席树静态区间第k大)
[转载]一篇还算可以的文章,关于可持久化线段树http://finaltheory.info/?p=249 无修改的区间第K大 我们先考虑简化的问题:我们要询问整个区间内的第K大.这样我们对值域建线段 ...
- hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...
- HDU 2665 Kth number(主席树静态区间第K大)题解
题意:问你区间第k大是谁 思路:主席树就是可持久化线段树,他是由多个历史版本的权值线段树(不是普通线段树)组成的. 具体可以看q学姐的B站视频 代码: #include<cmath> #i ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- 解决区间第K大的问题的各种方法
例题:http://poj.org/problem?id=2104 最近可能是念念不忘,必有回响吧,总是看到区间第k大的问题,第一次看到是在知乎上有人面试被弄懵了后来又多次在比赛中看到.以前大概是知道 ...
- 可持久化线段树(主席树)(图文并茂详解)【poj2104】【区间第k大】
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=63740442 向大(hei)佬(e)实力学(di ...
随机推荐
- python安装包是出现错误解决
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed/limits.h:168:61: fatal error: limits.h: No such file ...
- 【转】Spring的中IoC及AOP
1. Spring介绍 Spring是轻量级的J2EE应用程序框架.Spring的核心是个轻量级容器(container),实现了IoC(Inversion of Control)模式的容器,Spri ...
- ASP.NET WEB API处理流程
前言:大图请看 http://www.asp.net/posters/web-api/ASP.NET-Web-API-Poster.pdf Web Api Hosting 我们不仅可以通过Web应用程 ...
- struts2+jquery验证注冊用户是否存在
注冊界面 register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&q ...
- RandomForest&ROC
# -*- coding: utf-8 -*- # __author__ = 'JieYao' from biocluster.agent import Agent from biocluster.t ...
- JS回调函数全解析教程(callback)
自学jQuery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是 ...
- Pygame制作答题类游戏的实现
代码地址如下:http://www.demodashi.com/demo/13495.html 概述 个人比较喜欢玩这些答题类的游戏,在这类的游戏中其实存在着一些冷知识在里面.练习pygame的过程中 ...
- 基于shiro+jwt的真正rest url权限管理,前后端分离
代码地址如下:http://www.demodashi.com/demo/13277.html bootshiro & usthe bootshiro是基于springboot+shiro+j ...
- 一些JS常用的方法
/** * JS公用类库文件 */ (function(){ Tools = { W: window, D: document, Postfix: ".php", GetId: f ...
- Ubuntu13.04使用Jlink和ST-Link V2烧写STM32F1x和STM32F4x
近期做研究openpilot和crazyflie2.0,都须要在Linux进行代码的编写和烧录,所以研究了下Linux下如何使用Jlink,还有开源的支持多个仿真器的openocd,可是困难重重.总是 ...