Bzoj 1861 basic operations

Source: Internet
Author: User

Http://www.lydsy.com/JudgeOnline/problem.php? Id = 1861

After the three key functions are written, the simulation questions of water and water are used to rotate them. So easy

Note the following points:

1: Fetch a node in the splay and insert it back. Remember to clear the Left and Right sons of the node.

2: Do not forget to make up after each change.

No ..

# Include <cstdio> # include <cstring> # include <queue> # include <stack> # include <vector> # include <set> # include <algorithm> using namespace STD; typedef long LLD; # define L x-> C [0] # define r x-> C [1] # define KT root-> C [1]-> C [0] const int maxn = 111111; struct node {struct node * C [2], * Fa; int ID; int SZ; int val; inline bool D () {return fa-> C [0] = This;} inline void SETC (int d, node * s) {C [d] = s; s-> Fa = This;} inline void up () {SZ = C [0]-> SZ + C [1]-> SZ + 1;} inline void clear (node * null) {c [0] = C [1] = NULL; SZ = 1 ;}} node [2 * maxn], * null = & node [0]; int top; node * ID [maxn]; struct splaytree {node * root; void rotate (node * X, int f) {node * Y = x-> fa; y-> SETC (! F, x-> C [f]); X-> fa = Y-> fa; If (Y-> Fa! = NULL) Y-> fa-> SETC (! Y-> D (), x); X-> SETC (F, Y); y-> up ();} void splay (node * X, node * goal) {While (X-> Fa! = Goal) {If (X-> fa = goal) rotate (x, x-> D ()); else {int F = x-> fa-> D (); X-> D () = f? Rotate (X-> FA, f): Rotate (x ,! F); rotate (x, f) ;}} X-> up (); If (Goal = NULL) root = x;} void RTO (int K, node * goal) {If (k <1 | K> root-> sz) while (1); node * x = root; while (L-> SZ + 1! = K) {If (k <L-> SZ + 1) x = L; else {k-= L-> SZ + 1; X = r ;}} splay (x, goal);} void build (node * & X, int L, int R, node * Fa) {If (L> r) return; int M = L + r> 1; X = new_node (FA, num [m]); Build (X-> C [0], M-1, X ); build (X-> C [1], M + 1, R, x); X-> up ();} node * new_node (node * Fa, int V) {node * x = & node [++ top]; X-> id = top; X-> C [0] = x-> C [1] = NULL; x-> SZ = 1; X-> val = V; X-> fa = fa; If (V! =-1) ID [v] = x; return X;} void Init (int n) {for (INT I = 1; I <= N; I ++) scanf ("% d", & num [I]); root = new_node (null,-1); Build (root-> C [1], 1, n, root); root-> up ();} void del_root () {// Delete the root node * t = root; If (t-> C [1]! = NULL) {root = T-> C [1]; RTO (1, null); root-> C [0] = T-> C [0]; if (root-> C [0]! = NULL) Root-> C [0]-> fa = root;} else root = root-> C [0]; root-> fa = NULL; If (root! = NULL) Root-> up ();} void insert (int p, int v) {splay (ID [p], null ); int NC = root-> C [0]-> SZ-1; del_root (); Id [p]-> clear (null); NC ++ = V; RTO (nC + 1, null); If (root-> C [1] = NULL) {root-> SETC (1, Id [p]); root-> up (); return;} RTO (nC + 2, root); root-> C [1]-> SETC (0, Id [p]); root-> C [1]-> up (); root-> up ();} void bottom (INT p) {splay (ID [p], null ); del_root (); Id [p]-> clear (null); RTO (root-> SZ, null); root-> SETC (1, Id [p]); root-> up ();} void top (INT p) {splay (ID [p], null); del_root (); id [p]-> clear (null); RTO (1, null); If (root-> C [1] = NULL) {root-> SETC (1, id [p]); return;} RTO (2, root); root-> C [1]-> SETC (0, Id [p]); root-> C [1]-> up (); root-> up ();} int query (INT p) {RTO (p + 1, null ); return root-> val;} int ask (INT p) {splay (ID [p], null); Return root-> C [0]-> SZ-1 ;} void vist (node * X) {If (X! = NULL) {printf ("node: % 2D: Left son: % 2D right son: % 2D SZ: % 2D VAL: % d \ n", X-> ID, x-> C [0]-> ID, X-> C [1]-> ID, X-> SZ, X-> Val ); vist (X-> C [0]); vist (X-> C [1]);} void debug () {puts ("**************"); vist (Root ); puts ("\ n ******************");} int num [maxn];} SPT; void prepare () {Top = 0; null-> id = 0; null-> C [0] = NULL-> C [1] = NULL-> fa = NULL; null-> SZ = 0; null-> val = 0;} int main () {char op [10]; prepare (); int n, m, p; scanf ("% d", & N, & M); SPT. init (n); While (M --) {scanf ("% s", OP); If (OP [0] = 'q ') {scanf ("% d", & P); printf ("% d \ n", SPT. query (p);} else if (OP [0] = 'A') {scanf ("% d", & P ); printf ("% d \ n", SPT. ask (p);} else if (OP [0] = 'B') {scanf ("% d", & P); SPT. bottom (p);} else if (OP [0] = 'T') {scanf ("% d", & P); SPT. top (p) ;}else {int V; scanf ("% d", & P, & V); SPT. insert (p, v) ;}} 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.