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 <bits/stdc++.h>
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<bits/stdc++.h>
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 <ctime>
#include <cstdio>
#include <cstdlib>
#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章