Bare splay interval operation: Memory Pool + interval addition and subtraction + interval flip + insert + Delete + maintenance maximum value
Supermemo
Time limit:5000 Ms |
|
Memory limit:65536 K |
Total submissions:8552 |
|
Accepted:2801 |
Case time limit:2000 ms |
Description Your friend, Jackson is invited to a TV show called supermemo in which the particle ant is 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 each number in sub-sequence {Ax...Ay}. For example, please Ming"Add2 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, please Ming"Reverse2 4 "on {1, 2, 3, 4, 5} results in {1, 4, 3, 2, 5}
- Revolve x y t: Rotate sub-SEQUENCE {Ax...Ay}TTimes. For example, please Ming"Revolve2 4 2 "on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
- Insert x P: InsertPAfterAx. For example, Ming"Insert2 4 "on {1, 2, 3, 4, 5} results in {1, 2, 4, 3, 4, 5}
- Delete x: DeleteAx. For example, Ming"Delete2 "on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
- Min x y: Query the participates ipant what is the minimum number in sub-sequence {Ax...Ay}. For example, the correct answer"Min2 4 "on {1, 2, 3, 4, 5} is 2
To make the show more interesting, the participant is granted a chance to turn to someone else that means when Jackson feels difficult in answering a query he may call you for help. you task is to watch the TV show and write a program giving the correct answer to each query in order to assist Jackson whenever he CILS. Input The first line containsN(N≤ 100000 ). The followingNLines describe the sequence. Then followsM(M≤ 100000), the numbers of operations and queries. The followingMLines describe the operations and queries. Output For each"Min"Query, output the correct answer. Sample Input 51 2 3 4 52ADD 2 4 1MIN 4 5 Sample output 5 Source Poj founder monthly contest-2008.04.13, Yao Jinyu |
[Submit] [Go Back] [Status] [discuss]
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 220000; const int INF = 0x3f3f3f; # define key_value ch [CH [root] [1] [0] int ch [maxn] [2], rev [maxn], add [maxn], SZ [maxn], pre [maxn], key [maxn], Minn [maxn]; int root, tot1; int s [maxn], tot2; int N, Q, a [maxn]; void newnode (Int & X, int father, int K) {If (tot2) x = s [tot2 --]; else x = ++ tot1; ch [x] [0] = CH [x] [1] = rev [x] = Add [x] = 0; SZ [x] = 1; Pre [x] = Father; key [x] = Minn [x] = K;} void erase (int r) {If (r) {s [++ tot2] = r; erase (CH [r] [0]); erase (CH [r] [1]);} void upd_rev (int x) {If (! X) return; swap (CH [x] [0], CH [x] [1]); rev [x] ^ = 1;} void upd_add (int x, int d) {If (! X) return; key [x] + = D; Minn [x] + = D; add [x] + = D;} void push_up (int x) {SZ [x] = SZ [CH [x] [1] + SZ [CH [x] [0] + 1; Minn [x] = Key [x]; if (CH [x] [0]) Minn [x] = min (Minn [X], Minn [CH [x] [0]); if (CH [x] [1]) Minn [x] = min (Minn [X], Minn [CH [x] [1]);} void push_down (int x) {If (Rev [x]) {upd_rev (CH [x] [0]); upd_rev (CH [x] [1]); rev [x] = 0;} If (add [x]) {upd_add (CH [x] [0], add [x]); upd_add (CH [x] [1], add [x]); add [x] = 0 ;}} void build (Int & X, in T l, int R, int FA) {If (L> r) return; int mid = (L + r)/2; newnode (x, fa, A [Mid]); Build (CH [x] [0], L, mid-1, x); Build (CH [x] [1], Mid + 1, r, x); push_up (x);} void Init () {root = tot1 = tot2 = 0; ch [root] [0] = CH [root] [1] = pre [root] = SZ [root] = 0; Minn [root] = Key [root] = inf; newnode (root, 0, INF); newnode (CH [root] [1], root, INF); Build (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]; pre [y] = x; ch [x] [kind] = y; push_up (y);} void splay (int r, int goal) {push_down (R ); while (pre [R]! = Goal) {If (pre [pre [R] = goal) {push_down (pre [R]); push_down (r); rotate (r, ch [pre [R] [0] = R);} else {push_down (pre [pre [R]); push_down (pre [R]); push_down (r); int y = pre [R]; int kind = (CH [pre [y] [0] = y ); if (CH [y] [kind] = r) rotate (R ,! Kind); else rotate (Y, kind); rotate (R, kind) ;}} push_up (r); If (Goal = 0) root = r ;} int get_kth (int r, int K) {push_down (r); int T = SZ [CH [r] [0] + 1; if (k = T) return R; If (T <k) return get_kth (CH [r] [1], K-T); else return get_kth (CH [r] [0], k);} 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 ;}///....... do .. it ........ void add (int l, int R, int d) {splay (get_kth (root, L), 0); splay (get_kth (root, R + 2), root ); upd_add (key_value, d); push_up (CH [root] [1]); push_up (Root);} void reverse (int l, int R) {splay (get_kth (root, l), 0); splay (get_kth (root, R + 2), root); upd_rev (key_value); push_up (CH [root] [1]); push_up (Root);} void Delete (INT p) {splay (get_kth (root, P), 0); splay (get_kth (root, P + 2), root); er ASE (key_value); Pre [key_value] = 0; key_value = 0; push_up (CH [root] [1]); push_up (Root);} void insert (int p, int v) {splay (get_kth (root, p + 1), 0); splay (get_kth (root, P + 2), root); newnode (key_value, ch [root] [1], V); push_up (CH [root] [1]); push_up (Root);} int min (int l, int R) {splay (get_kth (root, L), 0); splay (get_kth (root, R + 2), root); Return Minn [key_value];} void revolve (INT l, int R, int t) {int Len = r-L + 1; t = (T % Len + Len) % Len; If (t = 0) return; int c = r-t + 1; // [L .. c-1] and [C .. r] splay (get_kth (root, c), 0); splay (get_kth (root, R + 2), root); int temp = key_value; key_value = 0; push_up (CH [root] [1]); push_up (Root); splay (get_kth (root, L), 0); splay (get_kth (root, L + 1 ), root); key_value = temp; Pre [key_value] = CH [root] [1]; push_up (CH [root] [1]); push_up (Root );} /**************** debug *********************/ void showit (int x) {If (x ){ Push_down (x); showit (CH [x] [0]); printf ("node: % 2D key: % 2D left son: % 2D right son: % 2D parent node: % 2D SZ: % 2D min: % 2D \ n ", X, key [X], CH [x] [0], CH [x] [1], pre [X], SZ [X], Minn [x]); showit (CH [x] [1]) ;}} void debug () {cout <"------------------------- \ n"; cout <"root:" <root <Endl; showit (Root);} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) scanf ("% d", A + I); Init (); // debug (); scanf ("% d", & Q); char op [100]; int x, y, z; while (Q --) {scanf ("% s", OP); If (strcmp (OP, "add") = 0) {scanf ("% d", & X, & Y, & Z); add (x, y, z);} else if (strcmp (OP, "reverse") = 0) {scanf ("% d", & X, & Y); reverse (x, y);} else if (strcmp (OP, "revolve") = 0) {scanf ("% d", & X, & Y, & Z); revolve (x, y, z);} else if (strcmp (OP, "insert") = 0) {scanf ("% d", & X, & Y); insert (x, y);} else if (strcmp (OP, "delete") = 0) {scanf ("% d", & X); Delete (x);} else if (strcmp (OP, "min ") = 0) {scanf ("% d", & X, & Y); printf ("% d \ n", min (x, y ));} // debug () ;}} return 0 ;}