链接

简单的两种操作,一种删除某段区间,加在第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. hdu-5728 PowMod(数论)

    题目链接: PowMod Time Limit: 3000/1500 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Others) ...

  2. 基于区域的全卷积神经网络(R-FCN)简介

    在 Faster R-CNN 中,检测器使用了多个全连接层进行预测.如果有 2000 个 ROI,那么成本非常高. feature_maps = process(image)ROIs = region ...

  3. bzoj 4398 福慧双修——二进制分组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4398 如果枚举1号点走哪些点出去,就从那些点出发跑多源最短路即可.最短路不会重复经过一条边. ...

  4. rsync应用实例

      一. 通过ssh的方式 前面介绍的rsync 5种方式当中,第二.第三(1个冒号)就属于通过ssh的方式,这种方式其实就是让用户去登录到远程机器,然后执行rsync的任务. [root@local ...

  5. 【225】ArcEngine 实现要素添加 & 删除

    参考:ArcGIS Engine效率探究——要素的添加和删除.属性的读取和更新 删除要素 //添加图层,显示在最上面 axMapControl1.AddShapeFile(@"D:\01-业 ...

  6. C++ windows 多线程 互斥锁

    #include <opencv2/core/version.hpp> #include <opencv2/calib3d/calib3d.hpp> #include < ...

  7. Eigen::aligned_allocator

    http://blog.csdn.net/rs_huangzs/article/details/50574141

  8. sql之索引

     作用: - 约束 - 加速查找 普通索引:加速查找 create index 索引名称 on 表名(列名,) drop index 索引名称 on 表名 主键索引:加速查找+不能为空+不能重复 cr ...

  9. Identity Server 4 原理和实战(完结)_----选看 OAuth 2.0 简介(下)

    https://www.yuque.com/yuejiangliu/dotnet/asu0b9 端点 Endpoint Authorization Endpoint,授权端点 在浏览器里面和用户交互 ...

  10. SQL2005恢复只有mdf文件的数据库

    我把原来的数据库分离后,直接把日志文件给干掉了.原来在SQL 2000里经常这么干,只用一个mdf就附加了.没想到sql2005居然不行.我试验了一圈 终于找到一个成功的方法.转载,供后来者参考. S ...