zoukankan      html  css  js  c++  java
  • LuoguP3377 左偏树 (左偏树)

    TLE but corrct in most cases.

    inline int Find(int x){
        //be careful with the way used for finding your grand papa
        for(; fa[x]; x = fa[x]);
        return x;
    }
    inline int Merge(int x,int y){
        if(!x) return y;
        if(!y) return x;
        if(val[x] > val[y] || (val[x] == val[y] && x > y)) x^=y^=x^=y;
        son[x][1] = Merge(son[x][1], y);
        fa[son[x][1]] = x;
        if(dis[son[x][1]] > dis[son[x][0]]) swap(son[x][1], son[x][0]);
        dis[x] = dis[son[x][1]] + 1;
        return x;
    }
    
    inline void Del(int x){
        val[x] = -1;
        fa[son[x][1]] = fa[son[x][0]] = 0;
        Merge(son[x][1], son[x][0]);
    }
    

    AC with an undescriable magic.

    inline int Find(int x){
        return x == fa[x] ? x : fa[x] = Find(fa[x]);
    }
    
    inline int Merge(int x,int y){
        if(!x) return y;
        if(!y) return x;
        if(val[x] > val[y] || (val[x] == val[y] && x > y)) swap(x, y);
        son[x][1] = Merge(son[x][1], y);
        fa[son[x][0]] = fa[son[x][1]] = x;
        if(dis[son[x][0]] < dis[son[x][1]]) swap(son[x][0], son[x][1]);
        dis[x] = dis[son[x][1]] + 1;
        return x;
    }
    int del[N];
    inline void Del(int x){
        del[x] = true;
        fa[son[x][0]] = son[x][0];
        fa[son[x][1]] = son[x][1];
        fa[x] = Merge(son[x][0], son[x][1]);//QAQ
    	/*
        Ah, it seems as father is not useful for himself anymore, he becomes a rope for his son to climb higher...
    	anyway, this program goes well with this sentence, so who care about the reason.
    	He who laughs last, laughs best...
        */
    }
    

    use struct

    struct LeftTree{
    	int l,r,val,dis;
    }t[N];
    //int son[N][2],fa[N],val[N],dis[N];
    int fa[N];
    inline int Find(int x){
        return x == fa[x] ? x : fa[x] = Find(fa[x]);
    }
    
    inline int Merge(int x,int y){
        if(!x) return y;
        if(!y) return x;
        if(t[x].val > t[y].val || (t[x].val == t[y].val && x > y)) swap(x, y);
        t[x].r = Merge(t[x].r, y);
        fa[t[x].l] = fa[t[x].r] = x;
        if(t[t[x].l].dis < t[t[x].r].dis) swap(t[x].l, t[x].r);
        t[x].dis = t[t[x].r].dis + 1;
        return x;
    }
    
    int del[N];
    inline void Del(int x){
        del[x] = true;
        fa[t[x].l] = t[x].l;
        fa[t[x].r] = t[x].r;
        fa[x] = Merge(t[x].l, t[x].r);//QAQ
    	/*
        Ah, it seems as father is not useful for himself anymore, he becomes a rope for his son to climb higher...
    	anyway, this program goes well with this sentence, so who care about the reason.
    	He who laughs last, laughs best...
        */
    }
    

  • 相关阅读:
    Contains Duplicate
    Valid Anagram
    Invert Binary Tree
    Same Tree
    代理模式——java设计模式
    单例模式——java设计模式
    关于春招自己感到很失落这件事
    Collections.sort()中的mergeSort归并排序
    腾讯云搭建日志收集系统
    centos7 mysql
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11185861.html
Copyright © 2011-2022 走看看