Topic: Give a sequence, give a sort of order, simulate this sort of sort, and output the location of each node found.
Thought: It makes you do what you do, nothing but a reverse, very simple. Pay attention to the case of equal weights when sorting.
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100010 #define INF 0x3f3f3f3fusing namespace std; struct splaytree{splaytree *son[2],*father; BOOL Reverse; int size; Splaytree (); BOOL Check () {return father->son[1] = = this; } void Combine (Splaytree *a,bool dir) {a->father = this; Son[dir] = A; } void Reverse () {Reverse ^= 1; Swap (son[0],son[1]); } void Pushup () {size = son[0]->size + son[1]->size + 1; } void Pushdown () {if (reverse) {son[0]->reverse (); Son[1]->reverse (); Reverse = false; }}}none,*nil = &none,*root; Splaytree:: Splaytree () {son[0] = son[1] = nil; Reverse = false; size = 1;} struct complex{int val,pos; Splaytree *a; BOOL operator < (const Complex &a) Const {if (val = = a.val) return pos < A.pos; Return Val < A.val; } void Read (int p) {scanf ("%d", &val); pos = p; }}src[max]; int cnt; void Pretreatment () {nil->size = 0; Nil->son[0] = nil->son[1] = Nil->father = nil;} Splaytree *buildtree (int l,int r) {if (L > R) return nil; int mid = (L + r) >> 1; Splaytree *re = new Splaytree (); SRC[MID].A = re; Re->combine (Buildtree (l,mid-1), false); Re->combine (Buildtree (mid + 1,r), true); Re->pushup (); return re; } inline void Rotate (Splaytree *a,bool dir) {Splaytree *f = a->father; F->pushdown (); A->pushdown (); F->son[!dir] = a->son[dir]; F->son[!dir]->father = f; A->son[dir] = f; A->father = f->father; F->father->son[f->check ()] = A; F->father = A; F->pushup (); if (f = = root) root = A;} inline void splay (Splaytree *a,splaytree *aim) {while (a->father! = Aim) {if (A->father->father = = aim) Rotate (a,!a->check ()); else if (!a->father->check ()) {if (!a->check ()) Rotate (a->father,true), Rotate (a,true) ; else Rotate (A,false), Rotate (a,true); } else {if (A->check ()) Rotate (A->father,false), Rotate (A,false); else Rotate (a,true), Rotate (A,false); }} a->pushup ();} Splaytree *find (Splaytree *a,int k) {A->pushdown (); if (a->son[0]->size >= k) return Find (A->SON[0],K); K-= a->son[0]->size; if (k = = 1) return A; Return Find (a->son[1],k-1);} inline void splayseg (int x,int y) {x++,y++; Splay (Find (root,x-1), nil); Splay (Find (root,y + 1), root);} int main () {CIN >> cnt; Pretreatment (); for (int i = 1; I <= cnt; ++i) Src[i]. Read (i); Src[0].val = src[cnt + 1].val = INF; Root = Buildtree (0,cnt + 1); Root->father = nil; Sort (src + 1,src + cnt + 1); for (int i = 1; I <= cnt; ++i) {splay (Src[i].a,nil); printf ("%d%c", root->son[0]->size, "\ n" [i = cnt]); Splayseg (i,root->son[0]->size); Root->son[1]->son[0]->reverse (); } return 0;}
Bzoj 3506 Cqoi 2014 Sort picker splay