1, test instructions: All kinds of splay operation, a good template title 2333
2, Analysis: splay template problem, no explanation qaq
#include <stack> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm >using namespace std; #define M 2000010 inline int read () {char ch = getchar (); int x = 0, f = 1; while (Ch < ' 0 ' | | ch > ' 9 ') {if (ch = = '-') f =-1; ch = getchar (); } while (' 0 ' <= ch && ch <= ' 9 ') {x = x * + ch-' 0 '; ch = getchar (); } return x * f;} namespace splay{struct node{Node *ch[2], *FA; char c; BOOL Rev; int size; inline int which (); inline void Reverse () {Rev ^= 1; Swap (ch[0], ch[1]); } inline void Pd () {if (rev.) {Ch[0]-reverse (); CH[1], reverse (); Rev = false; }}: Inline void Maintain () {size = 1 + ch[0], size + ch[1], size; } Node (); }*null = new Node, ft[m]; int tot; Node::node () {size = 1; c = ' + '; Ch[0] = ch[1] = FA = null; Rev = false; } inline int Node::which () {if (FA = = null) return-1; return this = = FA, ch[1]; } inline void Rotate (node *o) {node *p = o-fa; int L = o-which (), r = l ^ 1; FA = P, fa; if (P-which ()! =-1) p, FA, Ch[p, which ()] = O; P--ch[l] = O-ch[r]; if (O-ch[r]) O-ch[r], fa = P; O-ch[r] = p; P--fa = O; O-ch[r], maintain (); O-Maintain (); } inline void splay (Node *o) {static stack<node*> St; if (!o) return; Node *p = o; while (1) {St.push (P); if (P-which () = =-1) break; p = p, fa; } while (!st.empty ()) {st.top (), PD (); St.pop (); } while (O-which ()! =-1) {p = o-fa; if (P-which ()! =-1) {if (P-, which () ^ o-which ()) rotate (o); else rotate (p); } rotate (o); }} inline node* Kth (Node *o, int k) {o-pd (); int t = o-ch[0], size + 1; if (k = = t) return o; if (K < T) return Kth (O-ch[0], k); Return Kth (O-ch[1], k-t); } Inline Node *merge (node *a, node *b) {if (a = = null) return b; if (b = = null) return A; Node *p = Kth (A, a, size); Splay (P); Node *c = p; C-ch[1] = b; b, fa = C; C-Maintain (); return C; } inline void Split (Node *o, int k, node* &a, node* &b) {if (k = = 0) {a = null; b = o; Return } if (k = = O, size) {a = O; b = null; Return } Node *p = Kth (o, k); Splay (P); o = p; O-Maintain (); b = P-ch[1]; FA = null; P--ch[1] = null; A = p; A-maintain (); }} using namespace splay; int main () {null-ch[0] = null, ch[1] = null, FA = NULL; Null-c = ' + '; Null-rev = false; null-size = 0; int n = read (); int now = 0; Node *root = null; Char str[10]; for (int i = 1; I <= n; i + +) {scanf ("%s", str); if (str[0] = = ' M ') {int x = read (); now = x; } else if (str[0] = = ' I ') {int x = read (); Node *a, *b, *d = null; Split (Root, now, A, b); for (int i = 1; i <= x; i + +) {Char ch = getchar (); Node *c = &ft[++ tot]; C-c = ch; D = Merge (d, c); } root = Merge (A, d); Root = Merge (root, b); } else if (str[0] = = ' D ') {int x = read (); Node *a, *b, *c; Split (Root, now, A, b); Split (b, X, B, c); Root = Merge (A, c); } else if (str[0] = = ' R ') {int x = read (); Node *a, *b, *c; Split (Root, now, A, b); Split (b, X, B, c); B--reverse (); Root = Merge (a, b); Root = Merge (root, c); } else if (str[0] = = ' G ') {Node *p = Kth (root, now + 1); printf ("%c\n", P-C); } else if (str[0] = = ' P ') {now--; } else{now + +; }} return 0;}
BZOJ1269--[AHOI2006] Text Editor editor