P3391 【模板】文艺平衡树

原题链接
Splay
代码如下:

#include <cstdio>
#include <cctype> 
#include <algorithm>

using namespace std; 

inline int read() {
    int x = 0, f = 0; char ch = getchar(); 
    while (!isdigit(ch)) f = ch == '-', ch = getchar(); 
    while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); 
    return f ? -x : x; 
}

const int N = 1e5 + 10; 
int n, m, rt; 
struct Node {
    int fa, son[2], siz, rev, data; 
};
Node nd[N]; 

int get(int x) {
    return nd[nd[x].fa].son[1] == x; 
}

void connect(int x, int fa, int t) {
    if (x) nd[x].fa = fa; 
    if (fa) nd[fa].son[t] = x; 
}

void pushdown(int x) {
    if (nd[x].rev) { 
        swap(nd[x].son[0], nd[x].son[1]); 
        nd[nd[x].son[0]].rev ^= 1; nd[nd[x].son[1]].rev ^= 1; 
        nd[x].rev = 0; 
    }
}

void update(int x) { nd[x].siz = nd[nd[x].son[0]].siz + nd[nd[x].son[1]].siz + 1; }

void rotate(int x) {
    int f = nd[x].fa, ff = nd[f].fa, t = get(x), tf = get(f), b = nd[x].son[t ^ 1]; 
    connect(x, ff, tf); connect(b, f, t); connect(f, x, t ^ 1); 
    update(f); update(x); 
}

void splay(int x, int u) {
    for (int f = nd[x].fa; f != u; f = nd[x].fa) {
        if (nd[f].fa != u) {
            rotate(get(x) == get(f) ? f : x); 
        }
        rotate(x); 
    }
    if (!u) rt = x; 
}

void find(int x, int idx, int u) { 
    pushdown(x); 
    int s = nd[nd[x].son[0]].siz; 
    if (idx == s + 1) {
        splay(x, u); 
    } else if (idx <= s) {
        find(nd[x].son[0], idx, u); 
    } else {
        find(nd[x].son[1], idx - s - 1, u); 
    }
}

void reverse(int l, int r) {
    find(rt, l - 1, 0); 
    find(rt, r + 1, rt); 
    nd[nd[nd[rt].son[1]].son[0]].rev ^= 1;
}

void print(int x) {
    pushdown(x); 
    if (nd[x].son[0]) print(nd[x].son[0]); 
    if (nd[x].data) printf("%d ", nd[x].data); 
    if (nd[x].son[1]) print(nd[x].son[1]); 
}

