链接

splay的增删改操作。

刚开始对于某段区间首先有了lazy标记时,把其左右孩子给交换了,导致在pushup时又交换了一次而debug了n久。

 #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;
int a[N][];
struct splay_tree
{
int pre[N],size[N],vv[N];
int ch[N][];
int root,n,tot,num,tot1;
int s[N][],sta[N],key[N];
void dfs(int x)
{
if(x)
{
dfs(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d s0=%d s1 = %d vv=%d\n",
x,ch[x][],ch[x][],pre[x],size[x],key[x],s[x][],s[x][],vv[x]);
dfs(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
dfs(root);
}
//以上用于debug*/
void newnode(int &x,int v,int sta,int fa)//新建一结点
{
x = ++tot;
ch[x][]=ch[x][] = ;
pre[x] = fa;
s[x][] = s[x][] = ;
s[x][sta] = v;
size[x] = ;
vv[x] = sta;
key[x] = v;
}
void pushup(int w)//由儿子更新其父亲
{
size[w] = size[ch[w][]]+size[ch[w][]]+;
int st = vv[w];
int l = ch[w][],r = ch[w][];
int cnt1 = ,cnt2 = ;
cnt1 = s[l][];
if(s[r][])
{
if(cnt1) cnt1 = __gcd(s[r][],cnt1);
else cnt1 = s[r][];
}
cnt2 = s[l][];
if(s[r][])
{
if(cnt2) cnt2 = __gcd(s[r][],cnt2);
else cnt2 = s[r][];
}
s[w][] = cnt1,s[w][] = cnt2;
s[w][st] = s[w][st]?__gcd(s[w][st],key[w]):key[w];
//cout<<s[w][0]<<" "<<s[w][1]<<endl;
}
void rotate(int r,int kind)//旋转操作,根据kind进行左旋和右旋
{
int y = pre[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下
{
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;
while(size[ch[r][]]+!=k)
{
if(size[ch[r][]]>=k)
r = ch[r][];
else
{
k-=(size[ch[r][]]+);//根据左右结点的数量来确定第k个节点在哪里
r = ch[r][];
}
}
pushup(r);
return r;
}
void add(int k,int val,int sta)//添加一个结点 位置自己修改 这里为第一个
{
splay(get_k(k),);
splay(get_k(k+),root);
int r = ch[root][];
newnode(ch[r][],val,sta,r);
pushup(r);
pushup(root);
}
void updelete(int r)//删除第r个结点
{
splay(get_k(r),);
splay(get_k(r+),root);
key_value = ;
pushup(ch[root][]);
pushup(root);
}
void update(int k,int flag,int v) //更新区间信息
{
splay(get_k(k),);
splay(get_k(k+),root);
if(flag)
{
vv[key_value]^=;
swap(s[key_value][],s[key_value][]);
}
else
{key[key_value] = v;s[key_value][vv[key_value]] = v;}
pushup(ch[root][]);
pushup(root); }
int query(int l,int r,int sta)//询问l,r区间,将第l-1个结点旋自根,第r+1个结点旋自根的有儿子,
{
//则l-r就变成了根的右儿子的左儿子
splay(get_k(l),);
splay(get_k(r+),root);
return s[key_value][sta];
}
void build(int &x,int l,int r,int fa)
{
int m = (l+r)>>;
if(l>r) return ;
newnode(x,a[m][],a[m][],fa);
build(ch[x][],l,m-,x);
build(ch[x][],m+,r,x);
pushup(x);
}
void init(int o)
{
int i;
for(i = ; i <= o; i++)
scanf("%d%d",&a[i][],&a[i][]);
size[] = ch[][] = ch[][] = s[][] = s[][] = key[] = vv[] = ;
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)
{
SP.init(n);
while(q--)
{
char sq[];
int k,x,y;
scanf("%s%d",sq,&k);
if(sq[]=='I')
{
scanf("%d%d",&x,&y);
SP.add(k+,x,y);
// SP.debug();
}
else if(sq[]=='D')
{
SP.updelete(k);
}
else if(sq[]=='R')
{
SP.update(k,,);
}
else if(sq[]=='M')
{
scanf("%d",&x);
SP.update(k,,x);
}
else if(sq[]=='Q')
{
scanf("%d%d",&x,&y);
int ans = SP.query(k,x,y);
if(ans==)
printf("-1\n");
else
printf("%d\n",ans);
// SP.debug();
}
}
}
return ;
}

zoj3765Lights(splay)的更多相关文章

  1. ZOJ3765---Lights (Splay伸展树)

    Lights Time Limit: 8 Seconds      Memory Limit: 131072 KB Now you have N lights in a line. Don't wor ...

  2. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  3. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  4. splay最终模板

    来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...

  5. bzoj 3506 && bzoj 1552 splay

    查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...

  6. 【splay】文艺平衡树 BZOJ 3223

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  7. 【填坑】bzoj3224 splay裸题

    人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...

  8. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

  9. BZOJ1500: [NOI2005]维修数列[splay ***]

    1500: [NOI2005]维修数列 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 12278  Solved: 3880[Submit][Statu ...

随机推荐

  1. day2-python数据类型及关系

  2. 如何创建WindowsService

    创建Windows Service可分为以下几步: 1. 创建一个“Windows Service”项目 2. 设置服务的相关属性,以确定服务的名称及工作机制 属性 设置 ServiceName 服务 ...

  3. UIButton设置为圆形按钮并增加边框

    设置按钮的长和宽尺寸一致(即为正方形),然后将圆角半径设为边长的一半,即形成一个圆形 UIButton *btn = [UIButton buttonWithType:UIButtonTypeSyst ...

  4. 关于Spring Security的笔记

    1.web.xml配置文件 加载Spring Security,将DelegatingFilterProxy配置在DispatcherServlet之前. <filter> <fil ...

  5. chan_ss7 呼出的时候指定使用某个CICs,或者CICs范围 的方法

      Linkset one: 1-31 Incoming 33-58 Outgoing 58-63 Emergency Traffic Linkset two: 1-31 Incoming 33-58 ...

  6. 有关如何线程安全的使用map(hashMap)

    最近在写一个多线程中控制输出顺序的系统中的一个代码,使用了map的数据结构.具体的业务是需要一个单例的对象,然后需要在多线程的环境下实现添加和删除的操作.部分代码如下: public class Up ...

  7. monkey之monkeyServer

    基本命令: adb shell monkey --port 1080 & adb forward tcp:1080 tcp:1080 telnet 127.0.0.1 1080 启动andro ...

  8. 极客时间_Vue开发实战_04.开发环境搭建

    Vue CLI的形式搭建环境: vue create hello-world 我们选择default默认的配置,提供babel和eslint的支持.如果你已经对工程化的东西非常了解了.你可以选择自定义 ...

  9. 数据可视化-svg入门基础(二)

    接上一篇:数据可视化-svg入门基础(一),基础一主要是介绍了svg概念,元素样式设置等. svg是(scalable vector graphic)伸缩矢量图像. 一.目录 (1)图形元素 (2)文 ...

  10. HDU1829【种类并查集】

    题意: 检验给出条件是否有同性恋. 思路: 条件并查集. 还是一个类似的前缀和,sum[x]是x到根这段路径上的和,根一定是坐标越小的, 那么如果说对于同类(同一个集合)的判断就sum[a]是否等于s ...