T237469 【深基16.例7】普通二叉树(简化版)

#include<cstdio>

#include<iostream>

#include<set>

using namespace std;

multiset<int>q;

int n,t,x,order;

int main(){

    q.insert(-0x7fffffff);

    q.insert(0x7fffffff); //提前放入这两个数,避免错误

    scanf("%d",&n);

    while(n--)

    {

        scanf("%d%d",&t,&x);

        if(t==1)

        {

            auto it=q.lower_bound(x);

            //auto是自动判断数据类型

            // it 取得 x 的位置

            order=0; //order为排名

            for(auto i=q.begin();i!=it;i++,order++);

            //这里就处理出了x的排名——order

            printf("%d\n",order);

        }

        else if(t==2)

        {

            order=-1;

            //初值为-1是因为前面有一个-0x7fffffff

            for(int i:q)

                if(++order==x)//缩写,order先自增一,再判断是否与x相等

                        printf("%d\n",i);  //i就是容器里的值,输出i 查询排名为 x的数

        }

        else if(t==3)

        {

            auto it=q.lower_bound(x);   //取得第一个大于等于x的值,也就是第一个x的位置

            printf("%d\n",*--it);    //由于我们要取得前驱,所以it要自减一

        }

        else if(t==4)

        {

            printf("%d\n",*q.upper_bound(x)); //要取得后继,就是第一个大于x的值

            //用upper_bound方法取得第一个大于x的迭代器

        }

        else

        {

            q.insert(x); //直接插入一个数 xx

        }

    }

    return 0;

}

 

posted @   xh小小孩  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示