HDU1512 左偏树||斜堆

本来我是在开开心心(泪流满面)地刷Scapegoat Tree的,结果突然间就看见了一个奇奇怪怪的东西——斜堆。

因为昨天刚好学了左偏树,于是就来回忆了一发,然后发现。。。。。。。。。

既然有斜堆了为毛还要左偏树啊!!!

嗯好吧均摊确实不靠谱(此处@Splay),但你告诉我为毛斜堆跑得比左偏树快啊!!!

好吧常数我吃了。

其实斜堆和左偏树就一点不同,左偏树只有在维护左偏性质的时候才会交换左右子树,斜堆每次merge的时候都交换(各种均摊)

左偏树(我写程序自带常数TAT):

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100000+10;
int read(){
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
struct heapnode{
    int l,r,f,v,d;
}heap[N];
int merge(int x,int y){
    if(!x||!y)return x+y;
    if(heap[x].v<heap[y].v)swap(x,y);
    heap[x].r=merge(heap[x].r,y);
    heap[heap[x].r].f=x;
    if(heap[heap[x].l].d<heap[heap[x].r].d)swap(heap[x].l,heap[x].r);
    if(!heap[x].r)heap[x].d=0;
    else heap[x].d=heap[heap[x].r].d+1;
    return x;
}
int find(int x){
    return heap[x].f==x?x:heap[x].f=find(heap[x].f);
}
int pop(int x){
    int l=heap[x].l,r=heap[x].r;
    heap[l].f=l;heap[r].f=r;
    heap[x].l=heap[x].r=heap[x].d=0;
    return merge(l,r);
}
int push(int x,int y){
    return merge(x,y);
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            heap[i].l=heap[i].r=heap[i].d=0;
            heap[i].f=i;heap[i].v=read();
        }
        int q;q=read();
        while(q--){
            int x,y;
            x=read();y=read();x=find(x);y=find(y);
            if(x==y)puts("-1");
            else{
                heap[x].v/=2;
                int xx=pop(x);
                xx=push(xx,x);
                heap[y].v/=2;
                int yy=pop(y);
                yy=push(yy,y);
                int rt=merge(xx,yy);
                printf("%d\n",heap[rt].v);
            }
        }
    }
    return 0;
}


斜堆(RP好自带减常数):

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100000+5;
struct Node{
    int l,r,v,f;
}heap[N];
int merge(int x,int y){
    if(!x||!y)return x+y;
    if(heap[x].v<heap[y].v)swap(x,y);
    heap[x].r=merge(heap[x].r,y);
    heap[heap[x].r].f=x;
    swap(heap[x].l,heap[x].r);
    return x;
}
int findroot(int x){
    while(heap[x].f!=x)x=heap[x].f;
    return x;
}
int push(int x,int y){
    return merge(x,y);
}
int pop(int x){
    int l=heap[x].l,r=heap[x].r;
    heap[l].f=l;heap[r].f=r;
    heap[x].l=heap[x].r=0;
    return merge(l,r);
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            heap[i].l=heap[i].r=0;
            heap[i].f=i;
            scanf("%d",&heap[i].v);
        }
        int m;scanf("%d",&m);int x,y;
        while(m--){
            scanf("%d%d",&x,&y);
            x=findroot(x);y=findroot(y);
            if(x==y)puts("-1");
            else{
                int xx=pop(x);heap[x].v/=2;
                xx=push(xx,x);
                int yy=pop(y);heap[y].v/=2;
                yy=push(yy,y);
                int tmp=merge(xx,yy);
                printf("%d\n",heap[tmp].v);
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值