求助,只有最后一个点过了
查看原帖
求助,只有最后一个点过了
113001
xuhrrr楼主2024/1/23 21:40

感觉自己考虑的点都写到了,不知道自己还有什么地方有疏漏,可惜这题好像不能下原数据参考qaq

#include <iostream>
#define MAXN 2147483647
#define MINN -2147483647
using namespace std;
int cnt;
struct node{
    int left,right,num,size,val;
};
node t[10005];
int q;
void add(int x,int root){
   if(!root){
    cnt++;
    t[cnt] = node{0,0,1,1,x};
    return;
   }
   if(x < t[root].val){
    if(!t[root].left){
        t[root].left = ++cnt;
        t[cnt] = node{0,0,1,1,x};
    }
    else{
        add(x,t[root].left);
    }
   }
   else{
    if(x > t[root].val){
        if(!t[root].right){
            t[root].right = ++cnt;
            t[cnt] = node{0,0,1,1,x};
        }
        else{
            add(x,t[root].right);
        }
    }
    else{
        t[root].num++;
    }
   }
   t[root].size = t[t[root].left].size + t[root].num + t[t[root].right].size;
}
int xth(int x,int root){
    if(x <= t[t[root].left].size){
        return xth(x,t[root].left);
    }
    if(x <= t[t[root].left].size + t[root].num){
        return t[root].val;
    }
    return xth(x - t[t[root].left].size - t[root].num,t[root].right); 
}
int findx(int x,int root){
    if(!root)return 1;
    if(x < t[root].val){
       return findx(x,t[root].left);
    }
    if(x == t[root].val){
        return t[t[root].left].size + 1;
    }
    if(x > t[root].val){
        return findx(x,t[root].right) + t[t[root].left].size + t[root].num;
    }
}
int fr(int x,int root,int ans){
    if(x <= t[root].val){
        if(t[root].left == 0){
            return ans;
        }
        else{
            return fr(x,t[root].left,ans);
        }
    }
    else{
        if(t[root].right == 0){
            return t[root].val;
        }
        else{
            return fr(x,t[root].right,t[root].val);
        }
    }
}
int bk(int x,int root,int ans){
    if(x >= t[root].val){
        if(t[root].right == 0){
            return ans;
        }
        else{
            return bk(x,t[root].right,ans);
        }
    }
    else{
        if(t[root].left == 0){
            return t[root].val;
        }
        else{
            return bk(x,t[root].left,t[root].val);
        }
    }
}
int main()
{
    cin >> q;
    for(int _ = 1 ; _ <= q ; ++_){
        int op,x;
        cin >> op >> x;
        switch (op)
        {
        case 1:
            cout << findx(x,1) << endl;
            break;
        case 2:
            cout << xth(x,1) << endl;
            break;
        case 3:
            cout << fr(x,1,MINN) << endl;
            break;
        case 4:
            cout << bk(x,1,MAXN) << endl;
            break;
        case 5:
            add(x,1);
            break;
        default:
            break;
        }
    }
    return 0;
}
2024/1/23 21:40
加载中...