用BST测试的,样例能过,全WA,求助大神帮助,谢谢
查看原帖
用BST测试的,样例能过,全WA,求助大神帮助,谢谢
514264
August2012楼主2021/5/11 14:31
#include<iostream>
#include<cstdio>

using namespace std;


typedef struct TreeNode{
    int left =0;
    int right =0;
    int size = 0;
    int value = 0;
    int num = 0;
    TreeNode(int l, int r, int s, int v, int n){
        left = l;
        right = r;
        size =s;
        value = v;
        num = n;
    }
    TreeNode(){}
} TreeNode;

const int MAXINT =0X7FFFFFFF;

TreeNode tn[10010];
int next_point = 1;


//找x的前驱
int findFront(int x, int root, int ans){
    if(x > tn[root].value ){
        if(tn[root].right == 0){
            return tn[root].value;
        }
        return findFront(x,tn[root].right,tn[root].value);
    }
    else {
        if(tn[root].left == 0){
            return ans;
        }
        return findFront(x,tn[root].left,ans);
    }  
}

//找x的后继
int findBack(int x, int root, int ans){
    if(x < tn[root].value){
        if(tn[root].left == 0){
            return tn[root].value;
        }
        return findBack(x, tn[root].left, tn[root].value);
    }
    else {
        if(tn[root].right == 0){
            return MAXINT;
        }
        return findBack(x, tn[root].right, ans);
    }
}


// 排名为x的数
int findByOrder(int x, int root){
    if(x == 0){
        return 0;
    }

    if(x <= tn[tn[root].left].size)
    {
        return findByOrder(x, tn[root].left);
    }
    else if(x <= tn[tn[root].left].size+ tn[root].num){
        return tn[root].value;
    }
    else{
        return findByOrder(x- tn[tn[root].left].size -tn[root].num, tn[root].right);
    }
}

// 找值x的排名
int findByValue(int x, int root){
    if(root == 0)
        return 0;
    if(tn[root].value > x){
        return findByValue(x,tn[root].left);
    }
    else if(tn[root].value < x){
        return tn[tn[root].left].size+tn[root].num+findByValue(x, tn[root].right);
    }
    else
    {
        return tn[tn[root].left].size + tn[root].num;
    }
    
}

// 添加节点
inline int insert2(int value, int root){
    tn[root].size++;
    if(value < tn[root].value){
        if(tn[root].left != 0){
            insert2(value,tn[root].left);
        }
        else{
            //tn[next_point] = TreeNode(0,0,1,value,1);
            tn[next_point].value = value;
            tn[next_point].size = 1;
            tn[next_point].num = 1;
            tn[root].left = next_point;
            
            next_point++;
        }  
    }
    else if(value > tn[root].value){
        if(tn[root].right != 0)
            insert2(value, tn[root].right);
        else{
            //tn[next_point] = TreeNode(0,0,1,value,1);
            tn[next_point].value = value;
            tn[next_point].size = 1;
            tn[next_point].num = 1;
            tn[root].right = next_point;
            next_point ++;
        }

    }
    else{
        tn[root].num++;
        
    }
    return 0;
}



int main(){
    int n, opt, x;
    cin>>n;
    
    while(n--){
        cin>>opt>>x;
        switch (opt)
        {
        case 1:
            cout<<findByValue(x,1)<<endl;
            break;
        case 2:
            cout<<findByOrder(x,1)<<endl;
            break;
        case 3:
            cout<<findFront(x,1,-MAXINT)<<endl;
            // count<<findByOrder(findByValue(x,1)-1,1)<<endl;

            break;
        case 4:
            cout<<findBack(x,1,MAXINT)<<endl;
            // count<<findByOrder(findByValue(x,1)+1,1)<<endl;   //method 2
            // count<<findByOrder(findByValue(x+1,1),1)<<endl;   //method 3
            break; 
        case 5:
            if(tn[1].num == 0){
                tn[1] = TreeNode(0,0,1,x,1);
                next_point++;
            }
            else{
                insert2(x,1);
            }
            break;                         
        default:
            break;
        }
    }

    return 0;
}
2021/5/11 14:31
加载中...