数据结构(主席树):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 ...
随机推荐
- HTML简单介绍及常见元素
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- HDU 4288 Coder(线段树)
题意: 给定三种操作 1. add x 向序列中添加x,添加之后序列还保持有序 2. del x 删除序列中值为x的元素 3. sum 求下边模5等于3的元素和 思路: 直接暴力也可以过,就是看暴 ...
- 网站分析统计JS源码分享
之前公司做了一个分析云平台,用来跟踪收集海量的用户行为的相关数据,供运营人员实时监控网站访问量,统计PV,UV,独立IP,访问时段,访问时长,热点追踪等多类信息,我用JS写了一个小插件,只需要再页面加 ...
- JavaScript Math对象
Math对象是JavaScript的一个固有对象,其作用是执行常见的算数任务.该对象的使用并不是像Date,Array对象使用 new关键字来得到对象,而是直接 Math.[{property|met ...
- Eclipse清除SVN密码
1. Eclipse/Subclipse并不会记录用户名及密码, SVN密码是由SVN客户端(JavaHL or SVNKit)负责维持的 2. 可查看Eclipse的Preferences中SVN属 ...
- The performance between the 'normal' operation and the 'shift' operation.
First, I gonna post my test result with some code: //test the peformance of the <normal operation ...
- 关于mssql数据库锁和事务隔离级别
事务隔离级别有4种,这4种级别只是对于读操作,也就是select有不同程度的支持, 读未提交:不会对事务里读出来的数据附加任何锁 读已提交:会对事务里读出来的数据附加共享锁,读完就释放共享锁,其他事务 ...
- oracle数据库读取操作系统的物理文件-转载,待完善
--源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...
- 用JS实现版面拖拽效果
类似于这样的一个版面,点击标题栏,实现拖拽效果. 添加onmousedown事件 通过获取鼠标的坐标(clientX,clientY)来改变面板的位置 注意:面板使用绝对定位方式,是以左上角为参考点, ...
- Java jdk环境搭建
java JDK的配置在我的电脑环境变量中配置: 主要的配置参数path 例:path = C:\jdk1.7.0_13\bin ; 另外一个JAVA_HOME 例:JAVA_HOME = C:\j ...