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

记录一个菜逼的成长。。

题目链接

这题可以直接用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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值