您的位置:首页 > 理论基础 > 数据结构算法

hiho一下-第104周-平衡树·Splay

2017-03-16 18:56 302 查看
记录一个菜逼的成长。。

题目链接

这题可以直接用set做。

这里记下模板。

#include <bits/stdc++.h>
using namespace std;
#define MINK 0
#define MAXK 1000000001
struct Node{
Node *father,*left,*right;
int v;
Node(int k, Node* f) {left=NULL;right=NULL;father=f;v=k;}
}*root = NULL;
void right_Rotate(Node *x)
{
Node* p = x->father;
x -> father = p -> father;
if(p -> father != NULL){
if(p->father->left == p)
p -> father -> left = x;
else
p -> father -> right = x;
}
else
root = x;
p -> left = x -> right;
if(x -> right != NULL)x -> right -> father = p;
x -> right = p;
p -> father = x;
}
void left_Rotate(Node *x)
{
Node* p = x->father;
x -> father = p -> father;
if(p -> father != NULL){
if(p->father->left == p)
p -> father -> left = x;
else
p -> father -> right = x;
}
else
root = x;
p -> right = x -> left;
if(x -> left != NULL)x -> left -> father = p;
x -> left = p;
p -> father = x;
}
void splay(Node *x, Node *y)
{
if(x == NULL)return ;
while(x -> father != y){
Node *p = x -> father;
if(p -> father == y){
if(p -> left == x)
right_Rotate(x);
else
left_Rotate(x);
}
else {
Node *g = p -> father;
if(g -> left == p){
if(p -> left == x){
right_Rotate(p);
right_Rotate(x);
}
else {
left_Rotate(x);
right_Rotate(x);
}
}
else {
if(p -> right == x){
left_Rotate(p);
left_Rotate(x);
}
else {
right_Rotate(x);
left_Rotate(x);
}
}
}
}
}///在执行这个操作的时候,需要保证y节点一定是x节点祖先。
Node* bst_Insert(Node *T,int key)
{
if(key < T -> v){
if(T -> left == NULL){
T -> left = new Node(key,T);
return T -> left;
}
else {
return bst_Insert(T->left,key);
}
}
else {
if(T -> right == NULL){
T -> right = new Node(key,T);
return T -> right;
}
else {
return bst_Insert(T->right,key);
}
}
}
Node* bst_Find(Node *T,int key)
{
if(T -> v == key){
return T;
}
if(key < T -> v){
if(T -> left == NULL){
return NULL;
}
else {
return bst_Find(T->left,key);
}
}
else {
if(T -> right == NULL){
return NULL;
}
else {
return bst_Find(T->right,key);
}
}
}
void Insert(int key)
{
if(root == NULL){
root = new Node(key,NULL);
}
else {
Node *node = bst_Insert(root,key);
splay(node,NULL);
}
}
void* Find(int key)
{
Node *node = bst_Find(root,key);
splay(node,NULL);
}
Node* findPrev(int key)
{
Find(key);
Node *node = root -> left;
while(node -> right != NULL){
node = node -> right;
}
return node;
}
Node* findNext(int key)
{
Find(key);
Node *node = root -> right;
while(node -> left != NULL)
node = node -> left;
return node;
}
void Delete(int key)
{
Node* prev = findPrev(key);
Node* Next = findNext(key);
splay(prev,NULL);
splay(Next,prev);
if(Next != NULL)Next -> left = NULL;
}
void deleteInterval(int a,int b)
{
if(a <= MINK) a = MINK + 1;
if(b >= MAXK) b = MAXK - 1;

Node *aa = bst_Find(root,a);
if(aa == NULL)Insert(a);
Node *prev = findPrev(a);

Node *bb = bst_Find(root,b);
if(bb == NULL)Insert(b);
Node *Next = findNext(b);

splay(prev,NULL);
splay(Next,prev);
if(Next != NULL)Next -> left = NULL;
}
int ans;
int query(Node* T, int k) {
if (T->v == k) {
return k;
}
if (T->v > k) {
if (T->left==NULL) return ans;
else return query(T->left, k);
}
if (T->right==NULL) return T->v;
else {
ans=T->v;
return query(T->right,k);
}
}

int main()
{
int n,a,b;
char ope[5];
scanf("%d",&n);
Insert(MINK);
Insert(MAXK);
for( int i = 0; i < n; i++ ){
scanf("%s",ope);
if(ope[0] == 'I'){
scanf("%d",&a);
Insert(a);
}
if(ope[0] == 'Q'){
scanf("%d",&a);
ans = query(root,a);
printf("%d\n",ans==0?a:ans);
}
if(ope[0] == 'D'){
scanf("%d%d",&a,&b);
deleteInterval(a,b);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息