int main() {
    n = read(); m = read(); 
    rt = n + 2; 
    for (int i = 1; i <= n + 2; ++i) {
        nd[i].fa = i + 1; 
        nd[i].son[0] = i - 1; 
        nd[i].siz = i; 
    }
    for (int i = 2; i <= n + 1; ++i) {
        nd[i].data = i - 1; 
    }
    nd[rt].fa = 0; 
    while (m--) {
        int l = read(), r = read(); 
        reverse(l + 1, r + 1); 
    }
    print(rt); 
    return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡树也称为自平衡二叉搜索树,实现起来比较复杂,需要用到旋转操作来保持树的平衡,如左旋、右旋、双旋等。 下面是一份平衡树模板代码,主要包括插入、删除、查找操作,以及各种旋转操作实现。 ```c #include <stdio.h> #include <stdlib.h> // 平衡树节点结构体 typedef struct TreeNode { int val, height; // val:节点权值,height:节点高度 struct TreeNode *left, *right; // 左右子节点指针 } TreeNode; // 获取节点高度 int getHeight(TreeNode *node) { return node ? node->height : 0; } // 获取节点平衡因子 int getBalanceFactor(TreeNode *node) { return getHeight(node->left) - getHeight(node->right); } // 更新节点高度 void updateHeight(TreeNode *node) { node->height = 1 + fmax(getHeight(node->left), getHeight(node->right)); } // 左旋操作 TreeNode *leftRotate(TreeNode *node) { TreeNode *right = node->right; node->right = right->left; right->left = node; updateHeight(node); updateHeight(right); return right; } // 右旋操作 TreeNode *rightRotate(TreeNode *node) { TreeNode *left = node->left; node->left = left->right; left->right = node; updateHeight(node); updateHeight(left); return left; } // 平衡操作(使以node为根的树平衡,返回新根节点) TreeNode *balance(TreeNode *node) { // 更新节点高度 updateHeight(node); // 计算平衡因子 int balanceFactor = getBalanceFactor(node); // 如果平衡因子为2,说明左子树比右子树高 if (balanceFactor > 1) { if (getBalanceFactor(node->left) < 0) { // 如果左子树的右子树比左子树高 node->left = leftRotate(node->left); // 先左旋左子节点 } return rightRotate(node); } // 如果平衡因子为-2,说明右子树比左子树高 else if (balanceFactor < -1) { if (getBalanceFactor(node->right) > 0) { // 如果右子树的左子树比右子树高 node->right = rightRotate(node->right); // 先右旋右子节点 } return leftRotate(node); } // 否则说明当前子树已经平衡,直接返回 return node; } // 插入操作(p为当前子树的根节点,val为要插入的节点权值) TreeNode *insert(TreeNode *p, int val) { if (!p) { // 如果是空节点,说明找到了要插入的位置,新建一个节点 p = (TreeNode *) malloc(sizeof(TreeNode)); p->val = val; p->height = 1; p->left = p->right = NULL; } else if (val < p->val) { // 如果要插入的值小于节点值,向左子树递归 p->left = insert(p->left, val); } else { // 否则向右子树递归 p->right = insert(p->right, val); } return balance(p); // 插入成功后,再进行平衡操作 } // 寻找以p为根的树中权值为val的节点 TreeNode *find(TreeNode *p, int val) { if (!p) { // 如果搜索到空节点,说明未找到 return NULL; } else if (val < p->val) { // 如果要查找的值小于节点值,向左子树递归 return find(p->left, val); } else if (val > p->val) { // 如果要查找的值大于节点值,向右子树递归 return find(p->right, val); } else { // 否则就是要查找的节点 return p; } } // 查找以p为根的树中的最小节点 TreeNode *findMin(TreeNode *p) { while (p->left) { // 不停向左子节点遍历,直到为空 p = p->left; } return p; } // 删除操作(p为当前子树的根节点,val为要删除的节点权值) TreeNode *delete(TreeNode *p, int val) { if (!p) { // 如果搜索到空节点,说明未找到,直接返回 return NULL; } else if (val < p->val) { // 如果要删除的值小于节点值,向左子树递归 p->left = delete(p->left, val); } else if (val > p->val) { // 如果要删除的值大于节点值,向右子树递归 p->right = delete(p->right, val); } else { // 否则就是要删除的节点 if (!p->left && !p->right) { // 如果是叶子节点,直接删除 free(p); return NULL; } else if (p->left && !p->right) { // 如果只有左子节点(或没有子节点),指向左子节点 TreeNode *left = p->left; free(p); return left; } else if (!p->left && p->right) { // 如果只有右子节点,指向右子节点 TreeNode *right = p->right; free(p); return right; } else { // 如果有左、右子节点,用右子节点的最小值代替当前节点,然后再删除右子节点最小值 TreeNode *minRight = findMin(p->right); p->val = minRight->val; p->right = delete(p->right, p->val); } } return balance(p); // 删除成功后,再进行平衡操作 } // 深度优先遍历(先序遍历) void dfs(TreeNode *p) { if (!p) return; printf("%d ", p->val); dfs(p->left); dfs(p->right); } // 主函数 int main() { TreeNode *root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = delete(root, 30); root = delete(root, 20); root = insert(root, 25); dfs(root); return 0; } ``` 上述代码给出了平衡树节点结构体、获取节点高度、获取节点平衡因子、更新节点高度、左旋、右旋、平衡操作、插入、查找、查找最小节点和删除等常用函数实现。同时,还给出了一个简单的测试代码,用于对平衡树进行测试和验证。 使用平衡树可以大大提高数据插入、删除、查找等操作的效率,所以是实际应用中很常用的数据结构之一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值