跳到正文

个人记录

[2018.10.30]BZOJ1112: [POI2008]砖块Klo(数据结构/平衡树/Splay)

xryjr233约 17 分钟
原文
文章信息与版本

文章信息

发布
2018年10月30日
收录
2026年9月16日
更新
2026年11月16日
篇幅
约 17 分钟

版本

  1. v1当前

对于固定区间[l,l+k−1][l,l+k-1],转化问题为求区间[l,l+k−1][l,l+k-1]中的中位数。

瞬间变Splay裸题。(当然主席树之类的也可以)

先将区间[1,k][1,k]中的元素加入Splay,求其中的第(⌈k2⌉)(\lceil\frac{k}{2}\rceil)大数,并求出Splay中每个数与这个数的 差的绝对值 的和(有点绕),每个节点记录节点代表的值,这个值在区间中的数量,这个点及其子树的和以及节点数量。

从[l,l+k−1][l,l+k-1]移动到区间[l+1,l+k][l+1,l+k]时,删除hlh_{l},加入hl+kh_{l+k}即可。(代码中的区间是[r−k+1,r][r-k+1,r])

然后码Splay模板170多行,加上主程序也就共200多行。。。

这题原题还要求输出答案方案(最终的砖块高度),见Luogu P3466,代码附在这篇文章后面

code(BZOJ上的题):

C++
#include<bits/stdc++.h>
using namespace std;
struct node{
    int f,c[2];
    long long v,n,sum,num;
}s[100010];
int n,k,h[100010],rt,cnt;
long long ans=1e12,tmp;
inline void Upd(int x){
    s[x].sum=s[s[x].c[0]].sum+s[s[x].c[1]].sum+s[x].v*s[x].n;
    s[x].num=s[s[x].c[0]].num+s[s[x].c[1]].num+s[x].n;
}
inline void Rotate(int x){
    int y=s[x].f,z=s[y].f;
    int ic=s[y].c[1]==x,igc=s[z].c[1]==y;
    s[z].c[igc]=x;
    s[x].f=z;
    s[y].c[ic]=s[x].c[ic^1];
    s[s[x].c[ic^1]].f=y;
    s[x].c[ic^1]=y;
    s[y].f=x;
    Upd(y);
    Upd(x);
}
inline void Splay(int x,int f){
    while(s[x].f!=f){
        int y=s[x].f,z=s[y].f;
        if(z==f){
            Rotate(x);
        }else{
            int ic=s[y].c[1]==x,igc=s[z].c[1]==y;
            if(ic==igc){
                Rotate(y);
            }else{
                Rotate(x);
            }
            Rotate(x);
        }
    }
    if(!f){
        rt=x;
    }
}
inline void Find(int x){
    int nw=rt;
    while(1){
        if(s[nw].v==x){
            break;
        }
        if(s[nw].v>x){
            if(!s[nw].c[0]){
                break;
            }
            nw=s[nw].c[0];
        }else{
            if(!s[nw].c[1]){
                break;
            }
            nw=s[nw].c[1];
        }
    }
    Splay(nw,0);
}
inline void Create(int &x,int f,int val){
    x=++cnt;
    s[x].v=val;
    s[x].n=1;
    s[x].f=f;
    s[x].c[0]=0;
    s[x].c[1]=0;
    s[x].sum=val;
    s[x].num=1;
}
void Insert(int x,int v){
    if(!rt){
        Create(rt,0,v);
        return;
    }
    if(s[x].v==v){
        s[x].n++;
        s[x].num++;
        s[x].sum+=s[x].v;
        return;
    }
    if(s[x].v>v){
        if(!s[x].c[0]){
            Create(s[x].c[0],x,v);
        }else{
            Insert(s[x].c[0],v);
        }
        Upd(x);
    }else{
        if(!s[x].c[1]){
            Create(s[x].c[1],x,v);
        }else{
            Insert(s[x].c[1],v);
        }
        Upd(x);
    }
}
inline int Next(int x,int op){
    Find(x);
    if(s[rt].v>x&&op==1){
        return rt;
    }
    if(s[rt].v<x&&op==0){
        return rt;
    }
    int nw=s[rt].c[op];
    if(!nw){
        return 0;
    }
    while(s[nw].c[op^1]){
        nw=s[nw].c[op^1];
    }
    return nw;
}
inline void Erase(int &x){
    s[x].n--;
    s[x].num--;
    s[x].sum-=s[x].v;
    if(!s[x].n){
        s[x].f=0;
        x=0;
    }
}
inline void Delete(int x){
    int lst=Next(x,0),nxt=Next(x,1);
    if(!lst&&!nxt){
        Erase(rt);
    }else if(!lst){
        Splay(nxt,0);
        Erase(s[nxt].c[0]);
        Upd(nxt);
    }else if(!nxt){
        Splay(lst,0);
        Erase(s[lst].c[1]);
        Upd(lst);
    }else{
        Splay(lst,0);
        Splay(nxt,lst);
        Erase(s[nxt].c[0]);
        Upd(nxt);
        Upd(lst);
    }
}
inline long long Query(int x){
    int nw=rt;
    while(nw){
        if(s[s[nw].c[0]].num<x&&x<=s[s[nw].c[0]].num+s[nw].n){
            Splay(nw,0);
            break;
        }else if(s[s[nw].c[0]].num>=x){
            nw=s[nw].c[0];
        }else{
            x-=s[s[nw].c[0]].num+s[nw].n;
            nw=s[nw].c[1];
        }
    }
    return (s[s[rt].c[0]].num*s[rt].v-s[s[rt].c[0]].sum)+(s[s[rt].c[1]].sum-s[s[rt].c[1]].num*s[rt].v);
}
void scan(int &x){
	x=0;
	char c=getchar();
	while('0'>c||c>'9'){
		c=getchar();
	}
	while('0'<=c&&c<='9'){
		x*=10;
		x+=c-'0';
		c=getchar();
	}
}
void print(int x){
	if(x>9){
		print(x/10);
	}
	putchar(x%10+'0');
}
void print(long long x){
	if(x>9){
		print(x/10);
	}
	putchar(x%10+'0');
}
int main(){
    scan(n);
    scan(k);
    for(int i=1;i<=n;i++){
        scan(h[i]);
    }
    for(int i=1;i<k;i++){
        Insert(rt,h[i]);
        Find(h[i]);
    }
    for(int i=k;i<=n;i++){
        Insert(rt,h[i]);
        Find(h[i]);
        if(i-k){
            Delete(h[i-k]);
        }
        ans=min(ans,Query((k+1)/2));
    }
    print(ans);
    return 0;
}

