POJ - 3481 - 平衡树模板题2

跟 BZOJ 3224 不同的是
优先级是题目中给定的,且不同
所以把 sz 域删掉就好

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int n, cnt, rt;
int maxval, minval;

struct node{
    int lc, rc;
    int val, pri;
    int num;
}tr[maxn];

int NewNode(int v, int num){
    tr[++cnt].val = v;
    tr[cnt].pri = rand();
    tr[cnt].num = num;
    tr[cnt].lc = tr[cnt].rc = 0;
    return cnt;
}

void Zig(int &p){
    //Right Rotate
    int q = tr[p].lc;
    tr[p].lc = tr[q].rc;
    tr[q].rc = p;
    p = q;
}

void Zag(int &p){
    int q = tr[p].rc;
    tr[p].rc = tr[q].lc;
    tr[q].lc = p;
    p = q;
}

void Insert(int &p, int v, int num){
    if (!p){
        p = NewNode(v, num);
        return;
    }
    if (v <= tr[p].val){
        Insert(tr[p].lc, v, num);
        if (tr[p].pri < tr[tr[p].lc].pri)
            Zig(p);
    }
    else{
        //v > tr[p].val
        Insert(tr[p].rc, v, num);
        if (tr[p].pri < tr[tr[p].rc].pri)
            Zag(p);
    }
}

void Delete(int &p, int v){
    if (!p) return;
    if (v == tr[p].val){
        if (!tr[p].lc || !tr[p].rc)
            p = tr[p].lc + tr[p].rc;
        else if (tr[tr[p].lc].pri > tr[tr[p].rc].pri){
            Zig(p);
            Delete(tr[p].rc, v);
            }
        else{
            Zag(p);
            Delete(tr[p].lc, v);
        }
        return;
    }
    if (v < tr[p].val)
        Delete(tr[p].lc, v);
    else
        Delete(tr[p].rc, v);
}

int GetPre(int v){
    int p = rt;
    int res = 0;
    while(p){
        if (tr[p].val < v){
            res = tr[p].val;
            p = tr[p].rc;
        }
        else
            p = tr[p].lc;
    }
    return res;
}

int GetNxt(int v){
    int p = rt;
    int res = 0;
    while(p){
        if (tr[p].val > v){
            res = tr[p].val;
            p = tr[p].lc;
        }
        else
            p = tr[p].rc;
    }
    return res;
}

void printmax(int p){
    while(tr[p].rc){
        p = tr[p].rc;
    }
    printf("%d\n", tr[p].num);
    maxval = tr[p].val;
}

void printmin(int p){
    while(tr[p].lc){
        p = tr[p].lc;
    }
    printf("%d\n", tr[p].num);
    minval = tr[p].val;
}

int main(){
    int num, val;
    //freopen("input.txt", "r", stdin);
    while(scanf("%d", &n), n){
        if (n == 1){
            scanf("%d%d", &num, &val);
            Insert(rt, val, num);
        }
        else if (n == 2){
            if (rt == 0) printf("0\n");
            else{
                printmax(rt);
                Delete(rt, maxval);
            }
        }
        else if (n == 3){
            if (rt == 1) printf("0\n");
            else{
                printmin(rt);
                Delete(rt, minval);
            }
        }    
    }
    return 0;
}
全部评论

相关推荐

01-06 22:57
武汉大学 Java
点赞 评论 收藏
分享
2025-12-04 14:15
杭州电子科技大学 C++
点赞 评论 收藏
分享
我是秋招面的抖音电商部门,从八月开始给我打电话约面试,当时有加对应的hr微信,前几轮还好,从三面开始就是拖半个月一下在推进,面了三轮以后拖了一个月说再加一个交叉面,面完过了半个多月问hr,hr说她离职了不管我了,我连自己的流程在哪里都不清楚。在官网投诉了三次(前两次正常反馈问题,字节你像听不懂话一样),今天下午突然有其他部门约我面试,说在人才库里看见我简历了觉得很匹配,我拒了说不想面了。过了一会,声称原来那个部门的hr负责人给我回电话,表示抱歉,说因为北京hc不够了把我流程释放了,前面的hr是实习生离职了,没有和我对接非常抱歉,如果有想法可以帮我推荐给其他部门。那么我问你:1.&nbsp;从八月等到11月,三个月时间我前面不断确认还有没有hc,给我的回复都是有hc,没hc你面什么呢?2.&nbsp;作为一个大厂你流程这么一坨?让实习生对接校招流程,实习生离职了对应的候选人就不管了?不是我主动问我的流程就这样一直被卡?3.&nbsp;还说我面评没问题,帮我推荐其他部门,现在各个大厂都开奖了,你还让我从头开始面?我前面面了你们三个月啊,孩子饿死了开始装得想负责了。4.&nbsp;你们官网的流程反馈对应的人员是理解能力有问题吗,一个问题我投诉三次才能看明白是吗?套话一堆在哪里随便选了个常用语就给我回复了?不想开可以直接把入口关了
奋斗的小码农a:字节流程就是恶心死人,一次字节广告5面挂一次字节抖音电商6面挂,最后那个面试官装都不装了直接分屏聊天还外放语音家里真的私募了
秋招吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务