Sgu 187. Twist and whirl-want to cheat

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.