Http://poj.org/problem? Id = 3580
Note: The inf setting should not be too small, and the processing of the input data of the question (there may be illegal data). Pay attention to the judgment. For details, see the code.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int INF = ~ 0u> 2; # define l ch [x] [0] # define r ch [x] [1] # define kt (CH [CH [RT] [1] [0]) const int maxn = 200010; struct splaytree {int SZ [maxn]; int ch [maxn] [2]; int pre [maxn]; int RT, top; inline int min (int A, int B) {return a <B? A: B;} inline void down (INT X) {If (! X) return; If (FLIP [x]) {flip [l] ^ = 1; flip [R] ^ = 1; swap (L, R ); flip [x] = 0;} If (add [x]) {If (l) {mi [l] + = add [x]; add [l] + = add [X]; Val [l] + = add [X];} If (r) {mi [R] + = add [x]; add [R] + = add [X]; Val [R] + = add [X];} Add [x] = 0 ;}} inline void up (int x) {mi [x] = min (Val [X], min (MI [L], MI [R]); SZ [x] = 1 + SZ [l] + SZ [R];} inline void rotate (int x, int f) {int y = pre [x]; down (y); down (x); ch [y] [! F] = CH [x] [f]; Pre [CH [x] [f] = y; Pre [x] = pre [y]; if (pre [x]) CH [pre [y] [CH [pre [y] [1] = y] = X; ch [x] [f] = y; Pre [y] = x; up (y);} inline void splay (int x, int goal) {// rotate X to the bottom of the goal down (x); // prevent pre [x] from being the target point. The following loop cannot be entered, the information of X cannot be passed down while (pre [x]! = Goal) {down (pre [pre [x]); down (pre [x]); down (x); // you need to upload the marker before rotation, because the node location may change if (pre [pre [x] = goal) rotate (x, CH [pre [x] [0] = X ); else {int y = pre [X], Z = pre [y]; int F = (CH [Z] [0] = y ); if (CH [y] [f] = x) rotate (x ,! F), rotate (x, f); else rotate (Y, F), rotate (x, f) ;}} up (x); If (Goal = 0) RT = x;} inline void RTO (int K, int goal) {// rotate the K-digit to int x = RT; down (x) under goal ); while (SZ [l] + 1! = K) {If (k <SZ [l] + 1) x = L; else {k-= (SZ [l] + 1); X = r ;} down (x);} splay (x, goal);} void vist (int x) {If (x) {printf ("Node % 2D: left son % 2D right son % 2D % 2D MI: % d sum: % 2D Add: % 2D \ n ", X, L, R, Val [X], mi [X], sum [X], add [x]); vist (l); vist (r) ;}} void debug () {vist (RT );} void newnode (Int & X, int C, int f) {x = ++ top; L = r = 0; SZ [x] = 1; Pre [x] = F; mi [x] = Val [x] = C; flip [x] = add [x] = 0;} inline void build (Int & X, int L, int R, int f) {If (L> r) return; int M = L + r> 1; newnode (x, num [m], f); Build (l, l, m-1, x); Build (R, m + 1, R, x); Pre [x] = f; up (x);} inline void Init (int n) {ch [0] [0] = CH [0] [1] = pre [0] = SZ [0] = 0; RT = Top = 0; flip [0] = Val [0] = add [0] = 0; MI [0] = inf; newnode (RT, INF, 0 ); newnode (CH [RT] [1], INF, RT); SZ [RT] = 2; for (INT I = 1; I <= N; I ++) scanf ("% d", & num [I]); Build (KT, 1, n, CH [RT] [1]); up (CH [RT] [1]); up (RT);} void min () {int X, Y; scanf ("% d", & X, & Y); If (x> Y) Swap (x, y); RTO (x, 0); RTO (Y + 2, RT ); printf ("% d \ n", MI [Kt]);} void add () {int X, Y, D; scanf ("% d ", & X, & Y, & D); If (x> Y) Swap (x, y); RTO (x, 0); RTO (Y + 2, RT ); add [Kt] + = D; Val [Kt] + = D; MI [Kt] + = D;} void reverse () {int X, Y; scanf ("% d", & X, & Y); If (x> Y) Swap (x, y); RTO (x, 0 ); RTO (Y + 2, RT); flip [Kt] ^ = 1;} void insert () {int X, P; scanf ("% d", & X, & P); RTO (x + 1, 0); RTO (x + 2, RT); newnode (KT, P, CH [RT] [1]); up (CH [RT] [1]); up (RT) ;}void Delete () {int X; scanf ("% d", & X); RTO (X, 0); RTO (x + 2, RT); KT = 0; up (CH [RT] [1]); up (RT);} void revolve () {int X, Y, T, L, R; scanf ("% d", & X, & Y, & T); If (x> Y) swap (x, y); T % = (Y-x + 1); t = (t + Y-x + 1) % (Y-x + 1 ); if (t = 0) return; L = Y-t + 1, R = y; RTO (L, 0); RTO (R + 2, RT ); int TMP = kt; KT = 0; up (CH [RT] [1]); up (RT); RTO (x, 0); RTO (x + 1, RT); KT = TMP; Pre [TMP] = CH [RT] [1]; up (CH [RT] [1]); up (RT );} int MI [maxn]; int num [maxn]; int add [maxn]; int sum [maxn]; int flag; int flip [maxn]; int Val [maxn];} SPT; int main () {int n, m; scanf ("% d", & N); SPT. init (n); char op [10]; scanf ("% d", & M); While (M --) {scanf ("% s", OP ); if (OP [0] = 'A') {SPT. add ();} else if (OP [0] = 'M') {SPT. min ();} else if (OP [0] = 'I') {SPT. insert ();} else if (OP [0] = 'D') {SPT. delete ();} else if (strcmp (OP, "reverse") = 0) {SPT. reverse ();} else if (strcmp (OP, "debug") = 0) {SPT. debug ();} else {SPT. revolve () ;}} return 0 ;}