FHQ_Treap树(无旋Treap树)模板向

这种树是一种无需旋转操作的Treap树,由FHQ(范浩强)大佬发明,堪称是神级的数据结构!他短小精悍,易于学习,而其思想之优雅令无数OIer/ACMer以及程序员们为之着迷!

而这一切都来源于这一份ppt:范浩强谈数据结构

在了解这个数据结构之前最好先了解一下treap树以及笛卡尔树,这两种树本身具有一定的相似性,而为FHQ_Treap提供了思想延申的起点。

FHQ_Treap代替旋转操作的核心就是树的分离(split)与合并(merge)两种操作,使得我们可以将原树按照值/位置(下标)分解为两棵树,以及将两棵树合并为一棵平衡的树,而剩下的一切操作均基于这两种操作!

按值的split:

void split(int x ,int k, int &l, int &r)
{
    if(!x) {l=r=0;return;}//到空节点返回0
    pushdown(x);
    if(val[x]<=k){l=x;split(rs[l],rs[l],r,k); pushup(l);}//x分给左树,接着分x的右儿子
    else{r=x;split(ls[r],l,ls[r],k);pushup(r);}//x分给右树,接着分x的左儿子
}

按权值分裂应用于维护有序序列时(类比于STL的set,或堆),可以查询kth大等

按下标的split:

void split(int x, int k, int &l, int &r)
{
    if(!x) {l=r=0;return;}
    pushdown(x);
    if(sz[ls[x]]+1<=k){l=x;split(rs[l],rs[l],r,k-sz[ls[x]]-1);pushup(l);}//注意修改k
    else{r=x;split(ls[r],l,ls[r],k);pushup(r);}
}

按下标分裂用于维护普通序列,可进行插入节点、区间和查询、RMQ、区间修改等

合并(merge):

int merge(int x, int y)// 合并 
{
 	if(!x || !y) return x+y;// x和y中必定有一个是0
 	if(pri[x] < pri[y])// 把x加到左边的树上 
 	{
 		pushdown(x);
 	    rs[x] = merge(rs[x],y);
 	    pushup(x);
 	    return x;
 	} 
    else
 	{
 		pushdown(y);
 	    ls[y] = merge(x, ls[y]);
	    pushup(y);
 	    return y;
	}
}

split与merge的pushup与pushdown按具体情况自行增删修改即可。

鉴于本帖为模板向,不详细解释其背后原理(其实是自己不太会啦。。)
直接上题!!

洛谷P3369 普通平衡树

#include 
using namespace std;
typedef long long ll;
typedef unsigned long long usi;
const ll MOD = 998244353;
const int MAXN = 1e5 + 7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-12;

int ch[MAXN][3], val[MAXN], pri[MAXN], sz[MAXN];

class FHQ_Treap
{
public:
	int size = 0;

	bool empty() {return size == 0;}

	void insert(int a)
	{
		split(root, a, x, y);
		root = merge(merge(x, newnode(a)), y);
	}

	void erase(int a)
	{
		split(root, a, x, z);
		split(x, a-1, x, y);
		y = merge(ch[y][0], ch[y][1]);
		root = merge(merge(x, y), z);
	}

	int numrank(int a)
	{
		split(root, a-1, x, y);
		int res = sz[x] + 1;
		root = merge(x, y);
		return res;
	}

	int kth(int k)
	{
		return val[myrank(root, k)];
	}

	int pre(int a)
	{
		split(root, a-1, x, y);
		int res = val[myrank(x, sz[x])];
		root = merge(x, y);
		return res;
	}

	int next(int a)
	{
		split(root, a, x, y);
		int res = val[myrank(y, 1)];
		root = merge(x, y);
		return res;
	}

private:
	int root = 0, x, y, z;

	void update(int x)
	{
		sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
	}

	int newnode(int v)
	{
		sz[++size] = 1;
		val[size] = v;
		pri[size] = rand();
		return size;
	}

