[代码技巧]可删除的优先队列

这里说一下为什么不用multiset

  1. 平衡树实现,常数大
  2. 维护的东西多,空间复杂度常数也大,比如HNOI2016网络那题,multiset被卡空间了
struct MyHeap {
    priority_queue<int> q1, q2;//默认大根堆
    void push(int x) {//插入元素
        q1.push(x);
    }
    void del(int x) {//删除元素
        q2.push(x);
    }
    int top() {//返回堆顶元素
        while (q1.size() && q2.size() && q1.top() == q2.top()) q1.pop(), q2.pop();
        if (q1.size()) return q1.top();
        else return -1;
    }
} Heap;
原文地址:https://www.cnblogs.com/cnyali-Tea/p/10554660.html