为什么全re了!
查看原帖
为什么全re了!
755043
sinestrea楼主2023/10/27 17:22

萌新刚学oi,把re的数据下下来,结果本地过了,洛谷ide过了,但一交上去就re,不懂啊

#include <bits/stdc++.h>

const int MAX = 10000005;

class Node {
public:
    int val{}, dis{}, l{}, r{};
} node[MAX]{};

class priority_queue {
public:
    int root{}, cnt{};

    int merge(int x, int y) {
        if (!x || !y) return x + y;
        if (node[x].val > node[y].val) std::swap(x, y);
        node[x].r = merge(node[x].r, y);
        if (node[x].l == 0 || node[node[x].l].dis < node[node[x].r].dis) std::swap(node[x].l, node[x].r);
        if (node[x].r == 0) node[x].dis = 0;
        else node[x].dis = node[node[x].r].dis + 1;
        return x;
    }

    void push(int val) {
        node[++cnt].val = val;
        root = merge(root, cnt);
    }

    int top() { return node[root].val; }

    int pop() { 
        if (root != 0) root = merge(node[root].l, node[root].r); 
    }

    bool empty() { return root == 0; }
} t{};

int n{}, num{};
int opt{};

int main() {
    std::ios::sync_with_stdio(0);
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> opt;
        if (opt == 1) {
            std::cin >> num;
            t.push(num);
        }
        else if (opt == 2) {
            std::cout << t.top() << '\n';
        }
        else t.pop();
    }
}

另外再提供一份过了的代码

#include <bits/stdc++.h>


class node {
public:
    int val{}, dis{};
    node *l{}, *r{};
    node (int num) { val = num; }
};


class priority_queue {
public:
    node *root{};

    node *merge(node *x, node *y) {
        if (x == nullptr) return y;
        if (y == nullptr) return x;
        if (x -> val > y -> val) std::swap(x, y);
        x -> r = merge(x -> r, y);
        if (x -> l == nullptr || x -> l -> dis < x -> r -> dis) std::swap(x -> l, x -> r);
        if (x -> r == nullptr) x -> dis = 0;
        else x -> dis = x -> r -> dis + 1;
        return x;
    }

    void insert(int num) {
        root = merge(root, new node{num});
    }

    void pop() {
        if (root == nullptr) return;
        node *n = root;
        root = merge(root -> l, root -> r);
        delete n;
    }

    bool empty() { return root == nullptr; }

    int top() { return root -> val; }
}t{};


int n{}, num{};
int opt{};


int main() {
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> opt;
        if (opt == 1) {
            std::cin >> num;
            t.insert(num);
        }
        else if (opt == 2) {
            std::cout << t.top() << '\n';
        }
        else t.pop();
    }
}

提供一组re的数据:

in:

15
1 5384
2
3
1 4337
1 291
3
1 3776
2
1 2490
1 3903
1 5038
1 4504
3
3
3

out:

5384
3776
2023/10/27 17:22
加载中...