码迷,mamicode.com
首页 > 其他好文 > 详细

Pairing Heap模板

时间:2017-08-16 17:04:00      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:using   log   read   namespace   class   getchar   const   null   ret   

#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;
}

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,我调了一上午+半个下午,居然是因为可并堆打错了。。。

Pairing Heap模板

标签:using   log   read   namespace   class   getchar   const   null   ret   

原文地址:http://www.cnblogs.com/p0ny/p/7373464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!