Poj 3580 supermemo (splay tree)

Source: Internet
Author: User

Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove

It is also a very BT data structure question.

There are many operations. One of the Special Operations is the right shift operation of revolve. Shift the range [a, B] to the right of the C-bit

First, there may be a lot of C. You can first modulo the Interval Length.

After the right shift, we can find that [a, B] is divided into two intervals [a, B-c] [B-c + 1, B], insert the latter before the former.

It can be converted into insert and delete operations. The memory is tight and the manual memory pool is used. Dynamic splay may have more time and is not used to writing.

# Include <iostream> # include <cstring> # include <queue> # include <cstdio> # include <algorithm> # define n 200005 # define INF 1 <29 # define mod 100000007 # define ll long # define key_value ch [CH [root] [1] [0] # DEFINE _ match (, b) (a) = (B) using namespace STD; int N, Q, A [n]; int size [N], pre [N], rev [N], Val [N], same [N], add [N], M [N]; int ch [N] [2], tot1, root, s [N], tot2; // debug part copy from HH void treaval (int x) {If (x) {treav Al (CH [x] [0]); printf ("Node % 2D: Left son % 2D right son % 2D parent node % 2D size = % 2D, val = % 2D min = % 2D \ n ", X, CH [x] [0], CH [x] [1], pre [X], size [X], val [X], M [x]); treaval (CH [x] [1]);} void debug () {printf ("% d \ n ", root); treaval (Root);} // debug void update_rev (int r) {If (! R) return; swap (CH [r] [0], CH [r] [1]); rev [R] ^ = 1;} void update_add (int r, int add) {If (! R) return; m [R] + = add; Val [R] + = add; add [R] + = add;} void newnode (Int & R, int K, int father) {If (tot2) r = s [tot2 --]; else r = ++ tot1; ch [r] [0] = CH [r] [1] = 0; Pre [R] = Father; Val [R] = m [R] = K; size [R] = 1; add [R] = 0;} void push_up (int x) {int L = CH [x] [0], R = CH [x] [1]; Size [x] = size [l] + size [R] + 1; M [x] = min (M [L], M [R]), Val [x]);} void push_down (int x) {int L = CH [x] [0], r = CH [x] [1]; If (add [x]) {update_add (L, add [x]); update_add (R, add [x]); Add [x] = 0;} If (Rev [x]) {update_rev (l); update_rev (r); rev [x] = 0 ;}} void bulid (Int & R, int L, int R, int father) {If (L> r) return; int mid = (L + r)/2; newnode (r, A [Mid], Father); bulid (CH [r] [0], L, mid-1, R); bulid (CH [r] [1], mid + 1, R, R); push_up (r);} void Init () {tot1 = tot2 = root = 0; ch [root] [0] = CH [root] [1] = pre [root] = rev [root] = size [root] = add [root] = 0; M [root] = inf; newnode (root, INF, 0); newnode (CH [root] [1], INF, root); push _ Up (Root); bulid (key_value, 1, n, CH [root] [1]); push_up (CH [root] [1]); push_up (Root );} void rotate (int x, int kind) {int y = pre [X]; push_down (y); push_down (x); ch [y] [! Kind] = CH [x] [kind]; Pre [CH [x] [kind] = y; If (pre [y]) ch [pre [y] [CH [pre [y] [1] = y] = x; Pre [x] = pre [y]; ch [x] [kind] = y; Pre [y] = x; push_up (y);} void splay (int r, int goal) {push_down (R ); while (pre [R]! = Goal) {If (pre [pre [R] = goal) rotate (R, CH [pre [R] [0] = R ); else {int y = pre [R]; int kind = (CH [pre [y] [0] = y ); if (CH [y] [kind] = r) {rotate (R ,! Kind); rotate (R, kind);} else {rotate (Y, kind); rotate (R, kind) ;}} push_up (R ); if (Goal = 0) root = r;} void rotateto (int K, int goal) {int r = root; push_down (R ); while (size [CH [r] [0]! = K) {If (k <size [CH [r] [0]) {r = CH [r] [0];} else {k-= (size [CH [r] [0] + 1); r = CH [r] [1];} push_down (r );} splay (R, goal);} int get_kth (int r, int K) {push_down (r); int T = size [CH [r] [0] + 1; if (t = k) return r; If (T> K) return get_kth (CH [r] [0], k ); else return get_kth (CH [r] [1], K-T);} int get_min (int r) {push_down (r); While (CH [r] [0]) {r = CH [r] [0]; push_down (r);} return r;} int get_max (int r) {push_down (r); While (Ch [r] [1]) {r = CH [r] [1]; push_down (r);} return r;} void reversal (int l, int R) {int x = get_kth (root, L); splay (x, 0); int y = get_kth (root, R + 2); splay (Y, root ); update_rev (key_value);} void cut (int A, int B, int c) {int x = get_kth (root, a); int y = get_kth (root, B + 2); splay (x, 0); splay (Y, root); int TMP = key_value; key_value = 0; push_up (CH [root] [1]); push_up (Root); int z = get_kth (root, C + 1); splay (z, 0); int M = get_min (CH [R OOT] [1]); splay (M, root); key_value = TMP; Pre [key_value] = CH [root] [1]; push_up (CH [root] [1]); push_up (Root);} int CNT; void inorder (int r) {If (r = 0) return; push_down (r); inorder (CH [r] [0]); If (CNT> = 1) printf ("% d", Val [R]); CNT ++; inorder (CH [r] [1]);} void insert (INT POs, int K) {int x = get_kth (root, POS); splay (x, 0 ); int y = get_min (CH [root] [1]); splay (Y, root); newnode (key_value, K, CH [root] [1]); push_up (CH [root] [1]); push_up (Root);} void erase (int r) {If (! R) return; s [++ tot2] = r; erase (CH [r] [0]); erase (CH [r] [1]);} void Delete (int l) {int x = get_kth (root, L); splay (x, 0); int y = get_kth (root, L + 2); splay (Y, root); erase (key_value); Pre [key_value] = 0; key_value = 0; push_up (CH [root] [1]); push_up (Root );} void add (int l, int R, int K) {int x = get_kth (root, L); splay (x, 0); int y = get_kth (root, R + 2); splay (Y, root); update_add (key_value, k); push_up (CH [root] [1]); push_up (Root);} int Get_answer (int l, int R) {int x = get_kth (root, L); splay (x, 0); int y = get_kth (root, R + 2 ); splay (Y, root); Return M [key_value];} void revolve (int A, int B, int t) {If (! T) return; int c = B-t; int x = get_kth (root, a); splay (x, 0); X = get_kth (root, C + 2 ); splay (x, root); // extract the latter int TMP = key_value; key_value = 0; push_up (CH [root] [1]); push_up (Root ); splay (get_kth (root, B-c + a), 0); splay (get_kth (root, B-c + A + 1), root ); // rotate to the corresponding position key_value = TMP; Pre [key_value] = CH [root] [1]; // insert push_up (CH [root] [1]); push_up (Root);} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); scanf ("% d", & Q); Init (); char STR [10]; int L, R, K; while (Q --) {scanf ("% s", STR); If (! Strcmp (STR, "add") {scanf ("% d", & L, & R, & K); add (L, R, k);} else if (! Strcmp (STR, "reverse") {scanf ("% d", & L, & R); reversal (L, R);} else if (! Strcmp (STR, "revolve") {scanf ("% d", & L, & R, & K); revolve (L, R, (K % (R-l + 1) + (R-l + 1) % (R-l + 1);} else if (! Strcmp (STR, "insert") {scanf ("% d", & L, & K); insert (L + 1, k);} else if (! Strcmp (STR, "delete") {scanf ("% d", & L); Delete (l) ;}else {scanf ("% d ", & L, & R); printf ("% d \ n", get_answer (L, R) ;}} 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.