[BZOJ1861] [Zjoi2006] Book Bookshelf

Source: Internet
Author: User

[BZOJ1861] [Zjoi2006] Book Bookshelf

Question Description

Little T has a big bookcase. The structure of this bookcase is somewhat unique, that is, the book in the bookcase is stacked from top to bottom into a column. She numbered every book with a positive integer of 1 to N. Small T in reading, every time take out a book, after reading and put back to the bookcase and then take a copy. Because these books are so attractive, she often forgets what position they were placed on the bookcase after reading it. But the memory of little T is very good, so at least every time when the book can be placed in the place where the book is taken out, for example, when she took the book has an x book, then put back when the book can only be X-1, X or x+1 book. Of course, there are special circumstances, such as when the phone rang when the book suddenly or have friends to visit. This time the careless little T will conveniently put the book in the bookcase in the top or bottom of all the books, and then turned away. Over time, the order of the books in Little T's bookcase will become more and more chaotic, and finding a specific numbered book becomes more and more difficult. So she wants you to help her write a book management program, handle her reading some of the operation, as well as answer her two questions: (1) The book is numbered x where the bookcase, (2) from the top to the next I book number is how much.

Input

The first line has two number n,m, respectively, the number of books and the number of orders, and the second behavior n positive integers: The number of number I represents the initial time from the top to the bottom of the book placed in the first position, the third line to the m+2 line, one command per line. There are 5 forms of the command: 1. Top s--says the study with number S is on the top. 2. Bottom s--says the study with the number S is at the bottom. 3. Insert s t--t∈{-1,0,1}, if there is an X book on the book numbered S, this command means that the book is put back and that it has a x+t book on it; 4. Ask s--asked how many books are currently on the top of the book numbered S. 5. Query s--asks for the number of book S from above.

Output

For each ask or query statement you should output a line, a number, representing the answer to the query.

Input example

Ten Ten1 3 2 7 5 8 Ten 4 9 6Query3Top5Ask6Bottom3Ask3Top6Insert4-1Query5Query2Ask2

Output example

2 9 9 7 5 3

Data size and conventions

100% of data, n,m < = 80000

Exercises

Let the number of each node match its number in the stretch tree, find the rank using the subtree size, and query the rank of a node to extend it to the root.

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype > #include <algorithm>using namespace Std;int read () {int x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if ( c = = '-') f =-1; c = GetChar (); }while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 80010struct Node {int siz; Node () {}} ns[maxn];int ToT, FA[MAXN], CH[2][MAXN], id[maxn];void maintain (int o) {ns[o].siz = 1;for (int i = 0; i < 2; i++) if (Ch[i][o]) Ns[o].siz + = Ns[ch[i][o]].siz;return;} void Build (int& o, int l, int r) {if (L > R) return; int mid = L + R >> 1; o = Id[mid];build (Ch[0][o], L, mid -1); Build (Ch[1][o], mid + 1, R), if (Ch[0][o]) fa[ch[0][o]] = o;if (Ch[1][o]) fa[ch[1][o]] = O;return maintain (o);}  void rotate (int u) {int y = Fa[u], z = fa[y], l = 0, r = 1;if (z) ch[ch[1][z]==y][z] = u;if (ch[1][y] = = u) Swap (L, R); Fa[u] = Z; Fa[y] = u; Fa[ch[r][u]] = y;ch[l][y] = Ch[r][u]; Ch[r][u] = Y;maintain (y); MaIntain (u); return;} void splay (int u) {while (Fa[u]) {int y = Fa[u], z = fa[y];if (z) {if (ch[0][y] = = u ^ ch[0][z] = = y) rotate (u); else rotate (y );} Rotate (u);} return;} int splitl (int u) {splay (u); int tmp = ch[0][u];fa[tmp] = Ch[0][u] = 0;maintain (u); return tmp;} int splitr (int u) {splay (u); int tmp = ch[1][u];fa[tmp] = Ch[1][u] = 0;maintain (u); return tmp;} int merge (int a, int b) {if (!a) return maintain (b), b;if (!B) return maintain (a), A;while (Ch[1][a]) A = Ch[1][a];splay (a); c H[1][a] = b; FA[B] = A;return Maintain (a), A;} int getrt () {int u = 1; while (fa[u]) u = Fa[u];return u;} int Find (int o, int k) {if (!o) return 0;int ls = ch[0][o]? ns[ch[0][o]].siz:0;if (k = = ls + 1) return o;if (k > ls + 1 Return find (Ch[1][o], k-ls-1); return find (Ch[0][o], k);} int main () {int n = read (), q = Read (), for (int i = 1; I <= n; i++) Id[i] = read (); int tmp = 0; Build (tmp, 1, n); int TQ = Q;while (q--) {char cmd[10]; scanf ("%s", cmd), if (cmd[0] = = ' T ') {int MRT = read (), LRT = SPLITL (MRT), RRT = Splitr (MRT); LRT = merge (LRT, RRT); Merge (MRT, LRT);} if (cmd[0] = = ' B ') {int MRT = read (), LRT = SPLITL (MRT), RRT = Splitr (MRT), LRT = Merge (LRT, RRT), merge (LRT, MRT);} if (cmd[0] = = ' I ') {int MRT = read (), t = Read (), RK, LRT, Rrt;if (!t) continue;splay (MRT); rk = (CH[0][MRT]? NS[CH[0][MRT] ].siz:0) + 1 + t;if (RK < 1 | | RK > N) continue; RK = Find (GETRT (), RK), if (T < 0) {LRT = SPLITL (RK); splitl (MRT); RRT = Splitr (MRT); LRT = merge (LRT, MRT); LRT = merge (l RT, RK); Merge (LRT, RRT);} else {LRT = SPLITL (MRT); splitl (RK); RRT = Splitr (RK); LRT = merge (LRT, RK); LRT = merge (LRT, MRT); Merge (LRT, RRT);}} if (cmd[0] = = ' A ') {int u = read (); splay (U);p rintf ("%d\n", Ch[0][u]? ns[ch[0][u]].siz:0);} if (cmd[0] = = ' Q ') printf ("%d\n", Find (GETRT (), read ()));} return 0;}

[BZOJ1861] [Zjoi2006] Book Bookshelf

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.