Code:

#include<bits/stdc++.h>
using namespace std;
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 200000
#define inf 10000000000000
#define get(x) (ch[f[x]][1]==x)
#define ll long long
int root,cc,kk;
int h[maxn],ch[maxn][2],f[maxn],siz[maxn],val[maxn];
long long sumv[maxn];
long long ans=inf;
void pushup(int x)
{
sumv[x]=sumv[ch[x][0]]+sumv[ch[x][1]]+h[x];
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}
void rotate(int x)
{
int old=f[x],fold=f[old],which=get(x);
ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
ch[x][which^1]=old,f[old]=x,f[x]=fold;
if(fold) ch[fold][ch[fold][1]==old]=x;
pushup(x),pushup(old);
}
void splay(int x,int &tar)
{
int u=f[tar];
for(int fa;(fa=f[x])!=u;rotate(x))
if(f[fa]!=u)
rotate(get(fa)==get(x)?fa:x);
tar=x;
}
void insert(int &x,int key,int ff)
{
if(!x) x=key,f[x]=ff;
else insert(ch[x][h[key]>h[x]],key,x);
pushup(x);
}
int query(int kth)
{
int x=root;
while(1)
{
if(siz[ch[x][0]]+1==kth) return x;
if(siz[ch[x][0]]>=kth) x=ch[x][0];
else kth-=(siz[ch[x][0]]+1),x=ch[x][1];
}
}
void del(int x)
{
if(!ch[x][0]) root=ch[x][1], f[root]=ch[x][1]=0;
else if(!ch[x][1]) root=ch[x][0], f[root]=ch[x][0]=0;
else
{
int l=ch[x][0];
while(ch[l][1]) l=ch[l][1];
splay(l, ch[x][0]);
ch[l][1]=ch[x][1], f[ch[x][1]]=l, f[l]=ch[x][0]=ch[x][1]=0, pushup(l);
root=l;
}
}
void solve(int k,int L)
{
int mid=(k%2==0)?k/2:(k/2)+1,x=query(mid);
splay(x,root);
int l=ch[root][0],r=ch[root][1];
ll re=0;
re+=(h[x]*siz[l]-sumv[l]);
re+=(sumv[r]-h[x]*siz[r]);
if(re<ans) ans=re, cc=L, kk=h[x];
}
int main()
{
// setIO("input");
int n,k,i,j;
scanf("%d%d",&n,&k);
for(i=1;i<=n;++i) scanf("%d",&h[i]);
for(i=1;i<=k;++i) { insert(root,i,0); if(i%6==0) splay(i,root); }
solve(k,1);
for(i=k+1;i<=n;++i)
{
j=i-k+1;
splay(j-1, root), del(j-1), insert(root,i,0), splay(i,root), solve(k,j);
}
printf("%lld\n",ans);
return 0;
}

  

BZOJ 1112: [POI2008]砖块Klo Splay + 性质分析的更多相关文章

  1. BZOJ 1112: [POI2008]砖块Klo

    1112: [POI2008]砖块Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1736  Solved: 606[Submit][Statu ...

  2. [BZOJ 1112] [POI2008] 砖块Klo 【区间K大】

    题目链接:BZOJ - 1112 题目分析 枚举每一个长度为k的连续区间,求出这个区间的最优答案,更新全局答案. 可以发现,这个区间的所有柱子最终都变成这k个数的中位数时最优,那么我们就需要查询这个区 ...

  3. 线段树 || BZOJ 1112: [POI2008]砖块Klo

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1112 题解: 希望有连续K柱的高度是一样的,就先把1~K的数扔进线段树(线段树的下标就是数值 ...

  4. BZOJ 1112 [POI2008]砖块Klo(可持久化线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1112 [题目大意] 给出一个数列,对于一个操作,你可以对一个数+1,或者一个数-1, ...

  5. bzoj 1112: [POI2008]砖块Klo【对顶堆】

    priority_queue实现的对顶堆,细节超级多WA了十几次--但是理论上是最简便的orz其实是我已经不会写平衡树了 枚举左端点,显然要把这一段的高度搞成(l,l+k-1)的高度中位数,所以需要一 ...

  6. BZOJ 1112: [POI2008]砖块Klo1112( BST )

    枚举每个长度为k的区间, 然后用平衡树找中位数进行判断, 时间复杂度O(nlogn). 早上起来精神状态不太好...连平衡树都不太会写了...果断去看了会儿番然后就A了哈哈哈 ------------ ...

  7. 1112: [POI2008]砖块Klo

    1112: [POI2008]砖块Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1245  Solved: 426[Submit][Statu ...

  8. [Bzoj1112][POI2008]砖块Klo(splay)

    1112: [POI2008]砖块Klo Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2353  Solved: 831[Submit][Statu ...

  9. [BZOJ1112][POI2008]砖块Klo

    [BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...

随机推荐

  1. FutureTask的用法以及两种常用的使用场景

    参考博客:https://blog.csdn.net/linchunquan/article/details/22382487 FutureTask可用于异步获取执行结果或取消执行任务的场景.通过传入 ...

  2. VMware 虚拟化编程(2) — 虚拟磁盘文件类型详解

    目录 目录 前文列表 虚拟磁盘文件 VMDK 用户可以创建的虚拟磁盘类型 VixDiskLib 中支持的虚拟磁盘类型 虚拟机文件类型 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/ ...

  3. shift()函数

    用于对dataframe中的数整体上移或下移, 当为正数时,向下移. 当为负数时,向上移. 缺少的会填充NaN 参考: https://blog.csdn.net/kizgel/article/det ...

  4. sql exist 和not exist(转载)

    exists : 强调的是是否返回结果集,不要求知道返回什么, 比如:  select name from student where sex = 'm' and mark exists(select ...

  5. herizai_CD2所做答案

    //herizai_CD1第一题 #include<iostream> #include<iomanip> using namespace std; void print1(i ...

  6. js:获取单选组radio中的被选择的数据

    现在有一name为sex的单选组,代表的是选择性别,要求获取radio中被选择的选项值 <div class="sexDiv"> 用户性别: <input cla ...

  7. PHP foreach &$ 引发的bug

    在使用foreach &$来更新数据的时候,造成数据被更新掉了 $arr = array(1,2,3,4,5); foreach ($arr as &$row) { $row += 1 ...

  8. vs2015上编译QT程序的环境搭建

    下载相对应版本的QT(以QT5.7.0为例),进入网站http://download.qt.io/archive/qt/5.7/5.7.0/,下载MSVC版本QT,我的系统是64位,VS版本是2015 ...

  9. C++ 函数返回对象时并没有调用拷贝构造函数

    #include <iostream> #include <vector> #include <string.h> using namespace std; cla ...

  10. Mysql共享锁、排他锁、悲观锁、乐观锁

    一.相关名词 |--表级锁(锁定整个表) |--页级锁(锁定一页) |--行级锁(锁定一行) |--共享锁(S锁,MyISAM 叫做读锁) |--排他锁(X锁,MyISAM 叫做写锁) |--间隙锁( ...