	int merge(int x, int y) 
	{
 	    if(!x || !y) return x + y;
 	    if(pri[x] < pri[y])
 	    {
 	        ch[x][1] = merge(ch[x][1],y);
 	        update(x);
 	        return x;
 	    } 
 	    else
 	    {
 	        ch[y][0] = merge(x, ch[y][0]);
	        update(y);
 	        return y;
	    }
	}

	void split(int now, int k, int &x, int &y)
	{
		if(!now) x = y = 0;
		else
		{
			if(val[now] <= k) x = now, split(ch[now][1], k, ch[now][1], y);
			else y = now, split(ch[now][0], k, x, ch[now][0]);
			update(now);
		}
	}

	int myrank(int now, int a)
	{
		while(true)
		{
			if(a < sz[ch[now][0]]) now = ch[now][0];
			else if(a == sz[ch[now][0]] + 1) return now;
			else a -= sz[ch[now][0]] + 1, now = ch[now][1];
		}
	}
};

FHQ_Treap mytree;

int main()
{
	srand((unsigned)time(NULL));
	int n, opt, a;
	cin >> n;
	while(n--)
	{
		scanf("%d%d", &opt, &a);
		if(opt == 1) mytree.insert(a);
		else if(opt == 2) mytree.erase(a);
		else if(opt == 3) printf("%d\n", mytree.numrank(a));
		else if(opt == 4) printf("%d\n", mytree.kth(a));
		else if(opt == 5) printf("%d\n", mytree.pre(a));
		else if(opt == 6) printf("%d\n", mytree.next(a));
	}
    return 0;
}

牛客 1221 E Forsaken的数列

本题使用了建笛卡尔树的方法优化建树到O(n)

#include
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;

struct FHQ_Treap
{
    int size = 0, root = 0;
    struct node
    {
        int ls, rs, rnd, siz;
        ll val, sum, lazy;
        node() {}
        node(int LS, int RS, int RND, int SIZ, ll VAL, ll SUM, ll LAZY):
            ls(LS), rs(RS), rnd(RND), siz(SIZ), val(VAL), sum(SUM), lazy(LAZY) {}
    }tree[MAXN];

    inline void pushUp(int p)
    {
        tree[p].siz = tree[tree[p].ls].siz + tree[tree[p].rs].siz + 1;
        tree[p].sum = tree[tree[p].ls].sum + tree[tree[p].rs].sum + tree[p].val;
    }

    inline void pushDown(int p)
    {
        if(tree[p].lazy)
        {
            tree[tree[p].ls].sum += 1ll * tree[tree[p].ls].siz * tree[p].lazy;
            tree[tree[p].ls].lazy += tree[p].lazy;
            tree[tree[p].ls].val += tree[p].lazy;
            tree[tree[p].rs].sum += 1ll * tree[tree[p].rs].siz * tree[p].lazy;
            tree[tree[p].rs].lazy += tree[p].lazy;
            tree[tree[p].rs].val += tree[p].lazy;
            tree[p].lazy = 0;
        }
    }

    void split(int p, int rk, int &x, int &y)
    {
        if(!p) {x = y = 0; return;}
        pushDown(p);
        if(rk <= tree[tree[p].ls].siz)
        {
            y = p;
            split(tree[p].ls, rk, x, tree[p].ls);
            pushUp(y);
        }
        else
        {
            x = p;
            split(tree[p].rs, rk-tree[tree[p].ls].siz-1, tree[p].rs, y);
            pushUp(x);
        }
    }

    int merge(int x, int y)
    {
        if(!x || !y) return x | y;
        if(tree[x].rnd < tree[y].rnd)
        {
            pushDown(x);
            tree[x].rs = merge(tree[x].rs, y);
            pushUp(x);
            return x;
        }
        else
        {
            pushDown(y);
            tree[y].ls = merge(x, tree[y].ls);
            pushUp(y);
            return y;
        }
    }

    int newnode(ll v)
    {
        int p = ++size;
        tree[p] = node(0, 0, rand(), 1, v, v, 0);
        return p;
    }

    void insert(ll val, int p)
    {
        int x, y;
        split(root, p - 1, x, y);
        root = merge(merge(x, newnode(val)), y);
    }

