链接

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. Myeclipse项目内容没有报错但是项目上面却有红色叉叉

    当src文件夹为空的时候,git是不提交空文件夹的,所以check出来的项目中没有src文件夹,这个时候也会出现此问题.

  2. June 26,程序破解

    1.android程序破解练习初级 方法一: 文件名:KeygenMe#1.apk工具:ApktoolGui v2.0 Final 先用ApktoolGui v2.0 Final反编译成java通过查 ...

  3. HihoCoder 1508 : 剑刃风暴(占位)

    描述 主宰尤涅若拥有一招非常厉害的招式——剑刃风暴,“无论是战士还是法师,都害怕尤涅若的武士刀剑技”. 现在战场上有N名敌对英雄,他们的位置分别为(Xi, Yi),而剑刃风暴的伤害范围是一个半径为R的 ...

  4. Python ip与数字的转换方式

    例子:IP:192.168.1.10 方法一: In [1]: bin(192)Out[1]: '0b11000000' In [2]: bin(168)Out[2]: '0b10101000' In ...

  5. java第五天之---方法与数组

    案例一:从键盘输入两个数据,比较大小 import java.util.Scanner;class FunctionTest { public static void main(String[] ar ...

  6. 如何判断一个for循环执行完毕

    在外面一个变量a=arr.leng; 然后就是进行for循环, 在for循环下面进行判断,因为如果结束那么i的值就会>=a;if条件成立的话,可以在里面进行循环完毕要做的操作.

  7. Python下使用Psyco模块优化运行速度

    今天介绍下Psyco模块,Psyco模块可以使你的Python程序运行的像C语言一样快.都说Python语言易用易学,但性能上跟一些编译语言(如C语言)比较要差不少,这里可以用C语言和Python语言 ...

  8. bzoj2257瓶子与燃料——最大公约数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2257 可以知道最终能够导出的燃料一定是瓶子容量的gcd的倍数,所以此题转化为求n个数中k个数 ...

  9. 解除win10禁ping方法

    在局域网中,有一台电脑安装了win10操作系统,win10电脑连接网络没有问题,就是在其他电脑ping这台windows10电脑时就是ping不通,显示“请求超时.”.虽然这台windows10电脑联 ...

  10. vscode实现列编辑

    ctrl + shift + 左键选择要编辑的列 好用,再也不用使用\n替换了