Bzoj 3223:tyvj 1729 Literary Balance tree (splay)

Source: Internet
Author: User

The speed is actually in the top ten ... Eighth...

Splay, Interval rollover, with a lazy tag like a line segment tree indicating whether to flip

----------------------------------------------------------------------------------------

#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>#define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x)) using namespace std;const int MAXN = 100000 + 5;int n;struct node *null, *pt;struct Node {node* ch[2];int V, s;bool Flip;node (int _v = 0): V (_v), S (1), flip (0) {ch[0] = ch[1] = null;    }Inline int cmp (int k) const {K-= ch[0]->s;if (k = = 1) return-1;return k <= 0? 0:1;    }inline Void Maintain () {s = ch[0]->s + ch[1]->s + 1;    }inline void Pushdown () {if (flip) {flip = 0;swap (ch[0], ch[1]);ch[0]->flip ^= 1;ch[1]->flip ^= 1;        }    }void *operator New (size_t) {return pt++;}};node* Root;node N[MAXN], Null; void Rotate (node* &o, int d) {node* k = o->ch[d^1];o->ch[d^1] = k->ch[d];k->ch[d] = o;O->maintain (); K->maintain ();o = k;}void Splay (node* &o, int k) {O->pushdown ();int d = o->cmp (k);if (d = = 1) K-= O->ch[0]->s + 1;if (d! =-1) {node* p = o->ch[d];P->pushdown ();int d2 = p->cmp (k);int k2 = d2? K-p->ch[0]->s-1: K;if (d2! =-1) {splay (P->ch[d2], K2);d = = D2 rotate (o, d^1): Rotate (o->ch[d], d);        }rotate (o, d^1);    }}node* Build (int l, int r) {if (l >= R) return null;int m = (L + r) >> 1;node* o = new node (m);if (L < m) O->ch[0] = Build (L, m);if (m + 1 < r) O->ch[1] = build (M + 1, R);O->maintain ();return o;}void init () {pt = N; null = &Null; Null->s = 0; root = Build (0, N + 2);}void Dfs (node* o) {if (o = = null) return;O->pushdown ();DFS (o->ch[0]);if (o->v >= 1 && o->v <= N) printf ("%d", o->v);DFS (o->ch[1]);}inline int Read () {int ans = 0, f = 1;char C = getchar ();While (!isdigit (c)) {if (c = = '-') f =-1;C = GetChar ();}While (IsDigit (c)) {ans = ans * + C-' 0 ';C = GetChar ();}return f * ans;}int main () {n = read ();int m = read ();init ();While (m--) {int L = Read (), r = Read ();if (L = = r) Continue;splay (Root, L);splay (root->ch[1], R + 1-root->ch[0]->s);root->ch[1]->ch[0]->flip ^= 1;    }Dfs (root);return 0;}

----------------------------------------------------------------------------------------

3223:TYVJ 1729 Literary Balance tree time limit: ten Sec Memory Limit: MB
Submit: 1675 Solved: 931
[Submit] [Status] [Discuss] Description

you need to write a data structure (which can refer to the title of the topic) to maintain an ordered sequence, which requires the following actions: Flip an interval, such as the original ordered sequence is 5 4 3 2 1, the flip interval is [2,4], the result is 5 2 3 4 1

Input

The first behavior n,m n means the initial sequence has n number, this sequence is (1,2......n-1,n) m for the number of rollover operations
Next m line two numbers per line [L,r] Data guarantee 1<=l<=r<=n

Output

Outputs a row of n numbers, indicating the result of the original sequence after M-transform

Sample Input5 3

1 3

1 3

1 4

Sample Output4 3 2) 1 5

HINT



n,m<=100000

Source

Balance Tree

Bzoj 3223:tyvj 1729 Literary Balance tree (splay)

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.