    void add(int l, int r, ll v)
    {
        int x, y, z;
        split(root, l - 1, x, y);
        split(y, r + 1 - l, y, z);
        tree[y].sum += 1ll * tree[y].siz * v;
        tree[y].lazy += v;
        tree[y].val += v;
        root = merge(merge(x, y), z);
    }

    void query(int l, int r)
    {
        int x, y, z;
        split(root, l - 1, x, y);
        split(y, r + 1 - l, y, z);
        printf("%lld\n", tree[y].sum);
        root = merge(merge(x, y), z);
    }

    int st[MAXN], top;
    inline void build(int n)
    //用建笛卡尔树的方法O(n)建树,st为栈,返回新树的根
    {
        top = 0;
        for(int i = 0; i < n; ++i)
        {
            ll val;
            scanf("%lld", &val);
            int tmp = newnode(val), last = 0;
            while(top && tree[st[top]].rnd > tree[tmp].rnd)
            {
                last = st[top];
                pushUp(last);
                st[top--] = 0;
            }
            if(top) tree[st[top]].rs = tmp;
            tree[tmp].ls = last;
            st[++top] = tmp;
        }
        while(top) pushUp(st[top--]);
        root = st[1];
    }
} mytree;

int main()
{
    int n, m, op, pos, l, r;
    ll v;
    scanf("%d", &n);
    mytree.build(n);
    scanf("%d", &m);
    while(m--)
    {
        scanf("%d", &op);
        if(op == 1) scanf("%d", &pos), mytree.insert(0, pos);
        else if(op == 2) scanf("%d%d%lld", &l, &r, &v), mytree.add(l, r, v);
        else scanf("%d%d", &l, &r), mytree.query(l, r);
    }
    return 0;
}

洛谷P3391 文艺平衡树

#include 
#include 
#include 
#define N 100005

int Root;
int lazy[N];
int n,m,cnt;
int val[N],sze[N];
int ch[N][2],prio[N];

void pushup(int o){
    sze[o]=sze[ch[o][0]]+sze[ch[o][1]]+1;
}

void pushdown(int o){
    if(!lazy[o] or !o) return;
    ch[o][0]^=ch[o][1]^=ch[o][0]^=ch[o][1];
    lazy[ch[o][0]]^=1;
    lazy[ch[o][1]]^=1;
    lazy[o]=0;
}

void split(int o,int k,int &x,int &y){
    if(!o) x=y=0;
    else{
        pushdown(o);
        if(k>sze[ch[o][0]]) x=o,split(ch[o][1],k-sze[ch[o][0]]-1,ch[o][1],y);
        else y=o,split(ch[o][0],k,x,ch[o][0]);
        pushup(o);
    }
}

int merge(int x,int y){
    if(!x or !y) return x+y;
    pushdown(x); pushdown(y);
    if(prio[x]<prio[y]){
        ch[x][1]=merge(ch[x][1],y);
        pushup(x);
        return x;
    }
    else{
        ch[y][0]=merge(x,ch[y][0]);
        pushup(y);
        return y;
    }
}

int newnode(int v){
    val[++cnt]=v;
    sze[cnt]=1;
    prio[cnt]=rand();
    return cnt;
}

void res(int l,int r){
    int a,b,c,d;
    split(Root,r,a,b);
    split(a,l-1,c,d);
    lazy[d]^=1;
    Root=merge(merge(c,d),b);
}

void dfs(int now){
    if(!now) return;
    pushdown(now);
    dfs(ch[now][0]);
    printf("%d ",val[now]);
    dfs(ch[now][1]);
}

signed main(){
    srand(time(0));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        Root=merge(Root,newnode(i));
    //printf("Root=%d\n",Root);
    for(int x,y,i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        res(x,y);
        //printf("i=%d\n",i);
        //dfs(Root);
    }
    //printf("Root=%d\n",Root);
    dfs(Root);
    return 0;
}

你可能感兴趣的:(原创,数据结构学习)