Splay
But I used a reverse to get the water through...
Code for calling STL: (156 ms)
# Include <iostream> # include <cstdio> # include <algorithm> using namespace STD; int f [130009]; int N, M, L, R; int main () {scanf ("% d", & N, & M); For (INT I = 1; I <= N; I ++) f [I] = I; for (INT I = 1; I <= m; I ++) {scanf ("% d", & L, & R); reverse (F + L, F + R + 1) ;}for (INT I = 1; I <= N; I ++) printf ("% d", F [I]);}
Splay (46 ms) (the Code comes from the Network)
# Include <cstdio> # include <algorithm> const int maxn = 130000 + 9; struct splay_tree {int L [maxn], R [maxn], F [maxn], n, root; int size [maxn]; bool re [maxn]; void push (int u) {If (Re [u]) {re [u] = 0; if (L [u]) Re [L [u] ^ = 1; if (R [u]) Re [R [u] ^ = 1; STD :: swap (L [u], R [u]) ;}} void Update (int u) {size [u] = size [L [u] + size [R [u] + 1;} void rotate (int x) {int y = f [x]; push (y); push (x); F [x] = f [y]; If (F [y]) {If (L [f [y] = y) L [F [y] = x; else R [f [y] = x;} f [y] = x; If (x = R [y]) {If (L [x]) f [L [x] = y; R [y] = L [X]; L [x] = y ;} else {If (R [x]) f [R [x] = y; L [y] = R [X]; R [x] = y ;} update (y); Update (x);} void splay (int u, int aim) {int t; while (t = f [u])! = Aim) if (F [T] = aim) rotate (U); else if (L [f [T] = T) = (L [T] = u) rotate (t), rotate (U); else rotate (u), rotate (U); If (! Aim) root = u;} void build (INT num) {size [0] = 0; Re [0] = 0; n = num; root = 1; for (INT I = 1; I <= N; ++ I) {L [I] = 0; R [I] = I + 1; F [I] = I-1; size [I] = n-I + 1; Re [I] = 0;} R [N] = 0; splay (n, 0);} int find (INT rank) {If (! Rank | rank> N) return 0; For (INT u = root;) {push (U); If (size [L [u] + 1 = rank) {splay (u, 0); Return U;} else if (rank <= size [L [u]) u = L [u]; else {rank-= size [L [u] + 1; u = R [u] ;}} void reserve (int A, int B) {int pre = find (A-1), SUF = find (B + 1); If (pre) {splay (PRE, 0); If (SUF) {splay (SUF, PRE); Re [L [SUF] ^ = 1;} else re [R [pre] ^ = 1;} else if (SUF) {splay (SUF, 0); Re [L [SUF] ^ = 1;} else re [root] ^ = 1;} void print (int u) {for (; U; U = R [u]) {push (U); print (L [u]); printf ("% d", u) ;}} tree; int main () {# ifndef online_judge freopen ("in.txt", "r", stdin); freopen ("out.txt", "W", stdout); # endif int n, m, I, j, k; scanf ("% d", & N, & M); tree. build (n); for (I = 1; I <= m; ++ I) {scanf ("% d", & J, & K); tree. reserve (j, k);} tree. print (tree. root); Return 0 ;}
Sgu 187. Twist and whirl-want to cheat