HDU 5818 Joint Stacks (left-leaning tree)

Source: Internet
Author: User

Topic Link: Click to open the link

Ideas:

The key to the problem is how to merge two stacks, we can use a data structure called left-leaning tree, to satisfy the nature of the heap and the nature of the collection, to support the complexity of O (logn) to delete the top element of the heap, insert an element, merge two of the heap.

See the code for details:

#include <bits/stdc++.h>using namespace Std;typedef pair<int, int> p;const int maxn = 152400;    P v[maxn];int tot, u, A, B, L[MAXN], R[MAXN], d[maxn];//id[x] represents the number of the tree where x is the int Merge (int x, int y) {if (!x) return y;    if (!y) return x;    if (V[x] < v[y]) swap (x, y);    R[X] = Merge (r[x], y);    if (D[l[x]] < d[r[x]]) swap (l[x], r[x]);    D[X] = d[r[x]] + 1; return x;}    int init (P x) {tot++;    V[tot] = x;    L[tot] = R[tot] = D[tot] = 0; return tot;} int Insert (int x, P y) {return Merge (x, init (y));} P Top (int x) {return v[x];} int pop (int x) {return Merge (L[x], r[x]);}    Char op[30], str[30], str2[30];int val;int main () {int kase = 0;    int N;        while (scanf ("%d", &n), N) {memset (V, 0, sizeof (v));        memset (l, 0, sizeof (l));        memset (r, 0, sizeof (r));        memset (d, 0, sizeof (d));        tot = U = A = b = 0;        printf ("Case #%d:\n", ++kase);        int PosA = init (P (-1,-1));        int PosB = init (P (-2,-1)); for (int i = 1; I <= N;            ++i) {scanf ("%s%s", op, str);                if (op[1] = = ' U ') {scanf ("%d", &val);                if (str[0] = = ' A ') {PosA = Insert (PosA, P (i, Val));                } else {PosB = Insert (PosB, P (i, Val));                }} else if (op[1] = = ' O ') {P ans;                    if (str[0] = = ' A ') {ans = top (PosA);                PosA = Pop (PosA);                    } else {ans = top (PosB);                PosB = Pop (PosB);            } printf ("%d\n", Ans.second);                } else if (op[1] = = ' E ') {scanf ("%s", str2);                    if (str[0] = = ' A ') {PosA = Merge (PosA, PosB);                PosB = init (P (-I,-1));                    } else {PosB = Merge (PosA, PosB);                PosA = init (P (-I,-1));  }            }        }    }  return 0;} 


HDU 5818 Joint Stacks (left-leaning tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.