This is my comprehensive treap operation of poj1442 3481 2352 to get the treap full version template. (Tested AC)
From nocow-treap, a copy of the code that imitates the exquisite processing.
Struct tree
{
Int key; // key value
Int size; // The total number of nodes in the subtree
Int PRI; // Its Random Value
Int son [2]; // learned from nocow code. 0 indicates the left son, 1 indicates the right son, and only one function is needed for rotation.
(INT num;) // the original position of the node in the sequence. You can add
}
The treap template supports the following operations:
1. insert value
2. Delete Value
3. Find the number of the node whose number is smaller than P (find (T [RT]. Size, RT) at the maximum );)
4. Find the number of nodes whose values are less than or equal to the key.
(You can modify other similar operations by yourself)
# Include <cstdio> # include <cstring> # include <ctime> # include <iostream> # include <algorithm> # include <cstdlib> # include <cmath> # include <utility> # include <vector> # include <queue> # include <map> # include <set> # define max (X, y) (x)> (y )? (X) :( y) # define min (x, y) (x)> (y )? (Y) :( X) # define INF 0x3f3f3f3f # define maxn 100005 using namespace STD; int CNT = 1, rT = 0; // node number starting from 1 struct tree {int key, size, PRI, son [2]; // ensure that the father's pri is greater than the son's pri void set (int x, int y, int Z) {key = x; pri = y; size = z; son [0] = Son [1] = 0 ;}} T [maxn]; void rotate (int p, Int & X) {int y = T [X]. son [! P]; t [X]. size = T [X]. size-T [Y]. size + T [T [Y]. son [p]. size; t [X]. son [! P] = T [Y]. son [p]; t [Y]. size = T [Y]. size-T [T [Y]. son [p]. size + T [X]. size; t [Y]. son [p] = x; X = y;} void ins (INT key, Int & X) {If (x = 0) T [x = CNT ++]. set (key, Rand (), 1); else {T [X]. size ++; int P = Key <t [X]. key; ins (Key, t [X]. son [! P]); If (T [X]. PRI <t [T [X]. Son [! P]. PRI) rotate (p, x) ;}} void del (INT key, Int & X) // Delete the node with the value of key {If (T [X]. key = Key) {If (T [X]. son [0] & T [X]. son [1]) {int P = T [T [X]. son [0]. PRI> T [T [X]. son [1]. PRI; rotate (p, x); del (Key, t [X]. son [p]);} else {If (! T [X]. son [0]) x = T [X]. son [1]; else x = T [X]. son [0] ;}} else {T [X]. size --; int P = T [X]. key> key; del (Key, t [X]. son [! P]) ;}} int find (int p, Int & X) // find the number of the node with the smallest P value {If (P = T [T [X]. son [0]. size + 1) return X; If (P> T [T [X]. son [0]. size + 1) Find (p-t [T [X]. son [0]. size-1, t [X]. son [1]); else find (P, T [X]. son [0]);} int find_nolarger (INT key, Int & X) // find the number of nodes whose values are less than or equal to the key {If (x = 0) return 0; if (T [X]. key <= Key) return T [T [X]. son [0]. size + 1 + find_nolarger (Key, t [X]. son [1]); else return find_nolarger (Key, t [X]. son [0]);}
Treap full Template