code(原题):

C++
#include<bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
struct node{
    int f,c[2];
    long long v,n,sum,num;
}s[100010];
int n,k,h[100010],rt,cnt,ql,qr,qv;
long long ans=1e12,tmp;
inline void Upd(int x){
    s[x].sum=s[s[x].c[0]].sum+s[s[x].c[1]].sum+s[x].v*s[x].n;
    s[x].num=s[s[x].c[0]].num+s[s[x].c[1]].num+s[x].n;
}
inline void Rotate(int x){
    int y=s[x].f,z=s[y].f;
    int ic=s[y].c[1]==x,igc=s[z].c[1]==y;
    s[z].c[igc]=x;
    s[x].f=z;
    s[y].c[ic]=s[x].c[ic^1];
    s[s[x].c[ic^1]].f=y;
    s[x].c[ic^1]=y;
    s[y].f=x;
    Upd(y);
    Upd(x);
}
inline void Splay(int x,int f){
    while(s[x].f!=f){
        int y=s[x].f,z=s[y].f;
        if(z==f){
            Rotate(x);
        }else{
            int ic=s[y].c[1]==x,igc=s[z].c[1]==y;
            if(ic==igc){
                Rotate(y);
            }else{
                Rotate(x);
            }
            Rotate(x);
        }
    }
    if(!f){
        rt=x;
    }
}
inline void Find(int x){
    int nw=rt;
    while(1){
        if(s[nw].v==x){
            break;
        }
        if(s[nw].v>x){
            if(!s[nw].c[0]){
                break;
            }
            nw=s[nw].c[0];
        }else{
            if(!s[nw].c[1]){
                break;
            }
            nw=s[nw].c[1];
        }
    }
    Splay(nw,0);
}
inline void Create(int &x,int f,int val){
    x=++cnt;
    s[x].v=val;
    s[x].n=1;
    s[x].f=f;
    s[x].c[0]=0;
    s[x].c[1]=0;
    s[x].sum=val;
    s[x].num=1;
}
void Insert(int x,int v){
    if(!rt){
        Create(rt,0,v);
        return;
    }
    if(s[x].v==v){
        s[x].n++;
        s[x].num++;
        s[x].sum+=s[x].v;
        return;
    }
    if(s[x].v>v){
        if(!s[x].c[0]){
            Create(s[x].c[0],x,v);
        }else{
            Insert(s[x].c[0],v);
        }
        Upd(x);
    }else{
        if(!s[x].c[1]){
            Create(s[x].c[1],x,v);
        }else{
            Insert(s[x].c[1],v);
        }
        Upd(x);
    }
}
inline int Next(int x,int op){
    Find(x);
    if(s[rt].v>x&&op==1){
        return rt;
    }
    if(s[rt].v<x&&op==0){
        return rt;
    }
    int nw=s[rt].c[op];
    if(!nw){
        return 0;
    }
    while(s[nw].c[op^1]){
        nw=s[nw].c[op^1];
    }
    return nw;
}
inline void Erase(int &x){
    s[x].n--;
    s[x].num--;
    s[x].sum-=s[x].v;
    if(!s[x].n){
        s[x].f=0;
        x=0;
    }
}
inline void Delete(int x){
    int lst=Next(x,0),nxt=Next(x,1);
    if(!lst&&!nxt){
        Erase(rt);
    }else if(!lst){
        Splay(nxt,0);
        Erase(s[nxt].c[0]);
        Upd(nxt);
    }else if(!nxt){
        Splay(lst,0);
        Erase(s[lst].c[1]);
        Upd(lst);
    }else{
        Splay(lst,0);
        Splay(nxt,lst);
        Erase(s[nxt].c[0]);
        Upd(nxt);
        Upd(lst);
    }
}
inline long long Query(int x){
    int nw=rt;
    while(nw){
        if(s[s[nw].c[0]].num<x&&x<=s[s[nw].c[0]].num+s[nw].n){
            Splay(nw,0);
            break;
        }else if(s[s[nw].c[0]].num>=x){
            nw=s[nw].c[0];
        }else{
            x-=s[s[nw].c[0]].num+s[nw].n;
            nw=s[nw].c[1];
        }
    }
    return (s[s[rt].c[0]].num*s[rt].v-s[s[rt].c[0]].sum)+(s[s[rt].c[1]].sum-s[s[rt].c[1]].num*s[rt].v);
}
void scan(int &x){
	x=0;
	char c=getchar();
	while('0'>c||c>'9'){
		c=getchar();
	}
	while('0'<=c&&c<='9'){
		x*=10;
		x+=c-'0';
		c=getchar();
	}
}
void print(int x){
	if(x>9){
		print(x/10);
	}
	putchar(x%10+'0');
}
void print(long long x){
	if(x>9){
		print(x/10);
	}
	putchar(x%10+'0');
}
int main(){
    scan(n);
    scan(k);
    for(int i=1;i<=n;i++){
        scan(h[i]);
    }
    for(int i=1;i<k;i++){
        Insert(rt,h[i]);
        Find(h[i]);
    }
    for(int i=k;i<=n;i++){
        Insert(rt,h[i]);
        Find(h[i]);
        if(i-k){
            Delete(h[i-k]);
        }
        tmp=Query((k+1)/2);
        if(tmp<ans){
            ans=tmp;
            ql=i-k+1;
            qr=i;
            qv=s[rt].v;
        }
    }
    print(ans);
    puts("");
    for(int i=1;i<=n;i++){
        if(ql<=i&&i<=qr){
            print(qv);
        }else{
            print(h[i]);
        }
        puts("");
    }
    return 0;
}
原文