跳到正文

算法·理论

【模板】红黑树 指针实现 普通平衡树

LJY_ljy约 16 分钟
原文
文章信息与版本

文章信息

发布
2024年4月2日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 16 分钟

版本

  1. v1当前

Source: 2024年春 算法基础 第二次实验T1

example: P6136 【模板】普通平衡树(数据加强版)

C++
//P6136 【模板】普通平衡树(数据加强版) 红黑树指针实现
//一定要留心Undefined Behavior!非void类型函数一定要留心所有形参情况都要有对应的返回值!!! 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

inline int read() {
    register int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

struct node1 {
	int color, siz, key, cnt;
	node1 *l;
	node1 *r;
	node1 *p;
};

struct node2 {
	node1 *root;
	node1 *nil;
} T;

void pushup(node2 *T, node1 *x) {
	int y = 0;
	if (x->l != T->nil) y += x->l->siz;
	if (x->r != T->nil) y += x->r->siz;
	y += x->cnt; 
	x->siz = y;
	return;
}

void maintain(node2 *T, node1 *p) {	
	pushup(T, p);
	if (p != T->root) maintain(T, p->p);
	return;
}

node1 *tree_minimum(node2 *T, node1 *x) {
	while (x->l != T->nil) {
		x = x->l;
	}
	return x;
}

node1 *tree_maximum(node2 *T, node1 *x) {
	while (x->r != T->nil) {
		x = x->r;
	}
	return x;
}

node1 *tree_successor(node2 *T, node1 *x) {
	node1 *y = x->p;
	if (x->r != T->nil) {
		return tree_minimum(T, x->r);
	}
	while ((y != T->nil) && (x == y->r)) {
		x = y;
		y = y->p;
	}
	return y;
}

node1 *tree_predecessor(node2 *T, node1 *x) {
	node1 *y = x->p;
	if (x->l != T->nil) return tree_maximum(T, x->l);
	while ((y != T->nil) && (x == y->l)) {
		x = y;
		y = y->p;
	}
	return y;
}

node1 *Search(node2 *T, int key) {
	node1 *node = T->root;
	while (node != T->nil) {
		if (key < node->key) {
			node = node->l;
		} else if (key > node->key) {
			node = node->r;
		} else {
			return node;
		}	
	}
	return T->nil;
}

void left_rotate(node2 *T, node1 *x) {
	node1 *y = x->r;
	x->r = y->l;
	if (y->l != T->nil) {
		y->l->p = x;
	}
	y->p = x->p;
	if (x->p == T->nil) {
		T->root = y;
	} else if (x == x->p->l) {
		x->p->l = y;
	} else {
		x->p->r = y;
	}
	y->l = x;
	x->p = y; 
	maintain(T, x);
	maintain(T, y);
    return;
}

void right_rotate(node2 *T, node1 *y) {
	node1 *x = y->l;
	y->l = x->r;
	if (x->r != T->nil) {
		x->r->p = y;
	}
	x->p = y->p;
	if (y->p == T->nil) {
		T->root = x;
	} else if (y == y->p->r) {
		y->p->r = x;
	} else {
		y->p->l = x;
	}
	x->r = y;
	y->p = x;
	maintain(T, y);
	maintain(T, x);
	return;
}

void insert_fixup(node2 *T, node1 *z) {
	while (z->p->color == 1) {
		if (z->p == z->p->p->l) {
			node1 *y = z->p->p->r;
			if (y->color == 1) {
				z->p->color = 2;
				y->color = 2;
				z->p->p->color = 1;
				z = z->p->p;
			} else {
				if (z == z->p->r) {
					z = z->p;
					left_rotate(T, z);
				}
				z->p->color = 2;
				z->p->p->color = 1;
				right_rotate(T, z->p->p);
			}
		}else {
			node1 *y = z->p->p->l;
			if (y->color == 1) {
				z->p->color = 2;
				y->color = 2;
				z->p->p->color = 1;
				z = z->p->p;
			} else {
				if (z == z->p->l) {
					z = z->p;
					right_rotate(T, z);
				}
				z->p->color = 2;
				z->p->p->color = 1;
				left_rotate(T, z->p->p);
			}
		}
	}
	T->root->color = 2;
	return;
}

void Insert(node2 *T, node1 *z) {
	node1 *y = T->nil;
	node1 *x = T->root;
	while (x != T->nil) {
		y = x;
		if (z->key < x->key) {
			x = x->l;
		} else if (z->key > x->key)
			x = x->r;
	}
	z->p = y;
	if (y == T->nil) {
		T->root = z;
	} else if (z->key < y->key) {
		y->l = z;
	} else {
		y->r = z;
	}
	z->l = T->nil;
	z->r = T->nil;
	z->color = 1;
	maintain(T, z);
	insert_fixup(T, z);
	return;
}

void delete_fixup(node2 *T, node1 *x) {
	while ((x != T->root) && (x->color == 2)) {
		if (x == x->p->l) {
			node1 *w= x->p->r;
			if (w->color == 1) {
				w->color = 2;
				x->p->color = 1;
				left_rotate(T, x->p);
				w = x->p->r;
			}
			if ((w->l->color == 2) && (w->r->color == 2)) {
				w->color = 1;
				x = x->p;
			} else {
				if (w->r->color == 2) {
					w->l->color = 2;
					w->color = 1;
					right_rotate(T, w);
					w = x->p->r;
				}
				w->color = x->p->color;
				x->p->color = 2;
				w->r->color = 2;
				left_rotate(T, x->p);
				x = T->root;
			}
		} else {
			node1 *w = x->p->l;
			if (w->color == 1) {
				w->color = 2;
				x->p->color = 1;
				right_rotate(T, x->p);
				w = x->p->l;
			}
			if ((w->l->color == 2) && (w->r->color == 2)) {
				w->color = 1;
				x = x->p;
			} else {
				if (w->l->color == 2) {
					w->r->color = 2;
					w->color = 1;
					left_rotate(T, w);
					w = x->p->l;
				}
				w->color = x->p->color;
				x->p->color = 2;
				w->l->color = 2;
				right_rotate(T, x->p);
				x = T->root;
			}
		}
	}
	x->color = 2;
	return; 
}

void transplant(node2 *T, node1 *u, node1 *v) {
    if (u->p == T->nil) T->root = v;
    else if (u == u->p->l) u->p->l = v;
    else u->p->r = v;
    v->p = u->p;
    maintain(T, u);
    maintain(T, v);
    return;
}

node1* Delete(node2 *T, node1 *z) {
	node1 *y = T->nil;
	node1 *x = T->nil;
	if ((z->l == T->nil) || (z->r == T->nil)) {
		y = z;
	} else {
		y = tree_successor(T, z);
	}
	if (y->l != T->nil) {
		x = y->l;
	} else if (y->r != T->nil) {
		x = y->r;
	}
	x->p = y->p;
	if (y->p == T->nil) {
		T->root = x;
	} else if (y == y->p->l) {
		y->p->l = x;
	} else {
		y->p->r = x;
	}
	if (y != z) {
		z->key = y->key;
		z->cnt = y->cnt;
		z->siz = y->siz;
		maintain(T, y); maintain(T, z); maintain(T, y);
	}
	if (y->color == 2) {
		delete_fixup(T, x);
	}
	return y;
}

int get_rank(node2 *T, node1 *p, int x) {
    if (p == T->nil) return 0;
    if (x == p->key) return p->l->siz;
    if (x < p->key) return get_rank(T, p->l, x);
    return get_rank(T, p->r, x) + p->l->siz + p->cnt;
}

int get_kth(node2 *T, node1 *p, int k) {
	while (1) {
		if (p->l != T->nil && k <= p->l->siz) {
			p = p->l;
		} else {
			k -= p->cnt + p->l->siz;
			if (k <= 0) {
				return p->key;
			}
			p = p->r;
		}
	}
}

int get_minner(node2 *T, node1 *p, int v) {
	if (v <= p->key) {
		if (p->l != T->nil) return get_minner(T, p->l, v);
		return -1;
	} else {
		int Ans = p->key;
		if (p->r != T->nil) {
			int AAA = get_minner(T, p->r, v);
			if (AAA != -1)
				Ans = AAA;
		}
		return Ans;
	}
}

int get_maxxer(node2 *T, node1 *p, int v) {
    if (v >= p->key) {
		if (p->r != T->nil) return get_maxxer(T, p->r, v);
		return -1;
    } else {
		int Ans = p->key;
		if (p->l != T->nil) {
			int AAA = get_maxxer(T, p->l, v);
			if (AAA != -1) Ans = AAA;
		}
		return Ans;
    }
}

int main() {
    int n = read(), m = read();
	node2 *T = (node2 *)malloc(sizeof(node2));
	T->nil = (node1*)malloc(sizeof(node1));
	T->nil->color = 2;
	T->root = T->nil;
	node1 *node = T->nil;
	for (int i = 1; i <= n; i++) {
        int x = read();
        node1 *cur = Search(T, x);
        if (cur == T->nil) {
        	node = (node1*)malloc(sizeof(node1));
			node->key = x;
			node->siz = 1;
			node->cnt = 1;
			node->color = 1;
			node->l = T->nil; node->r = T->nil; node->p = T->nil;
			Insert(T, node);
		} else {
			cur->cnt++;
			maintain(T, cur);
		}
	}
	int last = 0, ans = 0;
	int Siz = 0;
	for (int i = 1; i <= m; i++) {
		int opt = read(), x = read();
		x ^= last;
		if (opt == 1) {
			Siz++;
			node1* cur = Search(T, x);
			if (cur == T->nil) {
				node = (node1*)malloc(sizeof(node1));
				node->key = x;
				node->siz = 1;
				node->cnt = 1;
				node->color = 1;
				node->l = T->nil; node->r = T->nil; node->p = T->nil;
				Insert(T, node);
			} else {
				cur->cnt++;
				maintain(T, cur);
			}
		} else if (opt == 2) {
			Siz--;
			node1 *cur = Search(T, x);
			cur->cnt--;
			maintain(T, cur);
			if (cur->cnt == 0) {
				node1* cur1 = Delete(T, cur);
				free(cur1);
			}
		} else if (opt == 3) {
			last = get_rank(T, T->root, x) + 1;
			ans ^= last;
		} else if (opt == 4) {
			last = get_kth(T, T->root, x);
			ans ^= last;
		} else if (opt == 5) {
			last = get_minner(T, T->root, x);
			ans ^= last;
		} else if (opt == 6) {
			last = get_maxxer(T, T->root, x);
			ans ^= last;
		}
	}
	printf("%d\n", ans);
	return 0;
}
原文