[Balanced binary tree] buy tickets

Source: Internet
Author: User
DescriptionRailway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join along queue…The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there.Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the nationalteam of Olympiad in Informatics.It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in thequeue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none theless better than freezing to death!People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by thepeople adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and allthe information about those who have jumped the queue and where they stand after queue-jumping is given, canI find out the final order of people in the queue?” Thought the Little Cat.InputThere will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posiand Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Valiare as follows:    Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person inthe queue. The booking office was considered the 0th person and the person at the front of the queue wasconsidered the first person in the queue.    Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.There no blank lines between test cases. Proceed to the end of input.OutputFor each test cases, output a single line of space-separated integers which are the values of people inthe order they stand in the queue.Sample Input40 771 511 332 6940 205231 192431 38900 31492Sample Output77 33 69 5131492 20523 3890 19243HintThe figure below shows how the Little Cat found out the final order of people in the queue described in thefirst test case of the sample input.

When you see this question, the first thing that comes to mind is the jump table, but the timeout is super space.
So I switched to SBT and it soon went through.
During insertion, data is not searched by keywords, but inserted by location.

# Include <cstdio> # include <cstdlib> # include <algorithm> # include <string> const int maxn = 400010, INF = 0x3f3f3f; int SZ [maxn], LC [maxn]; int RC [maxn], key [maxn]; int N, T, TOT; inline void Zig (Int & T) {int TMP = Lc [T]; LC [T] = RC [TMP]; RC [TMP] = T; SZ [TMP] = SZ [T]; SZ [T] = SZ [LC [T] + SZ [RC [T] + 1; t = TMP; return;} inline void zag (Int & T) {int TMP = RC [T]; RC [T] = Lc [TMP]; LC [TMP] = T; SZ [TMP] = SZ [T]; SZ [T] = SZ [LC [T] + SZ [RC [T] + 1; t = TMP; return;} void maintain (Int & T) {If (SZ [LC [LC [T]> SZ [RC [T]) Zig (t ); else if (SZ [RC [LC [T]> SZ [RC [T]) {zag (LC [T]); zig (t );} else if (SZ [RC [RC [T]> SZ [LC [T]) zag (t ); else if (SZ [LC [RC [T]> SZ [LC [T]) {Zig (RC [T]); zag (t);} else return; maintain (LC [T]); maintain (RC [T]); maintain (t); return;} void ins (Int & T, int V, int K) {If (! T) {SZ [T = ++ tot] = 1; key [tot] = V; return;} + SZ [T]; if (k <= SZ [LC [T]) ins (LC [T], v, k); else ins (RC [T], V, k-SZ [LC [T]-1); maintain (t); return;} void inorder (INT t) {If (LC [T]) inorder (LC [T]); If (Key [T] <inf) printf ("% d", key [T]); If (RC [T]) inorder (RC [T]); return;} inline int getint () {int res = 0; char TMP; while (! Isdigit (TMP = getchar (); Do res = (RES <3) + (RES <1) + TMP-'0 '; while (isdigit (TMP = getchar (); Return res;} int main () {freopen ("ticket. in "," r ", stdin); freopen (" ticket. out "," W ", stdout); While (scanf (" % d ", & N )! = EOF) {memset (Key, 0, sizeof key); memset (SZ, 0, sizeof sz); memset (LC, 0, sizeof Lc); memset (RC, 0, sizeof RC); t = tot = 0; // note that it is cleared. For (INT I = 0; I <n; ++ I) {int Pos = getint (), val = getint (); ins (T, Val, POS );} inorder (t); printf ("\ n") ;}return 0 ;}

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.