2213. K个串

★★★★   输入文件:bzoj_4504.in   输出文件:bzoj_4504.out   简单对比
时间限制:20 s   内存限制:512 MB

【题目描述】

兔子们在玩k个串的游戏。首先,它们拿出了一个长度为n的数字序列,选出其中的一
个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计一次)。
兔子们想知道,在这个数字序列所有连续的子串中,按照以上方式统计其所有数字之和,第
k大的和是多少。
 

【输入格式】

第一行,两个整数n和k,分别表示长度为n的数字序列和想要统计的第k大的和
接下里一行n个数a_i,表示这个数字序列
 

【输出格式】

一行一个整数,表示第k大的和
 

【样例输入】

7 5

3 -2 1 2 2 1 3 -2

【样例输出】

4

【提示】

1 <= n <= 100000, 1 <= k <= 200000, 0 <= |a_i| <= 10^9数据保证存在第 k 大的和

  

  和HDU 5654 xiaoxin and his watermelon candy几乎一模一样。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=;
const int INF=;
int hash[maxn],a[maxn],n,k;
int head[maxn],nxt[maxn],pre[maxn];
int rt[maxn],pos[maxn<<];
int ch[maxn<<][],cnt;
long long lx[maxn<<],tr[maxn<<];
void Push_up(int x){
int l=ch[x][],r=ch[x][];
tr[x]=tr[l]+tr[r];
if(lx[l]>tr[l]+lx[r]){
lx[x]=lx[l];
pos[x]=pos[l];
}
else{
lx[x]=tr[l]+lx[r];
pos[x]=pos[r];
}
}
void Build(int &rt,int l,int r){
rt=++cnt;
if(l==r){
pos[rt]=l;
if(!pre[l]){
tr[rt]=hash[a[l]];
lx[rt]=hash[a[l]];
}
return;
}
int mid=(l+r)>>;
Build(ch[rt][],l,mid);
Build(ch[rt][],mid+,r);
Push_up(rt);
} void Update(int pr,int &rt,int l,int r,int g,int d){
rt=++cnt;
tr[rt]=tr[pr]+d;
if(l==r){
pos[rt]=g;
lx[rt]=tr[rt];
return;
}
ch[rt][]=ch[pr][];
ch[rt][]=ch[pr][];
int mid=(l+r)>>;
if(g<=mid)Update(ch[pr][],ch[rt][],l,mid,g,d);
else Update(ch[pr][],ch[rt][],mid+,r,g,d);
Push_up(rt);
}
struct data{int x;long long y;};
data Query(int t,int l,int r,int x,int y)
{
if(l==x && r==y) return (data){pos[t],lx[t]};
int mid=(l+r)>>;
if(y<=mid) return Query(ch[t][],l,mid,x,y);
else if(x>mid){
data tmp=Query(ch[t][],mid+,r,x,y);
return (data){tmp.x,tmp.y+tr[ch[t][]]};
}
else{
data tmp1=Query(ch[t][],l,mid,x,mid);
data tmp2=Query(ch[t][],mid+,r,mid+,y);
if(tmp1.y>tmp2.y+tr[ch[t][]]) return tmp1;
else return (data){tmp2.x,tmp2.y+tr[ch[t][]]};
}
} struct node{int i,l,r,x; long long y;};
bool operator<(const node& a1,const node& a2)
{return a1.y<a2.y;}
priority_queue<node> q; int main(){
freopen("bzoj_4504.in","r",stdin);
freopen("bzoj_4504.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
hash[i]=a[i];
sort(hash+,hash+n+);
for(int i=;i<=n;i++)
a[i]=lower_bound(hash+,hash+n+,a[i])-hash;
for(int i=;i<=n;i++){
pre[i]=head[a[i]];
nxt[head[a[i]]]=i;
head[a[i]]=i;
}
Build(rt[],,n);
for(int i=;i<n;i++){
Update(rt[i],rt[i+],,n,i+,-hash[a[i]]);
if(nxt[i])Update(rt[i+],rt[i+],,n,nxt[i],hash[a[i]]);
}
for(int i=;i<=n;i++){
data tmp=Query(rt[i],,n,i,n);
q.push((node){i,i,n,tmp.x,tmp.y});
}
long long ans;
for(int i=;i<=k;i++){
node t=q.top(); q.pop(); ans=t.y;
if(t.l!=t.x){
data tmp=Query(rt[t.i],,n,t.l,t.x-);
q.push((node){t.i,t.l,t.x-,tmp.x,tmp.y});
}
if(t.r!=t.x){
data tmp=Query(rt[t.i],,n,t.x+,t.r);
q.push((node){t.i,t.x+,t.r,tmp.x,tmp.y});
}
}
printf("%lld\n",ans);
return ;
}

  有时间一定要重打!!!

数据结构(主席树):COGS 2213. K个串的更多相关文章

  1. 数据结构(主席树):COGS 2211. 谈笑风生

    2211. 谈笑风生 ★★★★   输入文件:laugh.in   输出文件:laugh.out   简单对比时间限制:3 s   内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...

  2. POJ 2104 K-th Number 主席树(区间第k大)

    题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...

  3. POJ 2104:K-th Number(主席树静态区间k大)

    题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...

  4. zoj2112 主席树动态第k大 (主席树&&树状数组)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  5. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  6. 主席树区间第K大

    主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...

  7. codeforces 1262D Optimal Subsequences 主席树询问第k小

    题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...

  8. 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解

    题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...

  9. 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

    Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...

随机推荐

  1. linux reboot命令

    命令简介: 该命令用来重启Linux系统.相当于Windows系统中的restart命令. 命令语法: /sbin/reboot [-n] [-w] [-d] [-f] [-i] 或 reboot [ ...

  2. Python之路【第五篇】:面向对象和相关

    Python之路[第五篇]:面向对象及相关   面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...

  3. RSA 加解密 签名 示例

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStrea ...

  4. Maven Integration for Eclipse 正确地址

    m2eclipse has moved from sonatype to eclipse. The correct update site is http://download.eclipse.org ...

  5. android 如何解决模块之间的通讯的耦合问题

    使用EventBus http://wuyexiong.github.io/blog/2013/04/30/android-fragment/ http://yunfeng.sinaapp.com/? ...

  6. aliyun云服务器硬件性能测试

    1.所购买阿里云服务器信息 2.dd命令测试 3.

  7. PE File.

    Figure 1 - PE File The CLR header stores information to indicate that the PE file is a .NET executab ...

  8. iOS目录结构

    默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 Documents:苹果建议将程序中建立的或在程序中浏览到的文件 ...

  9. 使用OC开发phonegp 组件

    使用OC开发phonegp 组件 1. 使用oc 对phonegp中的组件近些开发,首先具体的pgonegp跟nativecode之间的一些优劣就不说了,开发phonegp 对应的组件主要就是使用na ...

  10. 3 - testng.xml

    TestNG的调用有以下几种方式: testng.xml ant 命令行 这部分主要介绍testng.xml的格式. 当前testng.xml的DTD(文档类型定义(Document Type Def ...