ZJU 三月月赛题,当时见这个题目没辙,没学过splay,敲了个链表TLE了,所以回来好好学了下Splay,这道题目是伸展树的第二题,对于伸展树的各项操作有了更多的理解,这题不同于上一题的用指针表示整个树,采用纯数组表示,null节点即为0节点,这样就带来一个问题,就是有时候会有事没事就指向0节点,结果把0节点也算在结果里面,弄得我要几个地方都判断下。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 310000
using namespace std;
int tot,root,n,m;
int ch[N][],val[N],gcdst[N][],size[N],sta[N],pre[N];
int num[N],state[N];
int gcd(int a,int b) //这里处理gcd注意下,因为有-1要独判一下
{
if (a==-)
return b;
if (b==-)
return a;
if (a<b)
{
swap(a,b);
}
return b== ? a : gcd(b,a%b);
}
void newnode(int &rt,int fa,int v,int s)
{
rt=++tot;
ch[rt][]=ch[rt][]=;
val[rt]=v;
pre[rt]=fa;
gcdst[rt][s]=v;
gcdst[rt][s^]=-;
size[rt]=;
sta[rt]=s;
}
void pushup(int rt) //为了求不同状态的gcd,索性把每个gcd都求出来。然后再选择正确的状态的作为该点的gcd
{
int ll=ch[rt][],rr=ch[rt][];
size[rt]=;
if (ll) size[rt]+=size[ll];
if (rr) size[rt]+=size[rr];
if (!ll)
gcdst[ll][]=gcdst[ll][]=-;
if (!rr)
gcdst[rr][]=gcdst[rr][]=-;
gcdst[rt][]=gcd(gcdst[ll][],gcdst[rr][]);
gcdst[rt][]=gcd(gcdst[ll][],gcdst[rr][]);
gcdst[rt][sta[rt]]=gcd(gcdst[rt][sta[rt]],val[rt]);
}
void rotate(int x,int c)
{
int y=pre[x];
ch[y][!c]=ch[x][c];
if (ch[x][c])
{
pre[ch[x][c]]=y;
}
pre[x]=pre[y];
if (pre[y])
{
if (ch[pre[y]][]==y)
ch[pre[y]][]=x;
else
ch[pre[y]][]=x;
}
ch[x][c]=y;
pre[y]=x;
pushup(y);
if (x)
pushup(x);
if (root==y)
root=x;
}
void splay(int x,int f)
{
while (pre[x]!=f)
{
if (pre[pre[x]]==f)
{
if (ch[pre[x]][]==x)
{
rotate(x,);
}
else
rotate(x,);
}
else
{
int y=pre[x],z=pre[y];
if (ch[z][]==y)
{
if (ch[y][]==x)
{
rotate(y,);
rotate(x,);
}
else
{
rotate(x,);
rotate(x,);
}
}
else
{
if (ch[y][]==x)
{
rotate(x,);
rotate(x,);
}
else
{
rotate(y,);
rotate(x,);
}
}
}
}
pushup(x);
if (f)
pushup(f);
}
void select(int k,int f)
{
int x=root;
//cout<<"root is "<<x<<" left child is "<<ch[x][0]<<endl;
int tmp=;
k++;
for (;;)
{
tmp=;
if (ch[x][])
tmp=size[ch[x][]];
// cout<<k<<" "<<tmp<<endl;
//int temp;
//cin>>temp;
if (tmp+==k) break;
if (k<=tmp)
x=ch[x][];
else{
k-=tmp+;
x=ch[x][];
}
}
//cout<<"test "<<pre[x]<<" "<<f<<endl;
splay(x,f);
}
void add(int num,int v,int s)//插入操作,把要插入的位置先移到根,再处理即可
{
select(num,);
int nt;
int x=ch[root][];
int p=root;
while (x)
{
// cout<<x<<" tianjia "<<val[x]<<endl;
p=x;
x=ch[x][];
}
//`cout<<x<<" final "<<val[x]<<" "<<p<<" "<<val[p]<<endl; if (p==root)
{
newnode(ch[p][],p,v,s);
splay(ch[p][],);
}
else
{ newnode(ch[p][],p,v,s);
splay(ch[p][],);
}
}
void deletes(int num) //删除某个点,这个需要考虑下,若左子树或者右子树为空,则可直接消除,否则找到左子树的最大节点移到根,然后被删除的点即在右节点上,删除即可
{
select(num,);
if (ch[root][]==)
{
root=ch[root][];
pre[root]=;
return;
}
if (ch[root][]==)
{
root=ch[root][];
pre[root]=;
return;
}
int rc=ch[root][];
while (ch[rc][])
rc=ch[rc][];
splay(rc,root);
ch[rc][]=ch[root][];
pre[ch[root][]]=rc;
root=rc;
pre[root]=;
pushup(root);
}
void change(int k)
{
select(k,);
sta[root]^=;
pushup(root);
}
void query(int l,int r,int s)//查询某个区间的gcd,只要把区间的前一个节点放在根节点 后一个节点放在根的右节点,则区间必定就在根的右节点的左节点上
{
select(l-,);
// cout<<"pass"<<endl;
select(r+,root);
//cout<<"pass"<<endl;
int rt=ch[ch[root][]][];
//pushup(rt);
//cout<<rt<<" "<<gcdst[rt][s]<<" "<<gcdst[rt][1-s]<<endl;
printf("%d\n",gcdst[rt][s]);
}
void modify(int k,int v)//修改某个值,这无疑是最简单的操作,移到根节点后直接修改即可
{
select(k,);
val[root]=v;
pushup(root);
}
void build(int l,int r,int &rt,int fa)//建树过程
{
if (l>r) return;
int mid=(l+r)>>;
newnode(rt,fa,num[mid],state[mid]);
build(l,mid-,ch[rt][],rt);
build(mid+,r,ch[rt][],rt);
pushup(rt);
}
void init() //初始化过程
{
for (int i=;i<=n;i++)
{
scanf("%d%d",&num[i],&state[i]);
}
root=tot=;
ch[][]=ch[][]=;
pre[]=size[]=;val[]=-;
gcdst[][]=gcdst[][]=-;
newnode(root,,,-);
newnode(ch[root][],root,,-);
build(,n,ch[ch[root][]][],ch[root][]);
pushup(ch[root][]);
pushup(root);
}
void test() //测试最终生成的树
{
int d=size[root];
for (int i=;i<d-;i++)
{
select(i,);
cout<<i<<" val is "<<val[root]<<" state "<<sta[root]<<" gcd[0] "<<gcdst[root][]<<" gcd[1] "<<gcdst[root][]<<endl;
}
}
int main()
{
char ques[];
int a,b,c;
while (scanf("%d%d",&n,&m)!=EOF)
{
init();
//cout<<" "<<size[root]<<endl;
for (int i=;i<m;i++)
{
getchar();
scanf("%s",ques);
//puts(ques);
if (ques[]=='Q')
{
scanf("%d%d%d",&a,&b,&c);
//cout<<"pass"<<endl;
query(a,b,c);
// cout<<"p2"<<endl;
}
if (ques[]=='I')
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
if (ques[]=='D')
{
scanf("%d",&a);
deletes(a);
}
if (ques[]=='R')
{
scanf("%d",&a);
change(a);
}
if (ques[]=='M')
{
scanf("%d%d",&a,&b);
modify(a,b);
}
// test(); }
}
return ;
}

ZOJ 3765 Lights (zju March I)伸展树Splay的更多相关文章

  1. 树-伸展树(Splay Tree)

    伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...

  2. 纸上谈兵: 伸展树 (splay tree)[转]

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢!  我们讨论过,树的搜索效率与树的深度有关.二叉搜索树的深度可能为n,这种情况下,每 ...

  3. K:伸展树(splay tree)

      伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(lgN)内完成插入.查找和删除操作.在伸展树上的一般操作都基于伸展操作:假设想要对一个二叉查找树执行一系列的查找操作,为了使 ...

  4. 高级搜索树-伸展树(Splay Tree)

    目录 局部性 双层伸展 查找操作 插入操作 删除操作 性能分析 完整源码 与AVL树一样,伸展树(Splay Tree)也是平衡二叉搜索树的一致,伸展树无需时刻都严格保持整棵树的平衡,也不需要对基本的 ...

  5. ZOJ 3765 Lights (伸展树splay)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765 Lights Time Limit: 8 Seconds ...

  6. 【BBST 之伸展树 (Splay Tree)】

    最近“hiho一下”出了平衡树专题,这周的Splay一直出现RE,应该删除操作指针没处理好,还没找出原因. 不过其他操作运行正常,尝试用它写了一道之前用set做的平衡树的题http://codefor ...

  7. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  8. 伸展树Splay【非指针版】

    ·伸展树有以下基本操作(基于一道强大模板题:codevs维护队列): a[]读入的数组;id[]表示当前数组中的元素在树中节点的临时标号;fa[]当前节点的父节点的编号;c[][]类似于Trie,就是 ...

  9. 伸展树(Splay tree)的基本操作与应用

    伸展树的基本操作与应用 [伸展树的基本操作] 伸展树是二叉查找树的一种改进,与二叉查找树一样,伸展树也具有有序性.即伸展树中的每一个节点 x 都满足:该节点左子树中的每一个元素都小于 x,而其右子树中 ...

随机推荐

  1. hook框架frida添加至于安卓应用中

    转载至于https://koz.io/using-frida-on-android-without-root/ Frida is a great toolkit by @oleavr, used to ...

  2. 021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量

    #编写一个存储过程 CREATE PROCEDURE ShowDate ( ) BEGIN #变量定义 ); #变量赋值 set m_str1 = '曾经沧海难为水'; #输出当前时间 SELECT ...

  3. 文献阅读报告 - Social BiGAT + Cycle GAN

    原文文献 Social BiGAT : Kosaraju V, Sadeghian A, Martín-Martín R, et al. Social-BiGAT: Multimodal Trajec ...

  4. Netty的出现

    原生NIO存在的问题 NIO的类库和API复杂, 使用麻烦: 需要熟练掌握Selector.ServerSocketChannel.SocketChannel.ByteBuffer 等. 需要具备其他 ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-forward

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. Centos 8双网卡设置

    原理:不管开发板是通过直连.路由器还是交换机连接到PC机,最终都是接到PC的以太网网卡(对笔记本来说,一般存在两个网卡,一个WIFI网卡和以太网网卡):因此要实现PC机与虚拟机的互ping,必须把虚拟 ...

  7. Golang的基础数据类型-字符串型

    Golang的基础数据类型-字符串型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符型概述 上一篇博客学习了使用单引号括起来的单个字符就是字符类型,在Golang中有两种表 ...

  8. HDU - 3724 Encoded Barcodes (字典树)

    题意:给定n个字符串和m个经过处理得到的字符串,问对于m个字符串中的每个字符串,n个字符串中以该字符串为前缀的个数.分析:1.误差在[0.95x, 1.05x],因此求8个数的平均数,大于平均数为1, ...

  9. redis3.2.2 集群

    http://blog.csdn.net/imxiangzi/article/details/52431729 http://www.2cto.com/kf/201701/586689.html me ...

  10. cf 785#

    23333再次水惨了..(心酸啊) A题呵呵呵呵呵 #include<cstdio> #include<iostream> using namespace std; int m ...