很裸的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. 几条sql语句

    1.行.列转换 --行转列 ),科目 ),分数 int) ) ) ) ) ) ) ) ) ) --方法1 select 姓名, end) as 语文, end) as 数学, end) as 物理 f ...

  2. CodeForces 569A 第八次比赛 C题

    Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't hav ...

  3. .Net码农学Android---小点整理

    小点整理 虽然两大语言的编程思想相同,语法也相似,但具体到使用时,还是有些别扭,可能还是不太熟悉,现就自己遇到的一些微小问题整理如下: String/Int转换 C#:String--->Int ...

  4. spring 方法注入

    package com.haut.grain.junit.test; public  class Command {private Object state;public void setState( ...

  5. Android 实现子View的状态跟随父容器的状态

    最近自学着做东西,需要做一个效果,就是我ListView的Item点击下或者选中的时候,我Item里面的各个组件的状态要和我Item的状态保持一直,这样我就可以用XML,去根据组件的不同状态去实现不同 ...

  6. 解压vmlinuz和解压initrd(initramfs)

    有时就算只得到一个Linux kernel的rpm包或者直接是编译后的vmlinuz和initrd的binary文件,也需要了解其中的一些细节,可能需要去查找这些binary有没有将我想要的patch ...

  7. rm -rf删除过多文件提示参数过长

    cd /var/tmp/ find . -name "*.log"|xargs rm -rf "*.log"

  8. iPhone的震动 基于SDK8.0 Swift实现

    导入AudioToolbox.framework包 在swift文件中  import AudioToolbox AudioServicesPlaySystemSound(SystemSoundID. ...

  9. verilog 学习笔记

    1.在寄存器中: -1=1111 -2=1110 -3=1101 2.{1,0}=64‘H00000001_00000000;//默认是32位的位数-拼接: 3.defparam P1.Depth=1 ...

  10. 二叉查找树(BST)

    二叉查找树(BST):使用中序遍历可以得到一个有序的序列