这种高级数据结构太难搞了.........现在还是先照着别人的代码敲,做模板..........慢慢花时间来弄懂

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <climits>//形如INT_MAX一类的
#define MAX 301111
#define INF 0x7FFFFFFF
#define REP(i,s,t) for(int i=(s);i<=(t);++i)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define LL(x) x<<1
#define RR(x) x<<1|1
# define eps 1e-5
//#pragma comment(linker, "/STACK:36777216") ///传说中的外挂
using namespace std; int n,q,root,tot,cnt;
int size[MAX],ch[MAX][2],key[MAX],pre[MAX],num[MAX],lazy[MAX],node[MAX]; void newnode(int &x, int va,int fa) {
x = ++ tot;
ch[x][0] = ch[x][1] = 0;
pre[x] = fa;
lazy[x] = 0;
key[x] = va;
} void up(int x) {
size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
} void down(int x) {
if(lazy[x]) {
swap(ch[x][0],ch[x][1]);
lazy[ch[x][0]] ^= 1;
lazy[ch[x][1]] ^= 1;
lazy[x] = 0;
}
} void build(int &x,int L,int R,int fa) {
if(L > R) return ;
int mid = (L + R) >> 1;
newnode(x,mid,fa);
build(ch[x][0],L,mid-1,x);
build(ch[x][1],mid+1,R,x);
up(x);
} void init() {
root = tot = 0;
ch[0][0] = ch[0][1] = pre[0] = lazy[0] = size[0] = 0;
newnode(root,-1,0);
newnode(ch[root][1],-1,root);
size[root] = 2;
build(ch[ch[root][1]][0],1,n,ch[root][1]);
up(ch[root][1]);
up(root);
} void rotate(int x,int kind) { // 0 :左旋 1:右旋
int y = pre[x];
down(y);
down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
up(y);
} void splay(int x,int g) {
down(x);
while(pre[x] != g) {
if(pre[pre[x]] == g) rotate(x,ch[pre[x]][0] == x);
else {
int y = pre[x];
int kind = (ch[pre[y]][0] == y);
if(ch[y][kind] == x) {
rotate(x,!kind) ;
rotate(x,kind);
} else {
rotate(y,kind);
rotate(x,kind);
}
}
}
up(x);
if(g == 0) root = x;
} int get_kth(int x,int k) {
down(x);
int s = size[ch[x][0]];
if(s == k-1) return x;
if(s >= k) return get_kth(ch[x][0],k);
else return get_kth(ch[x][1],k-s-1);
} int get_min(int x) {
down(x);
while(ch[x][0]) {
x = ch[x][0];
down(x);
}
return x;
} int get_max(int x) {
down(x);
while(ch[x][1]) {
x = ch[x][1];
down(x);
}
return x;
} int get_pre(int x) {
int now = ch[x][0];
while(ch[now][1]) {
now = ch[now][1];
}
return now;
} int get_suc(int x) {
int now = ch[x][1];
while(ch[now][0]) {
now = ch[now][0];
}
return now;
} void rev(int l,int r) {
int x = get_kth(root,l);
int y = get_kth(root,r+2);
splay(x,0);
splay(y,root);
lazy[ch[ch[root][1]][0]] ^= 1;
} void cut(int l,int r,int c) {
int x = get_kth(root,l);
int y = get_kth(root,r+2);
splay(x,0);
splay(y,root);
int tmp = ch[ch[root][1]][0];
ch[ch[root][1]][0] = 0;
up(ch[root][1]);
up(root);
int z = get_kth(root,c+1);
splay(z,0);
int m = get_min(ch[root][1]);
splay(m,root);
ch[ch[root][1]][0] = tmp;
pre[ch[ch[root][1]][0]] = ch[root][1];
up(ch[root][1]);
up(root);
} void print(int x) {
if(x == 0) return ;
down(x);
print(ch[x][0]);
if(cnt >= 1 && cnt <= n) {
if(cnt > 1) printf(" ");
printf("%d",key[x]);
}
cnt ++;
print(ch[x][1]);
} char str[11];
int a,b,c;
int main() {
while(scanf("%d%d",&n,&q) != EOF) {
if(n == -1 && q == -1) break;
init();
while(q--) {
scanf("%s",str);
if(str[0] == 'C') {
scanf("%d%d%d",&a,&b,&c);
cut(a,b,c);
}
if(str[0] == 'F') {
scanf("%d%d",&a,&b);
rev(a,b);
}
}
cnt = 0;
print(root);
puts("");
}
return 0;
}

HDU 3478 Play with Chain (Splay树)的更多相关文章

  1. HDU 3487 Play with Chain | Splay

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  3. hdu 3436 splay树+离散化*

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. hdu 1890 splay树

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. hdu3487 splay树

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. Splay树学习

    首先给出一论文讲的很好: http://www.docin.com/p-63165342.html http://www.docin.com/p-62465596.html 然后给出模板胡浩大神的模板 ...

  7. HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

  8. Splay树-Codevs 1296 营业额统计

    Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...

  9. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

随机推荐

  1. Linux命令: chown

    touch auth.log root@ubuntu:/work# ls -l auth.log -rw-r--r-- 1 root root 0 Feb 18 19:27 auth.log chow ...

  2. Arduino 入门程序示例之一个 LED(2015-06-11)

    前言 答应了群主写一些示例程序,一直拖延拖延拖延唉.主要还是害怕在各大高手面前班门弄斧……(这也算是给拖延症找一个美好的理由吧),这几天终于下决心要写出来了,各位高手拍砖敬请轻拍啊. 示例程序 首先是 ...

  3. QList 和std::list的比较

    QList QList<T> 是一个Qt通用容器类.它存储一序列的值,并且提供基于索引的数据访问方法和快速的插入和删除操作. QList<T>, QLinkedList< ...

  4. 进入MAC下面的Library目录

    从LION后,苹果将library目录隐藏起来了,要进入那个目录,需要用到一定的技巧. 打开Finder,按下shift+command+g,输入“~/Library”(输入引号里面的),再按回车就到 ...

  5. [C++Boost]程序参数项解析库Program_options使用指南

    介绍 程序参数项(program options)是一系列name=value对,program_options 允许程序开发者获得通过命令行(command line)和配置文件(config fi ...

  6. 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码

    整个程序的布局很简单 只在一个垂直方向上的线性布局里面有俩个按钮(Button)和一个显示图片的控件(ImageView)这里就不给出这部分的代码了   1.是打开系统的相册   Intent alb ...

  7. Java基础08 继承

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 继承(inheritance)是面向对象的重要概念.继承是除组合(composit ...

  8. ActiveMQ持久化方式(转)

    消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息 中心重新启动后仍然可以将消息发送出去,如果把这种持久化 ...

  9. Android异步载入全解析之使用多线程

    异步载入之使用多线程 初次尝试 异步.异步,事实上说白了就是多任务处理.也就是多线程执行.多线程那就会有各种问题,我们一步步来看.首先.我们创建一个class--ImageLoaderWithoutC ...

  10. 自定义navigationBar的高度

    原来看过一些解决办法,都不太好,最近解决自定义 tab bar的高度的问题,从中受到启发,找到下面的解决办法. 个人觉得和网上找到的其它方法比还是很简洁的. 关键是要调整navBarTransitio ...