Splay Summary-tree planting and splay Summary

Source: Internet
Author: User

Splay Summary-tree planting and splay Summary

I want to take the advanced data structure as my father.

Splay is a binary search tree with stable deformation.

Binary Search Tree: each node has no more than two children. The weights of vertices in the left subtree are smaller than those in the left subtree. the weights of vertices in the right subtree are larger than those in the left subtree ). This property applies to all vertices.

We can see that the binary search tree is suitable for solving problems such as seeking the successor, ranking, and ranking. However, if the sang operator (xin) wants to generate a decreasing or increasing data for the bing card (kuang, therefore, every insert and query of your regular Binary Search Tree is O (n) and will be eliminated by the times. We only need to improve our posture.

Splay is also a binary search tree. However, it has the following advantages: it can change the shape of the tree by rotating nodes, and lower the diameter of the tree, thus reducing the complexity of the river. Theoretically, the depth of this tree is logN, so the complexity of a query is logN.

So how to rotate nodes is a magic problem!

We need to rotate node x to the position of its father y to find the rule:

If x is the left son, then y will become the right son of x, and the original right son of x will become the left son of y.

If x is the right son, the situation is similar.

This is very easy to write. A few if statements are similar (so the constant is very large ). The key is the rotation technique.

Splay (x, goal) indicates that Jiang x is rotated to the son of goal.

If x is the grandson of goal, it would be nice to turn it over once.

If not, we can move x to the position of its grandfather (because it requires low complexity and tree diameter, don't ask me why, ask the tower tip ).

You can also optimize the rotation sequence to achieve the effect of low diameter.

Question. The BZOJ shelf operations are still comprehensive.

But in fact, it is better for beginners to do codevs Revenue statistics first, just like me, with fewer operations but a lot of splay.

Changing the location is equivalent to deleting and then inserting. Ranking is to lay it to the top and then look at the size of the Left subtree.

The k-th is the k-th largest, which is a common operation of the Binary Search Tree.

The bookshelf code is as follows.Num should be renamed to fact, indicating the number of the book corresponding to the vertex.

# Include <iostream> # include <cstdio> # include <cstdlib> # include <algorithm> # include <vector> # include <cstring> # include <queue> # define LL long int # define ls (x <1) # define rs (x <1 | 1) using namespace std; const int N = 300010; int n, m, T [N], ch [N] [2], size [N], num [N]; int fa [N], root, tot, pos [N], rec [N]; int gi () {int x = 0, res = 1; char ch = getchar (); while (ch> '9' | ch <'0') {if (ch = '-') res * =-1; ch = getchar ();} While (ch <= '9' & ch> = '0') x = x * 10 + ch-48, ch = getchar (); return x * res ;} LL gl () {LL x = 0, res = 1; char ch = getchar (); while (ch> '9' | ch <'0 ') {if (ch = '-') res * =-1; ch = getchar ();} while (ch <= '9' & ch> = '0 ') x = x * 10 + ch-48, ch = getchar (); return x * res;} int gsize (int x) {return x? Size [x]: x;} void up (int x) {size [x] = gsize (ch [x] [0]) + gsize (ch [x] [1]) + 1;} void rtt (int x, int kind) // The integrated version contains zig and zag. {Int y = fa [x], z = fa [y]; ch [y] [! Kind] = ch [x] [kind]; fa [x] = z; if (ch [x] [kind]) fa [ch [x] [kind] = y; if (z) if (ch [z] [0] = y) ch [z] [0] = x; else ch [z] [1] = x; ch [x] [kind] = y; fa [y] = x; up (y); up (x );} void splay (int x, int goal) {if (x = goal) return; while (fa [x]! = Goal) if (fa [fa [x] = goal) rtt (x, ch [fa [x] [0] = x ); else {int y = fa [x], kind = ch [fa [y] [0] = y; if (ch [y] [kind] = x) rtt (x, kind ^ 1), rtt (x, kind); else rtt (y, kind), rtt (x, kind ); // This can reduce the complexity. Actually, it is.} If (goal = 0) root = x;} void newnode (int & r, int fat, int x) {r = ++ tot; fa [tot] = fat; size [tot] = 1; ch [tot] [0] = ch [tot] [1] = 0; num [tot] = x; return ;} void insert (int rt, int x, int value) // place a book numbered x on the position value {if (value = 1 & ch [rt] [0] = 0) {newnode (ch [rt] [0], rt, x); return;} if (value = 2 + gsize (ch [rt] [0]) & ch [rt] [1] = 0) {newnode (ch [rt] [1], rt, x); return ;} if (value <= 1 + gsize (ch [rt] [0]) insert (ch [rt] [0], x, value); else insert (c H [rt] [1], x, value-gsize (ch [rt] [0])-1); up (x);} void del (int x) {splay (x, 0); if (ch [x] [0] = 0 & ch [x] [1] = 0) {root = 0; return;} if (ch [x] [0] = 0) {root = ch [x] [1]; fa [root] = 0; return ;} if (ch [x] [1] = 0) {root = ch [x] [0]; fa [root] = 0; return ;} int tmp = ch [x] [0]; while (ch [tmp] [1]) tmp = ch [tmp] [1]; splay (tmp, x ); root = tmp; fa [tmp] = 0; ch [tmp] [1] = ch [x] [1]; fa [ch [x] [1] = tmp; up (tmp);} int getsize (int x) {splay (x, 0); return gsize (ch [x] [0]) + 1 ;}int gc () {char ch = getchar (); while (ch> 'Z' | ch <'A') ch = getchar (); if (ch = 'T') return 1; if (ch = 'B') return 2; if (ch = 'I') return 3; if (ch = 'A') return 4; if (ch = 'q') return 5; return 0;} void pr (int x) {printf ("point % d: ls = % d rs = % d size = % d \ n", x, ch [x] [0], ch [x] [1], gsize (x); if (ch [x] [0]) pr (ch [x] [0]); if (ch [x] [1]) pr (ch [x] [1]);} int find (int x) {int pt = x, r = root; while (1) {if (pt = 1 + gsize (ch [r] [0]) retu Rn num [r]; if (pt> 1 + gsize (ch [r] [0]) pt-= (1 + gsize (ch [r] [0]), r = ch [r] [1]; else r = ch [r] [0];} return num [0];} int main () {n = gi (); m = gi (); for (int I = 1; I <= n; ++ I) {rec [I] = gi (); pos [rec [I] = I; if (I = 1) newnode (root, 0, rec [1]), root = tot; else {insert (root, rec [I], I); if (I % 2) splay (tot, 0) ;}} for (int I = 1; I <= m; ++ I) {int type = gc (), x = gi (), y = type = 3? Gi (): 0; switch (type) {case 1: {del (pos [x]); insert (root, x, 1); pos [x] = tot; splay (tot, 0); break;} case 2: {del (pos [x]); insert (root, x, n); pos [x] = tot; splay (tot, 0); break;} case 3: {int S = getsize (pos [x]) + y; del (pos [x]); insert (root, x, S); splay (tot, 0); pos [x] = tot; break;} case 4: {printf ("% d \ n ", getsize (pos [x])-1); break;} case 5: {printf ("% d \ n", find (x); break;} default: continue ;}} 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.