链接

简单的两种操作,一种删除某段区间,加在第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. 编译Thrift

    按照 https://syslint.com/blog/tutorial/how-to-install-apache-thrift-on-ubuntu-14-04/ 进行, 编译时出现错误 make[ ...

  2. TCP/IP 协议 —— ARP

    通过 ARP 广播获得对方的 MAC 地址: 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议. 1. 特点 ARP ...

  3. 记一次关于return的错误

     有时候瞎JB程序,调一天东改西改,都发现不了错:到最后弄出来发现就是那样一个SB错误,真不知道该笑还是该哭. 这个if语句中的return,如果加了那么判断了if语句成立后,下面就不再执行了.   ...

  4. [CTSC 2018] 混合果汁

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5343 [算法] 对于每组询问 , 首先二分答案 显然 , 最优策略为优先选择价格低的 ...

  5. bzoj 4289 PA2012 Tax——构图

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 可以把一个点上的边按权值排序,然后边权小的向第一个比它大的连差值的边,边权大的向第一个 ...

  6. 在IIS6.0以上版本发布Ajax中,解决添加.v路径找不到的问题?

    问题描述:配置Aiax方式如下: 1.在AppCode中加入文件夹Ajax,加入两个类文件: Ajax.cs: using System; using System.Collections.Gener ...

  7. Object.prototype.toString.call(obj)检测数据类型

    typeof bar=='object' 不能确切判断数据是一个‘纯粹’的对象 Array null的结果都是object 比较好的方法是: Object.prototype.toString.cal ...

  8. 【220】◀▶ IDL 数组操作函数说明

    参考:Array Creation Routines —— 创建数组函数参考:Array Manipulation Routines —— 操作数组函数 01   MAX 最大值. 02   MIN ...

  9. JSBridge框架解决通信问题实现移动端跨平台开发

    一.跨平台开发是趋势 目前主流的移动端平台主要是Android和iOS,为了尽可能复用代码和节省开发成本,各大巨头都开发了自己的跨平台框架,比如Facebook的React-Native.阿里的Wee ...

  10. shader实例(八)渲染路径RenderingPath

    Unity的摄像机上支持3种RenderingPath,分别是VertexLit,Forward和Dferred Lighting,而shader中的LightMode标签Vertex,Forward ...