Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1890
Pay special attention to downloading the flip mark
Flip the number between the number I and the number I, and then delete the number I
/* Interval flip. Pay attention to marked downloading */# include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define l ch [x] [0] # define r ch [x] [1] # define kt (CH [CH [RT] [1] [0]) const int maxn = 100010; int CMP (pair <int, int> A, pair <int, int> B) {if (. first! = B. first) return. first <B. first; return. second <B. second;} struct splaytree {int SZ [maxn]; int ch [maxn] [2]; int pre [maxn]; int RT, top; inline void down (int x) {If (FLIP [x]) {flip [l] ^ = 1; flip [R] ^ = 1; swap (L, R ); flip [x] = 0 ;}} inline void up (int x) {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 flip: % d \ n ", X, L, R, Val [X], flip [x]); vist (L ); vist (r) ;}} void newnode (Int & X, int C, int f) {x = ++ top; flip [x] = 0; L = r = 0; pre [x] = f; SZ [x] = 1; Val [x] = C;} inline void build (Int & X, int L, int R, int F) {If (L> r) return; int M = L + r> 1; newnode (x, num [ID [m]. first, f); MP [ID [m] = x; B Uild (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] = 0; Val [0] = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d", & num [I]. first); num [I]. second = I;} Sort (Num + 1, num + n + 1, CMP); For (INT I = 1; I <= N; I ++) id [num [I]. second] = I; build (RT, 1, n, 0);} void del () {int T = RT; If (CH [RT] [1]) {RT = CH [RT] [1]; RTO (1, 0); ch [RT] [0] = CH [T] [0]; if (CH [RT] [0]) Pre [C H [RT] [0] = RT;} else RT = CH [RT] [0]; Pre [RT] = 0; up (RT );} void solve (int n) {for (INT I = 1; I <= N; I ++) {splay (MP [I], 0 ); printf ("% d", I + SZ [CH [RT] [0]); flip [CH [RT] [0] ^ = 1; del (); printf (I = n? "\ N": "") ;}} pair <int, int> num [maxn]; int ID [maxn]; int MP [maxn]; int flip [maxn]; int Val [maxn];} SPT; int main () {int N; while (scanf ("% d", & N), n) {SPT. init (n); SPT. solve (n);} return 0 ;}