Skip to content

Instantly share code, notes, and snippets.

@cgiosy
Last active September 5, 2020 12:02
Show Gist options
  • Save cgiosy/14d0076e7f735c0a05890ad3c8071096 to your computer and use it in GitHub Desktop.
Save cgiosy/14d0076e7f735c0a05890ad3c8071096 to your computer and use it in GitHub Desktop.
PQ 두 개로 최소/최대만 필요한 set 대체하기
template<class T=int, class O=less<T>>
struct heap_set {
priority_queue<T, vector<T>, O> q, del;
const T& top() const { return q.top(); }
int size() const { return int(q.size()-del.size()); }
bool empty() const { return !size(); }
void flush() { while(del.size() && q.top()==del.top()) q.pop(), del.pop(); }
void insert(const T x) { q.push(x); flush(); }
void pop() { q.pop(); flush(); }
void erase(const T x) { del.push(x); flush(); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment