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

维护一个支持翻转次数M的长度N的序列..最后输出序列.1<=N<=130000, 1<=M<=2000

splay裸题...

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

#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];
else
k -= 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 cheat

time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



A well-known sharper I*** invented a new way to swindle people. There are N thimbles on the table, and there is a small ball with the number under each of them. The balls are 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.

Input
The first line contains two integer numbers N and M (1<=N<=130000, 1<=M<=2000) separated by a space. Each of the following M lines contains two integer numbers Pi, Qi (1<=Pi<=Qi<=N) - positions of the leftmost and rightmost thimbles in rotated sequence.

Output
Output 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

Author:Michael R. Mirzayanov
Resource:ACM International Collegiate Programming Contest 2003-2004 
North-Eastern European Region, Southern Subregion
Date:2003 October, 9




 

posted @ 2015-12-15 21:45  JSZX11556  阅读(390)  评论(0编辑  收藏  举报