Balance Tree Treap template and principle, tree treap Template
This time, let's talk about Treap (splay later)
The Balance Tree is a Sort binary tree (or binary search tree), so the sort binary tree can quickly determine the size of the two values, of course, there must be more operations (or what else we will learn ).
The Balance Tree mainly adds an optimization based on the binary tree sorting: It is highly balanced and can be dynamically balanced.
The treap we will talk about today is a dynamic balancing method.
First, I am sorry, because there is not much time, so I cannot talk about the left-hand and right-hand operations, but I can read the Blue Book of Liu lujia, it is quite clear.
Start now.
Treap uses a more mysterious method, that is, to attach a random value to each vertex, and then follow the heap nature, regardless of the size of the root heap, therefore, each time we add a value, we use the floating operation to rotate it to maintain the nature of the heap without changing the nature of the sort binary tree.
The operation is easy to understand, that is, the judgment can be performed each time the insert operation is performed. If it is not satisfied, the Operation will go up.
I will not go into details about the specific Binary Tree sorting operation. If you want to learn more, you can read the Blue Book of Liu lujia (I feel like I am advertising with Liu lujia ).
The following code is provided.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <ctime> 6 using namespace std; 7 int n, root, size, ans; 8 struct P {9 int l, r, sz, key, rd, re; // repeat times, key is added with a weight of 10} t [1000005]; 11 void update (int k) 12 {13 t [k]. sz = t [t [k]. l]. sz + t [t [k]. r]. sz + t [k]. re; 14} 15 void left (int & k) // right-hand 16 {17 int y = t [k]. r; 18 t [k]. r = t [y]. l; 19 t [y]. l = k; 20 t [y]. sz = t [k]. sz; 21 update (k); 22 k = y; 23} 24 void right (int & k) // left-hand 25 {26 int y = t [k]. l; 27 t [k]. l = t [y]. r; 28 t [y]. r = k; 29 t [y]. sz = t [k]. sz; 30 update (k); 31 k = y; 32} 33 void init (int & k, int x) // Add operation 34 {35 if (k = 0) 36 {37 size ++; 38 k = size; 39 t [k]. sz = 1; 40 t [k]. re = 1; 41 t [k]. key = x; 42 t [k]. rd = rand (); 43 return; 44} 45 t [k]. sz ++; 46 if (t [k]. key = x) t [k]. re ++; 47 else {48 if (x> t [k]. key) 49 {50 init (t [k]. r, x); 51 if (t [t [k]. r]. rd <t [k]. rd) left (k); // both here and below determine whether the heap is satisfied. 52} 53 if (x <t [k]. key) 54 {55 init (t [k]. l, x); 56 if (t [t [k]. l]. rd <t [k]. rd) right (k); 57} 58} 59} 60 void del (int & k, int x) 61 {62 if (k = 0) return; 63 if (t [k]. key = x) 64 {65 if (t [k]. re> 1) 66 {67 t [k]. re --; 68 t [k]. sz --; 69 return; 70} 71 if (t [k]. l * t [k]. r = 0) k = t [k]. l + t [k]. r; // k indicates the movement of the pointer, so it is moved to the left or right son 72 else 73 {74 if (t [t [k]. l]. rd <t [t [k]. r]. rd) {75 right (k); 76 del (k, x); 77} 78 else {79 left (k); 80 del (k, x ); 81} 82} 83} 84 else {85 if (x> t [k]. key) 86 {87 t [k]. sz --; 88 del (t [k]. r, x); 89} 90 else {91 t [k]. sz --; 92 del (t [k]. l, x); 93} 94} 95} 96 int rank1 (int k, int x) // find the rank 97 of x {98 if (k = 0) return 0; 99 if (t [k]. key = x) return t [t [k]. l]. sz + 1; 100 if (x> t [k]. key) return t [t [k]. l]. sz + rank1 (t [k]. r, x) + t [k]. re; 101 if (x <= t [k]. key) return rank1 (t [k]. l, x); 102} 103 int rank2 (int k, int x) // find the number of ranking x 104 {105 if (k = 0) return 0; 106 else if (x <= t [t [k]. l]. sz) return rank2 (t [k]. l, x); 107 else if (x> t [t [k]. l]. sz + t [k]. re) return rank2 (t [k]. r, x-t [t [k]. l]. sz-t [k]. re); 108 else return t [k]. key; 109} 110 void pre (int k, int x) // find the precursor 111 {112 if (k = 0) return; 113 if (t [k]. key <x) 114 {115 ans = k; 116 pre (t [k]. r, x); 117} 118 else pre (t [k]. l, x); 119} 120 void nxt (int k, int x) // find the successor 121 {122 if (k = 0) return; 123 if (t [k]. key> x) 124 {125 ans = k; 126 nxt (t [k]. l, x); 127} 128 else nxt (t [k]. r, x); 129} 130 int main () 131 {132 srand (time (0); 133 scanf ("% d", & n ); 134 for (int I = 1; I <= n; I ++) 135 {136 int num, x; 137 scanf ("% d", & num, & x); 138 if (num = 1) init (root, x); 139 if (num = 2) del (root, x ); 140 if (num = 3) printf ("% d \ n", rank1 (root, x); 141 if (num = 4) printf ("% d \ n", rank2 (root, x); 142 if (num = 5) 143 {144 pre (root, x ); 145 printf ("% d \ n", t [ans]. key); 146} 147 if (num = 6) 148 {149 nxt (root, x); 150 printf ("% d \ n", t [ans]. key); 151} 152} 153 return 0; 154}
Template question: https://www.luogu.org/problemnew/show/P3369
Thank you for watching.
If anything is wrong, please point it out.