POJ3481 Double Queue

題目連結

  • 題意:需做出一資料結構有以下操作:(1)加入一個優先值 $P$ 的資料 $K$ (2) 找到一個最低 / 高優先值的資料,輸出資料的值並刪除。
  • 題解:平衡樹題目,我用 treap 實作。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    #pragma GCC optimize(2)
    #include <cstring>
    #include <iostream>
    #include <string>
    #include <vector>

    using namespace std;
    const int INF = 1e9;
    const int MXN = 1e6 + 5;
    const int MXV = 0;
    #define MP make_pair
    #define PB push_back
    #define F first
    #define S second
    #define FOR(i, L, R) for (int i = L; i != (int)R; ++i)
    #define FORD(i, L, R) for (int i = L; i != (int)R; --i)
    #define IOS \
    cin.tie(NULL); \
    cout.tie(NULL); \
    ios_base::sync_with_stdio(false);
    struct Treap
    {
    int key, val, pri, sz;
    Treap *lc, *rc;
    Treap() {}
    Treap(int _key, int _val)
    {
    key = _key;
    val = _val;
    pri = rand();
    sz = 1;
    lc = rc = NULL;
    }
    };

    int getSize(Treap *a) { return (a == NULL ? 0 : a->sz); }

    void split(Treap *t, Treap *&a, Treap *&b, int k)
    {
    if (t == NULL)
    {
    a = b = NULL;
    return;
    }
    // cout << t->key << ' ' << k << '\n';
    if (t->key <= k)
    {
    a = t;
    split(t->rc, a->rc, b, k);
    }
    else
    {
    b = t;
    split(t->lc, a, b->lc, k);
    }
    }

    Treap *merge(Treap *a, Treap *b)
    {
    if (!a || !b)
    {
    return (a ? a : b);
    }
    if (a->pri > b->pri)
    {
    a->rc = merge(a->rc, b);
    return a;
    }
    else
    {
    b->lc = merge(a, b->lc);
    return b;
    }
    }

    void Insert(Treap *&t, int key, int val)
    {
    // cout << "ins " << key << ' ' << val << '\n';
    Treap *a, *b;
    split(t, a, b, key);
    t = merge(a, merge(new Treap(key, val), b));
    }

    void Delete(Treap *&t, int val)
    {
    // cout << "del " << val << '\n';
    Treap *a, *b, *c;
    split(t, b, c, val);
    split(b, a, b, val - 1);
    t = merge(a, c);
    }

    int find(Treap *t, int x)
    {
    if (x)
    {
    if (t->lc)
    {
    return find(t->lc, x);
    }
    }
    else
    {
    if (t->rc)
    {
    return find(t->rc, x);
    }
    }
    printf("%d\n", t->val);
    return t->key;
    }

    int main()
    {
    Treap *root = NULL;
    int cmd;
    while (scanf("%d", &cmd), cmd)
    {
    if (cmd == 1)
    {
    int k, p;
    scanf("%d %d", &k, &p);
    Insert(root, p, k);
    }
    else
    {
    if (root == NULL)
    {
    printf("%d\n", 0);
    continue;
    }
    int key = find(root, cmd - 2);
    Delete(root, key);
    }
    }
    }

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts