[BZOJ1112][POI2008]砖块Klo(splay)

题目描述

传送门

题解

维护一段长度为k的区间的splay,每一次查询中位数即可。
每一个点只会被加入和删除一次,时间复杂度 O(2nlogn)

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
#define N 100005

const LL inf=1e18;
int n,k,root,sz;
LL h[N];
int f[N],ch[N][2];
LL key[N],size[N],sum[N];
LL ans=inf;

void clear(int x)
{
    f[x]=ch[x][0]=ch[x][1]=key[x]=size[x]=sum[x]=0;
}
int get(int x)
{
    return ch[f[x]][1]==x;
}
void update(int x)
{
    size[x]=1;sum[x]=key[x];
    if (ch[x][0])
    {
        size[x]+=size[ch[x][0]];
        sum[x]+=sum[ch[x][0]];
    }
    if (ch[x][1])
    {
        size[x]+=size[ch[x][1]];
        sum[x]+=sum[ch[x][1]];
    }
}
void rotate(int x)
{
    int old=f[x],oldf=f[old],wh=get(x);
    ch[old][wh]=ch[x][wh^1];
    if (ch[old][wh]) f[ch[old][wh]]=old;
    ch[x][wh^1]=old;
    f[old]=x;
    if (oldf) ch[oldf][ch[oldf][1]==old]=x;
    f[x]=oldf;
    update(old);
    update(x);
}
void splay(int x)
{
    for (int fa;fa=f[x];rotate(x))
        if (f[fa])
            rotate( (get(x)==get(fa))?fa:x );
    root=x;
}
void insert(LL x)
{
    if (!root)
    {
        root=++sz;
        size[sz]=1;key[sz]=sum[sz]=x;
        return;
    }
    int now=root,fa=0;
    while (1)
    {
        fa=now;
        now=ch[now][x>key[now]];
        if (!now)
        {
            ++sz;
            f[sz]=fa;ch[fa][x>key[fa]]=sz;
            size[sz]=1;key[sz]=sum[sz]=x;
            update(fa);
            splay(sz);
            break;
        }
    }
}
int find(int x)
{
    int now=root;
    while (1)
    {
        if (x<=size[ch[now][0]]) now=ch[now][0];
        else
        {
            x-=size[ch[now][0]];
            if (x==1) return now;
            --x;
            now=ch[now][1];
        }
    }
}
int pre()
{
    int now=ch[root][0];
    while (ch[now][1]) now=ch[now][1];
    return now;
}
void del(int x)
{
    splay(x);
    if (!ch[root][0]&&!ch[root][1])
    {
        clear(root);
        root=0;
        return;
    }
    if (!ch[root][0])
    {
        int oldroot=root;
        root=ch[oldroot][1];
        f[root]=0;
        clear(oldroot);
        return;
    }
    if (!ch[root][1])
    {
        int oldroot=root;
        root=ch[oldroot][0];
        f[root]=0;
        clear(oldroot);
        return;
    }
    int oldroot=root;
    int leftbig=pre();
    splay(leftbig);
    ch[root][1]=ch[oldroot][1];
    f[ch[root][1]]=root;
    clear(oldroot);
    update(root);
    return;
}
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=n;++i) scanf("%lld",&h[i]);
    for (int i=1;i<=k;++i) insert(h[i]);
    int now=find((k>>1)+1);
    splay(now);
    LL cost=(size[ch[root][0]]*key[root]-sum[ch[now][0]]+sum[ch[root][1]]-size[ch[root][1]]*key[root]);
    ans=min(ans,cost);
    for (int i=k+1;i<=n;++i)
    {
        insert(h[i]);
        del(i-k);
        int now=find((k>>1)+1);
        splay(now);
        LL cost=(size[ch[root][0]]*key[root]-sum[ch[now][0]]+sum[ch[root][1]]-size[ch[root][1]]*key[root]);
        ans=min(ans,cost);
    }
    printf("%lld\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值