求助
查看原帖
求助
336981
竹下的魂楼主2020/8/29 13:39
#include <iostream>
using namespace std;
const int INF = 2147483647;
struct node{
    int value,left,right,size,count;
    static int len;
    node(int _v = 0,int _l = 0,int _r = 0,int _s = 0,int _c = 0){
        value = _v,left = _l,right = _r,size = _s,count = _c;
    }
}tree[10005];
int node::len = 0;
inline void update(int &root){
    tree[root].size = tree[tree[root].left].size+tree[tree[root].right].size+tree[root].count;
}
int Rank(int x,int root){
    if(!root)return INF;
    if(x<tree[root].value)
        return Rank(x,tree[root].left);
    else if(x>tree[root].value)
        return Rank(x,tree[root].right)+tree[tree[root].left].size+tree[root].count;
    else return tree[tree[root].left].size+1;
}
int Getnum(int x,int root){
    if(x<1||x>node::len)return INF;
    if(x<=tree[tree[root].left].size)
        return Getnum(x,tree[root].left);
    if(x<=tree[tree[root].left].size+tree[root].count)
        return tree[root].value;
    return Getnum(x-tree[tree[root].left].size-tree[root].count,tree[root].right);
}
void Insert(int x,int &root){
    if(root==0){
        tree[root = ++node::len] = node{x,0,0,1,1};
        return;
    }
    if(x<tree[root].value)
        Insert(x,tree[root].left);
    else if(x>tree[root].value)
        Insert(x,tree[root].right);
    else
        tree[root].count++;
    update(root);
}
int main(){
    int n,opt,x,root = 1;
    cin>>n;
    while(n--){
        cin>>opt>>x;
        if(opt==1) cout<<Rank(x,root)<<endl;
        else if(opt==2) cout<<Getnum(x,root)<<endl;
        else if(opt==3){
            //不能找到输出-INF
            if(Getnum(Rank(x,root)-1,root)==INF) cout<<-INF;
            //能找到正常输出
            else cout<<Getnum(Rank(x,root)-1,root)<<endl;
        }
        else if(opt==4){
            if(Getnum(Rank(x,root)+1,root)==INF) cout<<INF;
            else cout<<Getnum(Rank(x,root)+1,root)<<endl;
        }
        else Insert(x,root);
    }
    system("pause");
    return 0;
}
2020/8/29 13:39
加载中...