数据结构(主席树):COGS 2213. K个串
2213. K个串
★★★★ 输入文件:bzoj_4504.in 输出文件:bzoj_4504.out 简单对比
时间限制:20 s 内存限制:512 MB
【题目描述】
【输入格式】
【输出格式】
【样例输入】
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个串的更多相关文章
- 数据结构(主席树):COGS 2211. 谈笑风生
2211. 谈笑风生 ★★★★ 输入文件:laugh.in 输出文件:laugh.out 简单对比时间限制:3 s 内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- POJ 2104:K-th Number(主席树静态区间k大)
题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...
- zoj2112 主席树动态第k大 (主席树&&树状数组)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- HDU 6278 主席树(区间第k大)+二分
Just h-index Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)To ...
- 主席树区间第K大
主席树的实质其实还是一颗线段树, 然后每一次修改都通过上一次的线段树,来添加新边,使得每次改变就改变logn个节点,很多节点重复利用,达到节省空间的目的. 1.不带修改的区间第K大. HDU-2665 ...
- codeforces 1262D Optimal Subsequences 主席树询问第k小
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...
- 牛客多校第九场H Cutting Bamboos(主席树 区间比k小的个数)题解
题意: 标记为\(1-n\)的竹子,\(q\)个询问,每次给出\(l,r,x,y\).要求为砍区间\(l,r\)的柱子,要求砍\(y\)次把所有竹子砍完,每次砍的时候选一个高度,把比他高的都砍下来,并 ...
- 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy
Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...
随机推荐
- CentOS 6.7编译安装PHP 5.6
1.准备编译环境 yum install gcc gcc-c++ pcre* openssl* gd-devel* libxml2-devel bzip2-devel libcurl-devel 2. ...
- 与数据库打交道的Adapter----SimpleCursorAdapter
http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...
- Encapsulation.
Access control is often referred to as implementation hiding. Wrapping data and methods within class ...
- org.springframework.beans.factory.BeanCreationException: 求教育!
2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...
- oracle数组学习资料
--oracle数组,所谓数组就是 字段的 个数,数组应该很有用 --可变数组 declare type v_ar is varray(10) of varchar2(30); my_ar v ...
- power desinger 学习笔记<五>
怎样才能在修改表的字段Name的时候,Code不自动跟着变 tools-> General Options-> Dialog:Operation Modes: 去掉 NameToC ...
- AVAudioSession
AVAudioSession类由AVFoundation框架引入.每个IOS应用都有一个音频会话.这个会话可以被AVAudioSession类的sharedInstance类方法访问,如下: AVAu ...
- 十六、C# 常用集合类及构建自定义集合(使用迭代器)
常用集合类及构建自定义集合 1.更多集合接口:IList<T>.IDictionary<TKey,TValue>.IComparable<T>.ICollectio ...
- SpringMVC4+thymeleaf3的一个简单实例(篇四:form表单数据验证)
关于表单数据验证有很多中方法,这里我仅介绍JSR303注解验证.JSR303仅仅是一个规范,这里我们要用到它的一个实现:hibernate-validator. 注意在spring的配置文件sprin ...
- MongoDB 与传统关系型数据库mysql比较
与关系型数据库相比,MongoDB的优点: 转载自 http://blog.sina.com.cn/s/blog_966e430001019s8v.html①弱一致性(最终一致),更能保证用户的访问 ...