数据结构(荣誉)实验四 Splay伸展树

1. Splay —— Ver.I

题目描述

在这里插入图片描述

输入

第一行包含一个整数n,表示初始序列的长度。 以下n行每行包含一个整数,描述初始的序列。 接下来一行包含一个整数n,表示插入操作的数目。 以下m行每行描述一个操作。

接下来一行包含一个整数q,表示查询和删除操作的总数目,以下q行描述一个操作

输出

对于所有操作,输出正确的答案。

样例输入

5
1 2 3 4 5
3
ADD 9
ADD 10
ADD 199
5
QUERY 1
QUERY 3
DELETE 1
DELETE 3
QUERY 3

样例输出

the number is good
the number is good
2 3 4 5 9 10 199
2 4 5 9 10 199
the number is bad

题解

#include <iostream>
#include <string>

using namespace std;

struct Node
{
    struct Node *lchild;
    struct Node *rchild;
    struct Node *parent;
    int data;

    Node()
    {
        lchild = rchild = parent = NULL;
        data = 0;
    }
};

void Print(Node *root)
{
    if (root)
    {
        Print(root->lchild);
        if (root->data == 0)
        {
        }
        else
        {
            cout << root->data << ' ';
        }
        Print(root->rchild);
    }
}

void left_single_rotate(Node *&root, Node *&node)
{
    if (node == NULL)
    {
        return;
    }

    Node *parent = node->parent;
    Node *grandparent = parent->parent;
    parent->rchild = node->lchild;
    if (node->lchild)
    {
        node->lchild->parent = node->parent;
    }
    parent->parent = node;
    node->lchild = parent;
    node->parent = grandparent;
    if (grandparent)
    {
        if (grandparent->lchild == parent)
        {
            grandparent->lchild = node;
        }
        else
        {
            grandparent->rchild = node;
        }
    }
    else
    {
        root = node;
    }
}

void right_single_rotate(Node *&root, Node *&node)
{
    if (node == NULL)
    {
        return;
    }
    Node *parent = node->parent;
    Node *grandparent = parent->parent;
    parent->lchild = node->rchild;
    if (node->rchild)
    {
        node->rchild->parent = parent;
    }
    parent->parent = node;
    node->rchild = parent;
    node->parent = grandparent;
    if (grandparent)
    {
        if (grandparent->lchild == parent)
        {
            grandparent->lchild = node;
        }
        else
        {
            grandparent->rchild = node;
        }
    }
    else
    {
        root = node;
    }
}

void SplayUp(Node *&root, Node *&node)
{
    Node *parent = node->parent;
    Node *grandparent = parent->parent;
    if (grandparent->lchild == parent && parent->lchild == node)
    {
        right_single_rotate(root, node);
        right_single_rotate(root, node);
    }
    else if (parent->lchild != node && grandparent->lchild != parent)
    {
        left_single_rotate(root, node);
        left_single_rotate(root, node);
    }
    else if (grandparent->lchild == parent && parent->lchild != node)
    {
        left_single_rotate(root, node);
        right_single_rotate(root, node);
    }
    else if (grandparent->lchild != parent && parent->lchild == node)
    {
        right_single_rotate(root, node);
        left_single_rotate(root, node);
    }
}

void AddSplay(int x, Node *&root, Node *par)
{
    if (!root)
    {
        root = new Node;
        root->data = x;
        root->parent = par;
    }
    else if (x < root->data)
    {
        AddSplay(x, root->lchild, root);
    }
    else if (x > root->data)
    {
        AddSplay(x, root->rchild, root);
    }
}

void CreatSplay(int n, Node *&root)
{
    int temp;
    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        AddSplay(temp, root, NULL);
    }
}

Node *FindLChild(Node *r)
{
    while (r->lchild)
    {
        r = r->lchild;
    }
    return r;
}

Node *FindSplay(int x, Node *&root)
{
    if (!root)
        return NULL;
    if (root->data < x)
        return FindSplay(x, root->rchild);
    else if (root->data > x)
        return FindSplay(x, root->lchild);

    return root;
}

void splay(Node *&root, Node *node)
{
    while (root->lchild != node && root->rchild != node && root != node)
        SplayUp(root, node);
    if (root->lchild == node)
        right_single_rotate(root, node);
    if (root->rchild == node)
        left_single_rotate(root, node);
}

void QuerySplay(int x, Node *&root)
{
    Node *node = FindSplay(x, root);
    if (node)
    {
        cout << "the number is good" << endl;
        splay(root, node);
    }
    else
        cout << "the number is bad" << endl;
}

