SGU 187.Twist and whirl-want to Cheat (splay)

Source: Internet
Author: User
Tags integer numbers

maintains a sequence that supports the length n of the rollover number m: The final output sequence. 1<=n<=130000, 1<=m<=2000

Splay Nude questions ...

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

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 130009; struct Node {Node *p, *ch[2];int S, v;BOOL Rev;inline void setc (node* t, int c) {ch[c] = t;t->p = this;}inline int d () {return p->ch[1] = = this;}inline void Rev () {Rev ^= 1;}inline void Pushdown () {if (rev) {Ch[0]->rev ();Ch[1]->rev ();swap (ch[0], ch[1]);rev = false;}}inline void upd () {s = ch[0]->s + ch[1]->s + 1;}} MEMPOOL[MAXN], *pt, *root, *null;void Initsplay () {pt = mempool;Root = Null = pt++;null->s = 0;null->setc (Null, 0);null->setc (Null, 1);}node* newNode (int v) {pt->v = v;Pt->rev = false;pt->s = 1;pt->p = pt->ch[0] = pt->ch[1] = Null;return pt++;}void Rot (node* t) {node* p = t->p;P->pushdown ();T->pushdown ();int d = t->d ();p->p->setc (T, P->d ());p->setc (t->ch[d ^ 1], d);t->setc (p, D ^ 1);p->upd ();if (p = = root) root = t;}void splay (node* t, node* f = Null) {For (node* p = t->p; P! = f; p = t->p) {if (p->p! = f)p->d ()! = T->d ()? Rot (t): Rot (p);Rot (t);}t->upd ();} node* Build (int l, int r) {if (l >= R) return Null;int m = (L + r) >> 1;node* t = NewNode (m);t->setc (Build (L, M), 0);t->setc (Build (M + 1, R), 1);t->upd ();return t;}node* Select (int k) {For (node* t = Root;;) {T->pushdown ();int s = t->ch[0]->s;if (k = = s) return t;if (K < s)t = t->ch[0];ElseK-= s + 1, t = t->ch[1];}} node* Range (int l, int r) {splay (Select (--l));splay (Select (++r), Root);return root->ch[1]->ch[0];}void DFS (node* t) {if (t = = Null) return;T->pushdown ();DFS (t->ch[0]);printf ("%d", t->v);DFS (t->ch[1]);}int N, M;int main () {Initsplay ();scanf ("%d%d", &n, &m);Root = Build (0, N + 2);While (m--) {int L, R;scanf ("%d%d", &l, &r);node* t = Range (l, R);T->rev ();splay (t);}DFS (Range (1, N));return 0;}

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

187. Twist and whirl-want to Cheattime limit per test:0.25 sec.
Memory limit per test:4096 Kbinput:standard input
Output:standard output


A well-known sharper i*** invented a new to swindle people. There was N thimbles on the table, and there was a small ball with the number under each of the them. The balls is numbered with numbers from 1 to N from left to right. At one operation i*** changes the order of some subsequence of successive thimbles to the opposite. Your task is to find the order of numbers (from left to right) in sequence after all of his manipulations. The total number of manipulations is M.
InputThe first line contains the numbers N and M (1<=n<=130000, 1<=m<=2000) separated by a space. Each of the following M lines contains double integer numbers Pi, Qi (1<=pi<=qi<=n)-positions of the leftmost and Rightmost thimbles in rotated sequence.
OutputOutput the sequence of N numbers-the numbers of balls in the thimbles from left to right.
Sample Test (s)
Input
Test #1
5 2
1 3
4 5

Test #2
5 2
1 4
2 5
Output
Test #1
3 2 1) 5 4

Test #2
4 5 1 2 3[submit][forum]
Author: Michael R. Mirzayanov
Resource: ACM International Collegiate Programming Contest 2003-2004
North-eastern European Region, Southern subregion
Date: 2003 October, 9




SGU 187.Twist and whirl-want to Cheat (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.