链接

简单的两种操作,一种删除某段区间,加在第I个点的后面,另一个是翻转区间。都是splay的简单操作。

悲剧一:pushdown时候忘记让lz=0

悲剧二:删除区间,加在某点之后的时候忘记修改其父亲节点。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 300010
#define LL long long
#define INF 0xfffffff
#define key_value ch[ch[root][1]][0]
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
using namespace std;
struct splay_tree
{
int pre[N],size[N];
int ch[N][];
int root,tot,num,tot1;
int key[N],lz[N];
void dfs(int x)
{
if(x)
{
dfs(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d lz = %d\n",
x,ch[x][],ch[x][],pre[x],size[x],key[x],lz[x]);
dfs(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
dfs(root);
}
//以上用于debug*/
void newnode(int &x,int v,int fa)//新建一结点
{
x = ++tot;
ch[x][]=ch[x][] = ;
pre[x] = fa;
lz[x] = ;
size[x] = ;
key[x] = v;
}
void pushdown(int w)
{
if(lz[w])
{
int l = ch[w][],r = ch[w][];
swap(ch[w][],ch[w][]); lz[l]^=lz[w];
lz[r]^=lz[w];
lz[w] = ;
}
}
void pushup(int w)//由儿子更新其父亲
{
size[w] = size[ch[w][]]+size[ch[w][]]+;
//cout<<s[w][0]<<" "<<s[w][1]<<endl;
}
void rotate(int r,int kind)//旋转操作,根据kind进行左旋和右旋
{
int y = pre[r];
pushdown(y);
pushdown(r);
ch[y][!kind] = ch[r][kind];
pre[ch[r][kind]] = y;
if(pre[y])
{
ch[pre[y]][ch[pre[y]][]==y] = r;
}
pre[r] = pre[y];
ch[r][kind] = y;
pre[y] = r;
pushup(y);
pushup(r);
}
void splay(int r,int goal)//将r结点旋至goal下
{
pushdown(r);
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
rotate(r,ch[pre[r]][]==r);
}
else
{
int y = pre[r];
int kind = (ch[pre[y]][]==y);
if(ch[y][kind]==r)
{
rotate(r,!kind);
rotate(r,kind);
}
else
{
rotate(y,kind);
rotate(r,kind);
}
}
}
pushup(r);
if(goal==) root = r;
}
int get_k(int k)//得到第k个的结点
{
int r = root;
pushdown(r);
while(size[ch[r][]]+!=k)
{
if(size[ch[r][]]>=k)
r = ch[r][];
else
{
k-=(size[ch[r][]]+);//根据左右结点的数量来确定第k个节点在哪里
r = ch[r][];
}
pushdown(r);
}
pushup(r);
return r;
}
void cut(int l,int r,int k)
{
splay(get_k(l),);
splay(get_k(r+),root);
pushdown(ch[root][]);
int nod = key_value;
ch[ch[root][]][] = ;
pre[nod] = ;
pushup(ch[root][]);
pushup(root);
//debug();
splay(get_k(k),);
splay(get_k(k+),root);
ch[ch[root][]][] = nod;
pre[nod] = ch[root][];
pushup(ch[root][]);
pushup(root);
// debug();
}
void filp(int l,int r)
{
//cout<<get_k(l)<<" "<<get_k(r+2)<<endl;
splay(get_k(l),);
splay(get_k(r+),root); //cout<<","<<endl;debug();
lz[key_value]^=;
//swap(ch[key_value][0],ch[key_value][1]);
pushup(ch[root][]);
pushup(root);
}
void work(int n)
{
int i;
for(i = ;i < n ;i++)
{
printf("%d ",key[get_k(i+)]);
}
printf("%d\n",key[get_k(n+)]);
//debug();
}
void build(int &x,int l,int r,int fa)
{
int m = (l+r)>>;
if(l>r) return ;
newnode(x,m,fa);
build(ch[x][],l,m-,x);
build(ch[x][],m+,r,x);
pushup(x);
}
void init(int o)
{
size[] = ch[][] = ch[][] = key[] = lz[] = ;
root = tot = ;
newnode(root,,);
newnode(ch[root][],,root);
build(ch[ch[root][]][],,o,ch[root][]);
size[root] = ;
pushup(ch[root][]);
pushup(root);
}
}SP;
int main()
{
int n,q;
while(scanf("%d%d",&n,&q)!=EOF)
{
if(n==-&&q==-) break;
SP.init(n);
while(q--)
{
char sq[];
int k,x,y;
scanf("%s",sq);
if(sq[]=='C')
{
scanf("%d%d%d",&x,&y,&k);
SP.cut(x,y,k+);
// SP.debug();
// SP.work(n);
}
else
{
//SP.debug();
scanf("%d%d",&x,&y);
SP.filp(x,y);
//SP.debug();
}
//SP.work(n);
}
SP.work(n);
}
return ;
}

hdu3487Play with Chain(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(伸展树分裂合并)

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

  3. HDU 3487 Play with Chain | Splay

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

  4. HDU3487 Play With Chain [Splay]

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

  5. hdu3487Play with Chain

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

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

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

  7. 【HDU 3487】Play with Chain Splay

    题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...

  8. HDU3487 Play with Chain splay 区间反转

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

  9. HDU 3487 Play with Chain(Splay)

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

随机推荐

  1. python学习笔记:第二天(基本数据类型)

    Python3 基本数据类型 1.标准数据类型 Python3中有六个标准的数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictio ...

  2. HTML标签深入学习系列(1)——注释标签 <!-- -->

    一.HTML注释的语法 <!--注释内容--> 二.HTML注释的用处 1.普通注释(增强代码的可读性) 方便别人:方便其它程序员了解你的代码 方便自己:方便以后对自己代码的理解与修改等等 ...

  3. iptables 端口映射

    一.环境和要实现功能 PC1的网络设置如下: eth0       192.168.0.29 内网 eth1 219.239.11.22 外网 PC2的网络设置则为:192.168.0.21 内网 我 ...

  4. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

  5. 子组件索引$refs

    $refs只在组件渲染完成之后才填充,并且它是非响应式的,它仅仅作为一个直接访问访问子组件的应急方案-----应当避免的模板中或者计算属性中使用$refs

  6. JS获取元素的offsetTop,offsetLeft等相关属性

    1. obj.clientWidth //获取元素的宽度 obj.clientHeight //元素的高度 obj.offsetLeft //元素相对于父元素的left obj.offsetTop / ...

  7. [转]git merge 跟 git merge -no-ff

    根据这张图片可以看出 git merge –no-ff 可以保存你之前的分支历史.能够更好的查看 merge历史,以及branch 状态. git merge 则不会显示 feature,只保留单条分 ...

  8. 爬虫库之BeautifulSoup学习(一)

    Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. 官方解释如下: Beautiful Soup提供一些简单的.pytho ...

  9. matlab白底换红底

    白底换红底 局部阈值调整 边界高斯滤波 function demo global R global threshold global img global dstColor global rect g ...

  10. ubuntu删除g2o

    解决方法为:(1)删除/usr/local/include/g2o,指令为sudo rm -rf /usr/local/include/g2o:(2)删除/usr/local/lib下有关libg2o ...