很裸的Splay,抄一下CLJ的模板当作复习,debug了一个下午,收获是终于搞懂了以前看这个模板里不懂的内容。以前用这个模板的时候没有看懂为什么get函数返回的前缀要加个引用,经过一下午的debug终于明白,如果加了引用的时候是会被修改到的,删除实际上就是将root->ch[1]->ch[0]置为null,但由于我们还要把这一段插回去,所以get的时候的t前面没有加引用,否则一旦置为null的话 t也会变为null。还有就是这次是第一次用这个模板进行插入,本题倒腾了很久就是在这个插入上,没有找到合适的姿势。经过摸索一个正确的姿势是在  1 2 3 4 5 ... n 的第k个位置后插入,应该是先get(k+1,k+1),然后直接在root->ch[1]里设置左儿子即可。复习Splay是为了学习LCT做准备吧。。- -0

#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
using namespace std; #define ll long long
#define maxn 350000 struct Node
{
Node *ch[2], *p;
int size,val;
int rev;
Node(){ size = 0; val = 0; rev = 0; }
bool d(){
return this == p->ch[1];
}
void setc(Node *c, int d){
ch[d] = c; c->p = this;
}
void relax();
void revIt()
{
rev ^= 1;
}
void upd(){
size = ch[0]->size + ch[1]->size + 1;
}
}Tnull,*null=&Tnull; Node mem[maxn], *C = mem; void Node::relax()
{
if (rev){
swap(ch[0], ch[1]);
for (int i = 0; i < 2; i++){
if (ch[i] != null) ch[i]->revIt();
}
rev = 0;
}
} Node *make(int v)
{
C->ch[0] = C->ch[1] = null;
C->size = 1; C->val = v; C->rev = 0;
return C++;
} Node* build(int l, int r)
{
if (l >= r) return null;
int m = (l + r) >> 1;
Node *t = make(m);
t->setc(build(l, m), 0);
t->setc(build(m + 1, r), 1);
t->upd();
return t;
} Node *root; void rot(Node *t)
{
Node *p = t->p;
p->relax();
t->relax();
int d = t->d();
p->p->setc(t, p->d());
p->setc(t->ch[!d], d);
t->setc(p, !d);
p->upd();
if (p == root) root = t;
} void splay(Node *t, Node *f = null)
{
while (t->p != f){
if (t->p->p == f) rot(t);
else t->d() == t->p->d() ? (rot(t->p), rot(t)) : (rot(t), rot(t));
}
t->upd();
} Node* select(int k) {
for (Node*t = root;;) {
t->relax();
int c = t->ch[0]->size;
if (k == c)
return t;
if (k > c)
k -= c + 1, t = t->ch[1];
else
t = t->ch[0];
}
} Node*&get(int l, int r) { //[l,r)
Node*L = select(l - 1);
Node*R = select(r);
splay(L);
splay(R, L);
return R->ch[0];
} int n, m; int main()
{
while (cin >> n >> m)
{
if (n <0 && m <0) break;
memset(mem, 0, sizeof(mem));
C = mem;
root = build(0, n + 2); root->p = null;
char oper[10];
int li, ri, wi;
for (int i = 0; i < m; i++){
scanf("%s", oper);
if (oper[0] == 'C'){
scanf("%d%d%d", &li, &ri, &wi);
Node *t = get(li, ri + 1);
root->ch[1]->ch[0] = null;
splay(root->ch[1]);
Node *&x = get(wi+1, wi+1);
root->ch[1]->setc(t, 0);
}
else{
scanf("%d%d", &li, &ri);
Node *&t = get(li, ri + 1);
t->revIt();
splay(t);
}
}
for (int i = 1; i <= n; i++){
if (i >= 2) printf(" ");
Node *&t = get(i, i + 1);
printf("%d", t->val);
}
puts("");
}
return 0;
}

HDU3487 Play With Chains(Splay)的更多相关文章

  1. HDU--3487 Play with Chain (Splay伸展树)

    Play with Chain Problem Description YaoYao is fond of playing his chains. He has a chain containing ...

  2. HDU3487 Play With Chain [Splay]

    题目传送门 题目描述 Problem Description YaoYao is fond of playing his chains. He has a chain containing n dia ...

  3. HDU3487 Play with Chain splay 区间反转

    HDU3487 splay最核心的功能是将平衡树中的节点旋转到他的某个祖先的位置,并且维持平衡树的性质不变. 两个操作(数组实现) cut l,r, c把[l,r]剪下来放到剩下序列中第c个后面的位置 ...

  4. HDU-3487 Play with Chain Splay tee区间反转,移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 对于一个数列有两种操作:1.CUT a b c,先取出a-b区间的数,然后把它们放在取出后的第c ...

  5. hdu3487 splay树

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

  6. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

  7. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作

    HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...

  8. HDU 3487 Play with Chain | Splay

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

  9. HDU 3487 Play with Chain (splay tree)

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

随机推荐

  1. printf输出格式总结

    printf函数称为格式输出函数,其关键字最末一个字母f即为"格式"(format)之意.其功能是按用户指定的格式,把指定的数据显示到显示器屏幕上. printf函数调用的一般形式 ...

  2. Linux内核学习笔记——内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  3. 在Java如何保证方法是线程安全的

    废话开篇 都说Java程序好些,但是我觉得Java编程这东西,没个十年八年的真不敢说自己精通Java编程,由于工作原因,开始转战Java多线程,以前没怎么接触过,所以想留点脚印在这两条路上. 切入正题 ...

  4. 关于VS2010“ADO.NET Entity Data Model模板丢失或者添加失败问题

    我最近在安装vs2010后,添加ADO.NET Entity 实体时发现,我的新建项里面并没有这个实体模型,后来我就在博问里面发表了问题,请求大家帮忙解决,悲剧的是少有人回应啊,呵呵,不过我还是在网上 ...

  5. 47.MIF和COE文件格式

    .mif和.coe这两个文件分别是Quartus和ISE的RAM和ROM的初始化文件,因此了解他们的格式,是很必要的   MIF文件的格式如下:   WIDTH=14; --数据宽度为14位 DEPT ...

  6. 动态链接库知识点总结之三(如何以显示的方式加载DLL)

    总结一下如何显示加载方式加载DLL, 首先,我们新建一个win32项目,选择dll,空项目,再添加一个源文件,一个模块定义文件(.def),具体如下图.(详细方法已经在前两篇文章中讲述,如有不懂,打开 ...

  7. 关于在 loadView 中改变状态栏的可视性

    这种问题不知道大家是否遇见过,在此用两句话(时间紧迫,还得加班)分享下今天犯的错误 我把状态栏的的可视性的改变写在了loadView 里面,然后就出现了调用了两次 loadView 和 viewDid ...

  8. 关于 Google Chrome 中的全屏模式和 APP 模式

    前言:我一直在纠结这篇文章是否应该归类在「前段开发」的范围内,哈哈! 前段时间做了一个项目,涉及到一个要全屏模式去访问网页的需求,因为 Google Chrome 的效率不错,而且专门为 Chrome ...

  9. 搭建SpringMVC+MyBatis开发框架四

    在src/main下面新建一个resouces文件夹,我们继续配置一些资源 1.新增applicationContext.xml:  <?xml version="1.0" ...

  10. jQuery Dialog弹出层对话框插件

    Dialog.js的相关注释已经添加,可以按照注释,进行相关样式的修改,适用于自定义的各个系统! dialog.js /** * jQuery的Dialog插件. * * @param object ...