[HDU 1754] I Hate It Splay

http://acm.hdu.edu.cn/showproblem.php?pid=1754

题意:略

继续splay。。。。

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;/


const int maxn = 200005;

struct Tree{
    int val, maxval;
    int pre, chd[2];
};


int n, m;
Tree tr[maxn];

int F(int rt){
    return tr[tr[rt].pre].chd[1] == rt;
}

void PushUp(int rt)  //向上更新
{
    tr[tr[rt].chd[0]].pre = rt;
    tr[tr[rt].chd[1]].pre = rt;
    tr[rt].maxval = tr[rt].val;
    if(tr[rt].maxval < tr[tr[rt].chd[0]].maxval){   //更新最大值
        tr[rt].maxval = tr[tr[rt].chd[0]].maxval;
    }
    if(tr[rt].maxval < tr[tr[rt].chd[1]].maxval){    //更新最大值
        tr[rt].maxval = tr[tr[rt].chd[1]].maxval;
    }
}

void Rotate(int rt)      //旋转,将rt转到它父结点的位置
{
    int chd = F(rt);  //判断自己是哪一个孩子
    int pre = tr[rt].pre;
    tr[rt].pre = tr[pre].pre;
    tr[tr[pre].pre].chd[F(pre)] = rt;
    tr[pre].chd[chd] = tr[rt].chd[!chd];
    tr[rt].chd[!chd] = pre;
    PushUp(pre);
    PushUp(rt);
}

int Splay(int rt, int rw)
{
    while(tr[rw].pre != rt){
        int pre = tr[rw].pre;
        if(F(rw) != F(pre) || tr[pre].pre == rt){  //单旋转
            Rotate(rw);
        }
        else{     //双旋转
            Rotate(pre);
            Rotate(rw);
        }
    }
    if(rt)   //防止更新0
        PushUp(rt);
    return rw;
}


int BuildTree(int Size)
{
    for(int i = 0; i <= Size; i++){
        tr[i].pre = i - 1;
        tr[i].chd[0] = 0;
        tr[i].chd[1] = i + 1;
    }
    tr[0].pre = 0;
    tr[0].val = tr[0].maxval = 0;
    tr[1].pre = 0;
    tr[1].val = tr[1].maxval = 0;
    tr[Size].chd[1] = 0;
    tr[Size].val = tr[Size].maxval = 0;
    return 1;
}

int main()
{
    int rt;
    while(~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= n; i++){
            scanf("%d", &tr[i+1].val);
        }
        rt = BuildTree(n + 2);
        rt = Splay(0, n + 2);
        int x, y;
        char str[5];
        while(m--){
            scanf("%s%d%d", str, &x, &y);
            if(str[0] == 'U'){
                rt = Splay(0, x + 1);
                tr[rt].val = y;
                PushUp(rt);
            }
            else{
                if(x > y)
                    swap(x, y);
                rt = Splay(0, x);
                Splay(rt, y + 2);
                printf("%d\n", tr[tr[tr[rt].chd[1]].chd[0]].maxval);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

achonor

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

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

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

打赏作者

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

抵扣说明:

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

余额充值