求助,样例+本地构造数据通过,但Subtask#0全WA
查看原帖
求助,样例+本地构造数据通过,但Subtask#0全WA
438374
ImpactwasXD_楼主2024/5/17 16:37
#include <iostream>
#include <cstdio>
#include <cstring>
#include <limits>
#include <vector>
using namespace std;

const int err = numeric_limits<int>::max();
struct Node {
    int val, sub;
    Node *left, *right, *up;
    Node (int _val, int _sub, Node *_left = nullptr, Node *_right = nullptr) : val(_val), sub(_sub), left(_left), right(_right) {}
};
Node *origin;
void insertNode(int x) {
    if (origin == nullptr) {
        origin = new Node(x, 1);
        return;
    }
    Node *p = origin;
    while (true) {
        p->sub++;
        if (p->val > x) {
            if (p->left == nullptr) {
                p->left = new Node(x, 1);
                return;
            } else {
                p = p->left;
            }
        } else {
            if (p->right == nullptr) {
                p->right = new Node(x, 1);
                return;
            } else {
                p = p->right;
            }
        }
    }
}
int checkRank(int x) {
    Node *p = origin;
    int prank = 1;
    if (p->right != nullptr) prank = p->sub - p->right->sub;
    else prank = p->sub;
    while (p != nullptr && p->val != x) {
        if (p->val > x) {
            p = p->left;
            if (p != nullptr && p->right != nullptr) prank = prank - 1 - p->right->sub;
            else if (p != nullptr) prank = prank - 1;
            else prank = prank - 1;
        } else {
            p = p->right;
            if (p != nullptr && p->right != nullptr) prank = prank + p->sub - p->right->sub;
            else if (p != nullptr) prank = prank + p->sub;
            else prank = prank + 1;
        }
    }
    return prank;
}
int findPrev(int x) {
    Node *p = origin;
    int ans = -err;
    while (true) {
        if (p->val >= x) {
            if (p->left != nullptr) p = p->left;
            else return ans;
        } else {
            ans = p->val;
            if (p->right != nullptr) p = p->right;
            else return ans;
        }
    }
}
int findNext(int x) {
    Node *p = origin;
    int ans = err;
    while (true) {
        if (p->val <= x) {
            if (p->right != nullptr) p = p->right;
            else return ans;
        } else {
            ans = p->val;
            if (p->left != nullptr) p = p->left;
            else return ans;
        }
    }
}
int findRank(int x) {
    Node *p = origin;
    int prank = 1;
    if (p->right != nullptr) prank = p->sub - p->right->sub;
    else prank = p->sub;
    while (prank != x) {
        if (prank > x) {
            p = p->left;
            if (p->right != nullptr) prank = prank - 1 - p->right->sub;
            else prank = prank - 1;
        } else {
            p = p->right;
            if (p->right != nullptr) prank = prank + p->sub - p->right->sub;
            else prank = prank + p->sub;
        }
    }
    return p->val;
}
int main() {
    int k;
    scanf("%d", &k);
    while (k--) {
        int opt, x;
        scanf("%d%d", &opt, &x);
        switch(opt) {
        case 1:
            printf("%d\n", checkRank(x)); break;
        case 2:
            printf("%d\n", findRank(x)); break;
        case 3:
            printf("%d\n", findPrev(x)); break;
        case 4:
            printf("%d\n", findNext(x)); break;
        case 5:
            insertNode(x); break;
        }
    }
    return 0;
}

指针写法,不知道存在思路缺陷还是题目理解有问题

2024/5/17 16:37
加载中...