C + + path advanced--treap tree (normal balance tree)

Source: Internet
Author: User

4543 Normal Balance tree

time limit: 1 sspace limit: 128000 KBtopic rank: Master Master SolvingTitle Description Description

It's a water problem.

By the way, I wish Lez and Zqq Akakak

You need to write a data structure (refer to the title of the topic) to maintain some of the numbers, which need to provide the following actions:
1. Insert X number
2. Delete the number of x (if there are multiple identical numbers, because only one is deleted)
3. Query the rank of x number (if there are multiple identical numbers, because the output is the lowest ranking)
4. The number of queries ranked as X
5. Find the precursor of X (the precursor is defined as less than X and the maximum number)
6. The successor of X (subsequent definition is greater than X, and the smallest number)

Enter a description Input Description

The first action N, which represents the number of operations, the following n rows have two numbers per row opt and x,opt denote the ordinal number of the operation (1<=OPT<=6)

Output description Output Description

Output one number per line for the operation 3,4,5,6, indicating the corresponding answer

Sample input Sample Input

10

1 10

1 10

1 10

1 10

1 10

1 10

1 10

1 10

1 10

1 10

Sample output Sample Output

EOF (No output)

Data range and Tips Data Size & Hint

n=100000 all numbers are within -2*10^9 to 2*10^9

In fact, n=5000000 ... But in order to not pass the evaluation machine

The key: There is no technical content, is TREAP basic operation. Code:
1#include <iostream>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 structdata{6     intl,r,v,size,rnd,w;7}tr[100005];8 intN,size,root,ans;9 voidUpdateintK//Update node InformationTen { OneTr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+TR[K].W; A } - voidRturn (int&k) - { the     intT=tr[k].l;tr[k].l=tr[t].r;tr[t].r=K; -Tr[t].size=tr[k].size;update (k); k=T; - } - voidLturn (int&k) + { -     intT=tr[k].r;tr[k].r=tr[t].l;tr[t].l=K; +Tr[t].size=tr[k].size;update (k); k=T; A } at voidInsertint&k,intx) - { -     if(k==0) -     { -size++;k=size; -tr[k].size=tr[k].w=1; tr[k].v=x;tr[k].rnd=rand (); in     return; -     } totr[k].size++; +     if(tr[k].v==x) tr[k].w++;//each node records the number of the same values as the nodes by the way -     Else if(x>tr[k].v) the     { * Insert (tr[k].r,x); $     if(Tr[tr[k].r].rnd<tr[k].rnd) Lturn (k);//Maintenance Heap PropertiesPanax Notoginseng     } -     Else  the     { + Insert (tr[k].l,x); A     if(tr[tr[k].l].rnd<tr[k].rnd) Rturn (k); the     }  + } - voidDelint&k,intx) $ { $     if(k==0)return;  -     if(tr[k].v==x) -     { the     if(tr[k].w>1) -     {Wuyitr[k].w--;tr[k].size--;return;//If there are more than one number of the same value, delete a the     } -     if(tr[k].l*tr[k].r==0) K=TR[K].L+TR[K].R;//There was a son who was empty Wu     Else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd) - Rturn (k), Del (k,x); About     ElseLturn (k), Del (k,x); $     } -     Else if(x>tr[k].v) -tr[k].size--, Del (tr[k].r,x); -     Elsetr[k].size--, Del (tr[k].l,x); A } + intQuery_rank (intKintx) the { -     if(k==0)return 0; $     if(tr[k].v==x)returnTr[tr[k].l].size+1; the     if(X&GT;TR[K].V)returntr[tr[k].l].size+tr[k].w+Query_rank (tr[k].r,x); the     Else returnQuery_rank (tr[k].l,x); the } the intQuery_num (intKintx) - { in     if(k==0)return 0; the     if(x<=tr[tr[k].l].size) the     returnQuery_num (tr[k].l,x); About     Else if(x>tr[tr[k].l].size+TR[K].W) the     returnQuery_num (tr[k].r,x-tr[tr[k].l].size-TR[K].W); the     Else returntr[k].v; the } + voidQuery_pro (intKintx) - { the     if(k==0)return;Bayi     if(tr[k].v<x) the     { theans=K;query_pro (tr[k].r,x); -     } -     ElseQuery_pro (tr[k].l,x); the } the voidQuery_sub (intKintx) the { the     if(k==0)return; -     if(tr[k].v>x) the     { theans=k;query_sub (tr[k].l,x); the     }94     Elsequery_sub (tr[k].r,x); the } the intMain () the {98scanf"%d",&n); About     intopt,x; -      for(intI=1; i<=n;i++)101     {102scanf"%d%d",&opt,&x);103     Switch(opt)104     { the      Case 1: Insert (ROOT,X); Break;106      Case 2:d El (root,x); Break;107      Case 3:p rintf ("%d\n", Query_rank (root,x)); Break;108      Case 4:p rintf ("%d\n", Query_num (root,x)); Break;109      Case 5: ans=0; Query_pro (root,x);p rintf ("%d\n", TR[ANS].V); Break; the      Case 6: ans=0; Query_sub (root,x);p rintf ("%d\n", TR[ANS].V); Break;111     } the     }113     return 0; the}

C + + path advanced--treap tree (normal balance tree)

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.