Corresponding POJ topic: Click to open link
SuperMemo
| Time Limit: 5000MS |
|
Memory Limit: 65536K |
| Total Submissions: 11309 |
|
Accepted: 3545 |
| Case Time Limit: 2000MS |
Description
Your Friend, Jackson is invited to a TV show called SuperMemo in which the participant was told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {a1,a2, ... An}. Then the host performs a series of operations and queries on the sequence which consists:
- add x y d: Add D to all number in Sub-sequence {Ax ... Ay}. For example, performing "ADD 2 4 1" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5, 5}
- REVERSE x y: REVERSE the sub-sequence {Ax ... Ay}. For example, performing "REVERSE 2 4" on {1, 2, 3, 4, 5} results in {1, 4, 3, 2, 5}
- REVOLVE x y T: Rotate sub-sequence {Ax ... Ay} T times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
- Insert x P: Insert P after Ax. For example, performing "INSERT 2 4" on {1, 2, 3, 4, 5} results in {1, 2, 4, 3, 4, 5}
- Delete x: Delete Ax. For example, performing 'DELETE 2 ' on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
- MIN x y: Query the Participant what's the minimum number in sub-sequence {Ax ... Ay}. For example, the correct answer to "MIN 2 4" on {1, 2, 3, 4, 5} is 2
To make the show more interesting, the participant are granted a chance to turn to someone else so means when Jackson fee LS difficult in answering a query he is a call to help. You task was to watch the TV show and write a program giving the correct answer to each query in order to assist Jackson WH Enever he calls.
Input
The first line contains n (n ≤100000).
The following n lines describe the sequence.
Then follows m (m ≤100000), the numbers of operations and queries.
The following M lines describe the operations and queries.
Output
For each "MIN" query, output the correct answer.
Sample Input
Wuyi 2 3 4 52ADD 2 4 1MIN 4 5
Sample Output
5
Test instructions
There are 6 operations on the N number:
1) Added value: Add x y D: All values of interval [x, y] increase D
2) Flip: REVERSE x y: flips the interval [x, y]
3) Rotation: REVOLVE x y t: Rotates the T-time on the interval [x, y] clockwise (T > 0) or counterclockwise (T < 0)
4) Insert: Insert x P: Inserts p after a[x]
5) Delete: delete x: Remove a[x]
6) Maximum: Min x y: The minimum value within the interval [x, y]
Ideas:
Splay Tree integrated operation; The areas needing attention are:
1, Push_down (), push_up () of the wording, where should be called
2, the rotation operation of T can be negative
3, the rotation is actually the interval of the latter section to the front or the previous section to take down to the back, it is not difficult to understand
#include <cstdio> #include <cstdlib> #include <string> #include <algorithm> #include < string.h> #include <cmath> #include <iostream> #define MIN (x, y) ((x) < (y)? ( x):(y)) const int MAXN = 100100;using namespace std;typedef int type;typedef struct Tree{type val, add, Min_v;bool flag; TREE *fa, *l, *r; int sz; }tree;inline void Swap (int &a, int &b) {int t = A;a = B;b = t;} in the sum of the tree with this node root Class Splaytree{public:splaytree () {RT = Null;inf = 1000000000;} void Push_down (Tree *t) {if (NULL = = T) return;if (t->add) {if (t->l) {t->l->val + = t->add; T->l->add + = t->add; T->l->min_v + = T->add;} if (t->r) {t->r->val + = t->add; T->r->add + = t->add; T->r->min_v + = T->add;} T->add = 0;} if (t->flag) {tmp = t->l; T->l = t->r; T->r = Tmp;if (t->l) t->l->flag ^= 1;if (t->r) T->r->flag ^= 1; T->flag = 0;}} void Push_up (Tree *t) {T->sz = (t->l? t->l->sz:0) + (t->r?t->r->sz:0) + 1;if (t->l && t->r) t->min_v = min (t->val, min (T->l->min_v, T->r->mi N_v)), else if (t->l) t->min_v = min (T->l->min_v, t->val), else if (t->r) t->min_v = min (t->r-> Min_v, t->val); else T->min_v = t->val; Remember! }void NewNode (tree *pre, tree *&t, Type v) {T = (tree *) malloc (sizeof (tree)); T->val = T->min_v = v; T->add = 0; T->flag = 0; T->sz = 1; T->FA = pre; T->l = T->r = NULL;} void Maketree (tree *pre, tree *&t, int x, int y) {if (x > Y) return;int mid = ((x + y) >>1); NewNode (Pre, T, C[mid]); Maketree (T, T->l, X, mid-1); Maketree (T, T->r, mid + 1, y); PUSH_UP (T);} void Init (int n) {int i;for (i = 1; I <= n; i++) scanf ("%d", C + i); NewNode (NULL, RT,-inf); NewNode (RT, Rt->r, INF); rt->sz = 2; Maketree (Rt->r, rt->r->l, 1, N); PUSH_UP (RT->R); PUSH_UP (RT);} void R_rotate (tree *x) {Tree *y = x->fa; Tree *z = y->fa; Tree *k = x->r;y->l = K;x->r = Y;if (z{if (y = = z->l) Z->l = X;else z->r = x;} if (k) K->fa = Y;y->fa = X;x->fa = Z; Push_up (y);} void L_rotate (tree *x) {Tree *y = x->fa; Tree *z = y->fa; Tree *k = X->l;y->r = K;x->l = Y;if (z) {if (y = = z->r) Z->r = X;else z->l = x;} if (k) K->fa = Y;y->fa = X;x->fa = Z; Push_up (y);} Search for node x number of nodes tree *findtag (int x) {x++;if (NULL = = RT) return null; Tree *p;p = RT; Push_down (P); Type sum = (p->l? p->l->sz:0) + 1;while (sum! = x) {if (sum < x) {p = p->r;x-= sum;} else P = p->l;if (NULL = = p) break; Push_down (p); sum = (p->l? p->l->sz:0) + 1;} return p;} void Splay (tree *x, tree *&t) {tree *p, *end;end = T->fa;while (X->fa! = end) {p = x->fa;if (end = = P->FA) {// P is the root node if (x = = p->l) r_rotate (x); else l_rotate (x); break;} P is not the root node if (x = = P->l) {if (p = = p->fa->l) {r_rotate (P);//llr_rotate (x);//ll}else{r_rotate (x);//rll_rotate (x) ;}} Else{if (p = = p->fa->r) {//rrl_rotate (P); L_rotate (X);} else{//lrl_rotate (X); R_rotate(X);}}} T = X; PUSH_UP (T);} void Get_interval (int x, int y)//Turn the number of x to the root, and the number of Y to the right son of the root {tmp = Findtag (x); Splay (TMP, RT); tmp = Findtag (y); Splay (TMP, RT->R);} void Add (int x, int y, int d) {if (x > Y) Swap (x, y); Get_interval (x-1, y + 1); Rt->r->l->add + = D;rt->r->l->val + D;rt->r->l->min_v + = D; PUSH_UP (RT->R); PUSH_UP (RT);} void Reverse (int x, int y) {if (x > Y) Swap (x, y); Get_interval (x-1, y + 1); Rt->r->l->flag ^= 1;} void Revolve (int x, int y, int t) {if (x > Y) Swap (x, y), T = t% (y-x + 1),//modulo if (T < 0) T + = (y-x + 1); if (0 = = T ) return; Get_interval (y-t, y + 1); Tree *sub = Rt->r->l;rt->r->l = NULL; PUSH_UP (RT->R); PUSH_UP (RT); Get_interval (x-1, x); rt->r->l = SUB;SUB->FA = rt->r; PUSH_UP (RT->R); PUSH_UP (RT);} void Insert (int pos, int v) {get_interval (pos, pos + 1); NewNode (Rt->r, Rt->r->l, V); PUSH_UP (RT->R); PUSH_UP (RT);} void Delete (int pos) {get_interval (pos-1, POS + 1); free (rt->r->l); rt->r-> l = NULL; PUSH_UP (RT->R); PUSH_UP (RT);} void Min (int x, int y) {if (x > Y) Swap (x, y); Get_interval (x-1, y + 1); Push_down (rt->r->l);p rintf ("%d\n", Rt->r->l->min_v);} void free () {freetree (RT);} void Freetree (Tree *t) {if (NULL = = T) return; Freetree (T->L); Freetree (T->r); free (T);} Private:type C[MAXN], INF; Tree *rt, *tmp;}; Splaytree Spl;int Main () {//freopen ("In.txt", "R", stdin); int n, m, x, Y, Z;char ord[10];while (scanf ("%d", &n) = = 1) {SPL . Init (n); scanf ("%d", &m), while (m--) {scanf ("%s", Ord), if (!strcmp ("ADD", Ord)) {scanf ("%d%d%d", &x, &y, &Z); Spl. ADD (x, y, z);} if (!strcmp ("REVERSE", Ord)) {scanf ("%d%d", &x, &y), SPL. Reverse (x, y);} if (!strcmp ("REVOLVE", Ord)) {scanf ("%d%d%d", &x, &y, &z), SPL. Revolve (x, y, z);} if (!strcmp ("INSERT", Ord)) {scanf ("%d%d", &x, &y), SPL. Insert (x, y);} if (!strcmp ("DELETE", Ord)) {scanf ("%d", &x), SPL. Delete (x);} if (!strcmp ("MIN", Ord)) {scanf ("%d%d", &x, &y), SPL. Min (x, y);}} Spl. Free ();} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Splay Tree (Multi-action)--poj 3580 SuperMemo