/*************************************** * ********* Data structure: SBT (size balanced tree), also known as silly tree; data field: Value Field key, left child left, right child right, to maintain a balanced size; nature: the size of each subtree is not smaller than that of its brother. insert: insert Algorithm Insert a node and call a maintenance process to maintain its nature. Delete: The delete operation is the same as the Binary Search Tree of the general maintenance size field. Maximum Value and minimum value: because SBT itself has maintained the size field, you only need to use select (T, 1) to calculate the maximum value; select (t, t. size) calculates the minimum value. The select (T, k) function returns the node value of the tree T at the K position; **************************************** * ********/# include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <Algorithm> using namespace STD; const int n = 100000; int key [N], lefts [N], rights [N], size [N]; int U; // int node of the root node; inline void left_rotate (Int & X) {int K = rights [X]; rights [x] = lefts [k]; lefts [k] = X; size [k] = size [X]; Size [x] = size [lefts [x] + size [rights [x] + 1; X = K ;} inline void right_rotate (Int & Y) {int K = lefts [y]; lefts [y] = rights [k]; rights [k] = y; size [k] = size [y]; Size [y] = size [lefts [y] + size [rights [y] + 1; y = K ;} void maintain (Int & U, bool flag) // maintenance {If (flag = false) {If (size [lefts [lefts [u]> size [rights [u]) right_rotate (U ); else {If (size [rights [lefts [u]> size [rights [u]) {left_rotate (lefts [u]); right_rotate (U );} else return ;}} else {If (size [rights [rights [u]> size [lefts [u]) left_rotate (U ); else {If (size [lefts [rights [u]> size [lefts [u]) {right_rotate (rights [u]); left_rotate (U );} else return ;}} maintain (lefts [u], false); maintain (rights [u], true); maintain (u, true); maintain (u, false );} void insert (Int & U, int v) // insert node {If (u = 0) {key [U = ++ node] = V; size [u] = 1;} else {size [u] ++; If (v <key [u]) insert (lefts [u], V ); else insert (rights [u], V); maintain (u, v> = Key [u]) ;}} int Delete (Int & U, int V) // Delete the node {size [u] --; If (V = Key [u]) | (v <key [u] & lefts [u] = 0) | (V> key [u] & Rights [u] = 0 )) {int r = Key [u]; If (lefts [u] = 0 | rights [u] = 0) u = lefts [u] + rights [u]; else key [u] = Delete (lefts [u], key [u] + 1); return r;} else {If (v <key [u]) return Delete (lefts [u], V); else return Delete (rights [u], v) ;}} int search (int x, int K) // query {If (x = 0 | K = Key [x]) return X; If (k <key [x]) return search (lefts [X], k); else return search (rights [X], k);} int select (int u, int K) // return the node value of the tree at the K position {int r = size [lefts [u] + 1; if (k = r) Return key [u]; else if (k <r) return select (lefts [u], k); else return select (rights [u], K-R);} int successor (int u, int K) // query the successor of node K {If (u = 0) return K; If (Key [u] <= k) return successor (rights [u], k); else {int r = successor (lefts [u], k); If (r = k) Return key [u]; else return r ;}} int predecessor (int u, int K) // query the precursor of node K {If (u = 0) return K; If (Key [u]> = K) return predecessor (lefts [u], k); else {int r = predecessor (rights [u], k); If (r = k) Return key [u]; else return r ;}} int rank (int u, int K) // rank, also called rank, find the K-bit element in the ascending order of the entire tree; {If (u = 0) return 1; if (Key [u]> = K) return rank (lefts [u], k); else return size [lefts [u] + rank (rights [u], k) + 1;} int main () {freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt", "r", stdin); int N; scanf ("% d ", & N); For (INT I = 0; I <n; I ++) {int cmd, X; scanf ("% d", & cmd, & X); Switch (CMD) {Case 1: insert (u, x); break; Case 2: delete (u, x); break; Case 3: printf ("% d \ n", search (u, x); break; Case 4: printf ("% d \ n", rank (u, x )); break; Case 5: printf ("% d \ n", select (u, x); break; Case 6: printf ("% d \ n", predecessor (u, x); break; Case 7: printf ("% d \ n", successor (u, x); break ;}} return 0 ;}