Pairing Heap模板

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1000010;
struct Node {
    int key;
    Node *l, *r;    
    inline void sc(Node *c);
} Tnull, *null = &Tnull, *root = null;
Node mem[N], *C = mem;

inline Node* newNode(int v) {
    C->key = v;
    C->l = C->r = null;
    return C++;
}

inline void Node::sc(Node *c) {
//    if (l != null)
c->r = l; l = c; } Node* merge(Node *A, Node *B) { if (A == null) return B; if (B == null) return A; if (A->key < B->key) { A->sc(B); return A; } else { B->sc(A); return B; } return null; } inline int top(Node *t) { return t->key; } Node* push(Node *t, int key) { return merge(newNode(key), t); } Node* merge(Node *t) { if (t == null || t->r == null) return t; Node *A = t, *B = t->r, *New = t->r->r; A->r = B->r = null; return merge(merge(A, B), merge(New)); } Node* pop(Node *t) { return twoPassMerge(t->l); } inline void read(int &x) { static char buf = getchar(); x = 0; for (; !isdigit(buf); buf = getchar()); for (; isdigit(buf); buf = getchar()) x = x * 10 + buf - '0'; } int main() { int n; read(n); for (int t, i = 0; i < n; ++i) read(t), root = push(root, t); for (int i = 0; i < n; ++i) { printf("%d ", top(root)); root = pop(root); } return 0; }

顺便这道题http://cogs.pro/cogs/problem/problem.php?pid=279,我调了一上午+半个下午,居然是因为可并堆打错了。。。

posted @ 2017-08-16 15:10  p0ny  阅读(265)  评论(0编辑  收藏  举报