bool isDel(int x, Node *&root)
{
    Node *node = FindSplay(x, root);
    if (!node)
    {
        return false;
    }
    splay(root, node);
    if (!node->rchild)
    {
        Node *tmp = root;
        root = root->lchild;
        return true;
    }
    Node *min = FindLChild(node->rchild);
    node->data = min->data;
    if (min->parent->lchild == min)
    {
        min->parent->lchild = min->rchild;
    }
    else
    {
        min->parent->rchild = min->rchild;
    }
    return true;
}

void Delete(int x, Node *&root)
{
    if (isDel(x, root))
    {
        Print(root);
        cout << endl;
    }
    else
        cout << "Failed" << endl;
}

int main()
{
    Node *splayRoot = new Node;
    int n, temp;
    cin >> n;
    CreatSplay(n, splayRoot);
    cin >> temp;
    for (int i = 0; i < temp; i++)
    {
        string ope;
        int index;
        cin >> ope >> index;
        AddSplay(index, splayRoot, NULL);
    }
    cin >> temp;
    for (int i = 0; i < temp; i++)
    {
        string ope;
        int index;
        cin >> ope >> index;
        if (ope == "QUERY")
        {
            QuerySplay(index, splayRoot);
        }
        else if (ope == "DELETE")
        {
            Delete(index, splayRoot);
        }
    }
    return 0;
}



2. 宠物收养所(Splay —— 前驱后继操作)

题目描述

最近,贝贝开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,贝贝根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。

这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。

1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。

2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。

【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

输入

第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

输出

仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

样例输入

5
0 2
0 4
1 3
1 2
1 5

样例输出

3

题解

#include <iostream>

#define MAXLEN 80002
using namespace std;
int ans, n;

struct splaytree
{
    int a[MAXLEN], l[MAXLEN], r[MAXLEN];
    int t, tot;

    void zig(int &x)
    {
        int lc = l[x];
        l[x] = r[lc];
        r[lc] = x;
        x = lc;
    }

    void zag(int &x)
    {
        int rc = r[x];
        r[x] = l[rc];
        l[rc] = x;
        x = rc;
    }

    void splay(int &t, int x)
    {
        if (!t)
            return;
        int pl = 0, pr = 0;
        for (;;)
        {
            if (x == a[t])
                break;
            if (x < a[t] && !l[t])
                break;
            if (x > a[t] && !r[t])
                break;
            if (x < a[t])
            {
                if (x < a[l[t]] && l[l[t]])
                    zig(t);
                l[pr] = t, pr = t, t = l[t];
            }
            else
            {
                if (x > a[r[t]] && r[r[t]])
                    zag(t);
                r[pl] = t, pl = t, t = r[t];
            }
        }
        l[pr] = r[t];
        r[pl] = l[t];
        l[t] = r[0];
        r[t] = l[0];
        l[0] = r[0] = 0;
    }

    void insert(const int &x)
    {
        a[++tot] = x;
        if (!t)
        {
            t = tot;
            return;
        }
        splay(t, x);
        if (x < a[t])
        {
            l[tot] = l[t];
            l[t] = tot;
        }
        else
        {
            r[tot] = r[t];
            r[t] = tot;
        }
    }

    void suit(const int &x)
    {
        if (!t)
            return;
        splay(t, x);
        splay(l[t], a[t]);
        splay(r[t], a[t]);
        int pl = abs(a[l[t]] - x), pm = abs(a[t] - x), pr = abs(a[r[t]] - x);
        if (x < a[t] && pl <= pm && l[t])
        {
            ans += pl;
            ans %= 1000000;
            l[t] = l[l[t]];
        }
        else if (x > a[t] && pr < pm && r[t])
        {
            ans += pr;
            ans %= 1000000;
            r[t] = r[r[t]];
        }
        else
        {
            ans += pm;
            ans %= 1000000;
            if (l[t])
            {
                r[l[t]] = r[t];
                t = l[t];
            }
            else if (r[t])
            {
                l[r[t]] = l[t];
                t = r[t];
            }
            else
                t = 0;
        }
    }
} SP[2];

int main()
{
    int x, y;
    cin >> n;
    while (n--)
    {
        cin >> x >> y;
        if (SP[x].t)
        {
            SP[x].suit(y);
        }
        else
        {
            SP[!x].insert(y);
        }
    }
    cout << ans << endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上山打老虎D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值