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 @ 2022-05-13 19:38  xh小小孩  阅读(25)  评论(0)    收藏  举报