ZOJ 3765 Lights (zju March I)伸展树Splay
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的更多相关文章
- 树-伸展树(Splay Tree)
伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...
- 纸上谈兵: 伸展树 (splay tree)[转]
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们讨论过,树的搜索效率与树的深度有关.二叉搜索树的深度可能为n,这种情况下,每 ...
- K:伸展树(splay tree)
伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(lgN)内完成插入.查找和删除操作.在伸展树上的一般操作都基于伸展操作:假设想要对一个二叉查找树执行一系列的查找操作,为了使 ...
- 高级搜索树-伸展树(Splay Tree)
目录 局部性 双层伸展 查找操作 插入操作 删除操作 性能分析 完整源码 与AVL树一样,伸展树(Splay Tree)也是平衡二叉搜索树的一致,伸展树无需时刻都严格保持整棵树的平衡,也不需要对基本的 ...
- ZOJ 3765 Lights (伸展树splay)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765 Lights Time Limit: 8 Seconds ...
- 【BBST 之伸展树 (Splay Tree)】
最近“hiho一下”出了平衡树专题,这周的Splay一直出现RE,应该删除操作指针没处理好,还没找出原因. 不过其他操作运行正常,尝试用它写了一道之前用set做的平衡树的题http://codefor ...
- [Splay伸展树]splay树入门级教程
首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...
- 伸展树Splay【非指针版】
·伸展树有以下基本操作(基于一道强大模板题:codevs维护队列): a[]读入的数组;id[]表示当前数组中的元素在树中节点的临时标号;fa[]当前节点的父节点的编号;c[][]类似于Trie,就是 ...
- 伸展树(Splay tree)的基本操作与应用
伸展树的基本操作与应用 [伸展树的基本操作] 伸展树是二叉查找树的一种改进,与二叉查找树一样,伸展树也具有有序性.即伸展树中的每一个节点 x 都满足:该节点左子树中的每一个元素都小于 x,而其右子树中 ...
随机推荐
- 0108 spring的申明式事务
背景 互联网的金融和电商行业,最关注数据库事务. 业务核心 说明 金融行业-金融产品金额 不允许发生错误 电商行业-商品交易金额,商品库存 不允许发生错误 面临的难点: 高并发下保证: 数据一致性,高 ...
- VUE- 访问服务器端数据 axios
VUE- 访问服务器端数据 axios 一,安装 npm install axios 二,在http.js中引入 import axios from 'axios'; 三,定义http request ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-eject
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Golang的常量定义及使用案例
Golang的常量定义及使用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常量的定义 package main import ( "fmt" ) fu ...
- 概率图模型之EM算法
一.EM算法概述 EM算法(Expectation Maximization Algorithm,期望极大算法)是一种迭代算法,用于求解含有隐变量的概率模型参数的极大似然估计(MLE)或极大后验概率估 ...
- 102-PHP多维数组的元素输出
<?php //定义一个三维数组 $grade=array('class1'=>array('stu1'=>array('yuwen'=>85,'shuxue'=>95, ...
- Flutter如何引用第三方库并使用
Flutter如何引用第三方库并使用 https://www.jianshu.com/p/bbda7794345e Flutter官网点击访问Flutter教程(一)Flutter概览Flutter教 ...
- 代做Assignment时排比结构的使用解析
排比句式的作用想必各位留学生都不陌生,同理,在英文写作中,不管是从形式还是内容上来说,排比结构的作用都是强调.但是要注意,不能在分析的时候用太多这种套话,尽量还是能够根据具体情况具体分析.静态,小编将 ...
- pop3&smtp
pop3&smtp pop3 Post Office Protocol - Version 3 pop3协议是离线邮件协议,是客户端取邮件用的. 默认监听在TCP:110端口. POP3会话有 ...
- webpack知识点散记
1.今天学习webpack ,刚开头就碰到了钉子,因为现在都是4+的版本,用以前的老命令不好使,如下例子及解决办法 不好用: webpack3的 打包文件 webpack a.js b.j ...