POJ 3481 SBT Basics

Source: Internet
Author: User

1 indicates that the customer K is inserted, the priority is P (equivalent to the size), and 2 indicates that the customer with the highest priority is output (that is, the maximum value is found) and deleted. 3. Similarly, the output level is the lowest. Idea: The get_min () and get_max () operations in SBT can be easily completed. For details, see [cpp] # include <iostream> # include <cstdio> # include <algorithm> # include <string> # include <cmath> # include <cstring> # include <queue> # include <set> # include <vector> # include <stack> # include <map> # include <iomanip> # define PI acos (-1.0) # define Max 100005 # define inf 1 <28 # define LL (x) (x <1) # define RR (x) (x <1 | 1) # define FOR (I, s, t) for (int I = (s); I <= (t); ++ I) # define ll long # define Mem (a, B) memset (a, B, sizeof (a) # define mp (a, B) make_pair (a, B) using namespace std; struct SBT {int left, right, num, size, priority;} tree [Max]; void left_rot (int & x) // left-hand {int y = tree [x]. right; tree [x]. right = tree [y]. left; tree [y]. left = x; tree [y]. size = tree [x]. size; tree [x]. size = tree [tree [x]. left]. size + tree [tree [x]. right]. size + 1; x = y;} void right_rot (int & x) // right-hand {int y = tree [x ]. Left; tree [x]. left = tree [y]. right; tree [y]. right = x; tree [y]. size = tree [x]. size; tree [x]. size = tree [tree [x]. left]. size + tree [tree [x]. right]. size + 1; x = y;} void maintain (int & x, bool flag) // update {if (! Flag) // The left subtree is 0 {if (tree [tree [x]. left]. left]. size> tree [tree [x]. right]. size) right_rot (x); else if (tree [tree [x]. left]. right]. size> tree [tree [x]. right]. size) {left_rot (tree [x]. left); right_rot (x);} else return;} else // The right subtree is 1 {if (tree [tree [tree [x]. right]. right]. size> tree [tree [x]. left]. size) left_rot (x); else if (tree [tree [x]. right]. left]. size> tree [tree [x]. left]. size) {right_rot (Tree [x]. right); left_rot (x);} else return;} maintain (tree [x]. left, 0); maintain (tree [x]. right, 1); maintain (x, 1); maintain (x, 0);} int root, top; void insert (int & x, int num, int priority) // insert here, p is the priority, that is, the size stored in the SBT, And that num can be understood as the customer's name and output only. {If (x = 0) {x = ++ top; tree [x]. size = 1; tree [x]. left = tree [x]. right = 0; tree [x]. num = num; tree [x]. priority = priority;} else {tree [x]. size ++; if (priority <tree [x]. priority) insert (tree [x]. left, num, priority); else insert (tree [x]. right, num, priority); maintain (root, priority> = tree [x]. priority) ;}} int del (int & x, int priority) {int d_priority; if (! X) return 0; tree [x]. size --; if (priority = tree [x]. priority | (tree [x]. priority> priority & tree [x]. left = 0) | (tree [x]. priority <priority & tree [x]. right = 0) {d_priority = tree [x]. priority; if (tree [x]. left & tree [x]. right) {tree [x]. priority = del (tree [x]. left, tree [x]. priority + 1);} else x = tree [x]. left + tree [x]. right;} else if (priority> tree [x]. priority) d_priority = del (tree [X]. right, priority); else if (priority <tree [x]. priority) d_priority = del (tree [x]. left, priority); return d_priority;} int get_min () {int x; for (x = root; tree [x]. left; x = tree [x]. left); printf ("% d \ n", tree [x]. num); return tree [x]. priority; // return this value, used to delete} int get_max () {int x; for (x = root; tree [x]. right; x = tree [x]. right); printf ("% d \ n", tree [x]. num); return tree [x]. priority; // similarly} Int main () {int n; root = top = 0; while (scanf ("% d", & n) {if (n = 1) {int a, B; scanf ("% d", & a, & B); insert (root, a, B);} else if (n = 2) {if (! Root) puts ("0"); else {int k = get_max (); del (root, k) ;}} else {if (! Root) puts ("0"); else {int k = get_min (); del (root, k) ;}} return 0 ;}

Related Article

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.