第一道splay,仔细看了大白书上的内容后并不难,就是想记录一个大白书没有说清楚的地方。
就是为什么把第k个节点伸展到根节点要分3种情况?应该跟我一样想过,不就是一种情况?反正是树,那个节点到根节点就只有一条路,不停的往上翻转不就行了。的确,这样也可以达成目的,这样跟那3种情况的差别就在于第二种,3点共线的时候,他是先翻转x的父节点,再翻转x,而我们则是想翻转2次x。
精髓就在于这张图(出自学堂在线清华大学的数据结构课程)
地址:http://www.xuetangx.com/courses/TsinghuaX/30240184X/2014_T2/info中的伸展树双层伸展。
看了这张图应该就知道为什么要那么操作了。
这题用了一个真实的指针null,这样的确方便了很多。
AC代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 201000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
struct Node
{
Node* ch[2];
int s,num,flag;
int cmp(int x)
{
int p = ch[0] == NULL ? 0 : ch[0]->s;
if(x == p+1) return -1;
else if(x < p+1) return 0;
return 1;
}
void maintain()
{
s = 1 + ch[0]->s + ch[1]->s;
}
void pushdown()
{
if(flag)
{
flag = 0;
swap(ch[0],ch[1]);
ch[0]->flag ^= 1;
ch[1]->flag ^= 1;
}
}
};
Node *null = new Node();
void node_init(Node* &o,int _num)
{
o->ch[0] = o->ch[1] = null;
o->s = 1; o->flag = 0;
o->num = _num;
}
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);
if(d2 == 1) k -= p->ch[0]->s+1;
if(d2 != -1)
{
splay(p->ch[d2],k);
if(d == d2) rotate(o,d^1);
else rotate(o->ch[d],d);
}
rotate(o,d^1);
}
}
Node* merge(Node* left, Node* right)
{
splay(left,left->s);
left->ch[1] = right;
left->maintain();
return left;
}
void split(Node* o, int k, Node* &left, Node* &right)
{
splay(o,k);
left = o;
right = o->ch[1];
o->ch[1] = null;
left->maintain();
}
void removetree(Node* &o)
{
if(o->ch[0] != NULL) removetree(o->ch[0]);
if(o->ch[1] != NULL) removetree(o->ch[1]);
delete o;
o = NULL;
}
void build(Node* &o,int n,int ge)
{
int p = (n+1)/2;
o = new Node();
node_init(o,p+ge);
if(p > 1) build(o->ch[0],p-1,ge);
if(n-p >= 1) build(o->ch[1],n-p,ge+p);
o->maintain();
}
Node *root, *Left, *Right, *mid, *o;
void init()
{
if(root != NULL) removetree(root);
if(Left != NULL) removetree(Left);
if(Right != NULL) removetree(Right);
if(mid != NULL) removetree(mid);
if(o != NULL) removetree(o);
}
void dfs(Node* o)
{
o->pushdown();
if(o->ch[0] != null) dfs(o->ch[0]);
if(o->num != 0) printf("%d\n",o->num);
if(o->ch[1] != null) dfs(o->ch[1]);
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o3.txt","w",stdout);
#endif
int n,m,a,b;
null->s = 0;
while(~scanf("%d%d",&n,&m))
{
init();
build(root,n+1,-1);
while(m--)
{
scanf("%d%d",&a,&b);
split(root,a,Left,o);
split(o,b-a+1,mid,Right);
mid->flag ^= 1;
root = merge(merge(Left,Right),mid);
}
dfs(root);
}
return 0;
}