[知识点]平衡树之Splay
// 此博文为迁移而来,写于2015年7月18日,不代表本人现在的观点与看法。原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6rg.html
3.Zig-zag操作

#include<cstring>
#include<algorithm>
#include<queue>
#define INF 1<<30
using namespace std;
{
int son[2],l,r,m,k,t1,t2,s,sum,fa;
};
char now[10];
queue Q;
{
if (!now) return;
tree[now].k=val,tree[now].t1=1,tree[now].sum=tree[now].k*tree[now].s;
if (tree[now].k>0) tree[now].l=tree[now].r=tree[now].m=tree[now].sum;
else tree[now].l=tree[now].r=tree[now].m=tree[now].k;
}
{
if (!now) return;
swap(tree[now].son[0],tree[now].son[1]),tree[now].t2^=1;
swap(tree[now].l,tree[now].r);
}
{
if (!now) return;
if (tree[now].t1) plus1(tree[now].son[0],tree[now].k),plus1(tree[now].son[1],tree[now].k),tree[now].t1=0;
if (tree[now].t2) plus2(tree[now].son[0]),plus2(tree[now].son[1]),tree[now].t2=0;
}
{
if (!now) return;
push(now);
int lson=tree[now].son[0],rson=tree[now].son[1];
tree[now].s=tree[lson].s+tree[rson].s+1;
tree[now].sum=tree[lson].sum+tree[rson].sum+tree[now].k;
tree[now].l=max(tree[lson].l,tree[lson].sum+tree[now].k+max(tree[rson].l,0));
tree[now].r=max(tree[rson].r,tree[rson].sum+tree[now].k+max(tree[lson].r,0));
tree[now].m=max(max(tree[lson].m,tree[rson].m),tree[now].k+max(tree[lson].r,0)+max(tree[rson].l,0));
}
{
int fa=tree[now].fa,gfa=tree[fa].fa,q=(tree[fa].son[1]==now),son=tree[now].son[q^1];
if (gfa) tree[now].fa=gfa,tree[gfa].son[tree[gfa].son[1]==fa]=now;
else tree[now].fa=0,root=now;
tree[fa].son[q]=son; if (son) tree[son].fa=fa;
tree[fa].fa=now,tree[now].son[q^1]=fa;
update(fa),update(now);
// 让我来翻译一下这段话:
// 新建变量:我的爸爸,我爸爸的爸爸,“我是我爸的右儿子”判断,我的左(右)儿子;
// 如果我有爷爷,那么我的爸爸是我的爷爷,我的爷爷的儿子是我,左还是右取决于我爸爸是我爷爷的左还是右儿子;
// 如果我没爷爷,那么我爸爸也没有了,我就是你们的祖宗
// 我爸爸的儿子是我的儿子;如果我有儿子,那么我儿子的爸爸是我的爸爸
// 我爸爸的爸爸是我,我的左(右)儿子是我爸爸。
{
int k=0;
for (int x=now;x;x=tree[x].fa) { k++; a[k]=x; }
for (int i=k;i>=1;i--) push(a[i]);
while (tree[now].fa) rotate(now);
}
{
int mid=(l+r)>>1;
if (l<mid) tree[now].son[0]=Q.front(),Q.pop(),build(tree[now].son[0],l,mid-1),tree[tree[now].son[0]].fa=now;
scanf("%d",&tree[now].k);
if (mid<r) tree[now].son[1]=Q.front(),Q.pop(),build(tree[now].son[1],mid+1,r),tree[tree[now].son[1]].fa=now;
update(now);
}
{
int now=root;
while (now)
{
push(now);
if (tree[tree[now].son[0]].s+1==q) return now;
if (tree[tree[now].son[0]].s>=q) now=tree[now].son[0];
else q-=tree[tree[now].son[0]].s+1,now=tree[now].son[1];
}
return 0;
}
{
int pos,tot,l,r,root; scanf("%d %d",&pos,&tot);
l=rank(pos),r=rank(pos+1); splay(l),splay(r);
root=Q.front(),Q.pop(),build(root,1,tot);
if (l) tree[l].son[1]=root,tree[root].fa=l;
else tree[r].son[0]=root,tree[root].fa=r;
update(l),update(r);
}
{
if (tree[now].son[0]) DFS(tree[now].son[0]);
if (tree[now].son[1]) DFS(tree[now].son[1]);
Q.push(now),memset(tree+now,0,sizeof(tree[now]));
}
{
int pos,tot,l,r; scanf("%d %d",&pos,&tot);
l=rank(pos-1),r=rank(pos+tot); splay(l),splay(r);
if (l) DFS(tree[l].son[1]),tree[l].son[1]=0;
else DFS(tree[r].son[0]),tree[r].son[0]=0;
update(l),update(r);
}
{
int pos,tot,c,l,r; scanf("%d %d %d",&pos,&tot,&c);
l=rank(pos-1),r=rank(pos+tot); splay(l),splay(r);
if (l) plus1(tree[l].son[1],c); else plus1(tree[r].son[0],c);
update(l),update(r);
}
{
int pos,tot,l,r; scanf("%d %d",&pos,&tot);
l=rank(pos-1),r=rank(pos+tot); splay(l),splay(r);
if (l) plus2(tree[l].son[1]); else if (r) plus2(tree[r].son[0]); else plus2(root);
update(l),update(r);
}
{
int pos,tot,l,r; scanf("%d %d",&pos,&tot);
l=rank(pos-1),r=rank(pos+tot); splay(l),splay(r);
if (l) return tree[tree[l].son[1]].sum;
}
{
freopen("1500.in","r",stdin);
freopen("1500.out","w",stdout);
scanf("%d %d",&n,&m);
for (int i=1;i<=MAXN;i++) Q.push(i);
tree[0].l=tree[0].r=tree[0].m=-INF;
root=Q.front(),Q.pop(),build(root,1,n);
}
{
init();
for (int i=1;i<=m;i++)
{
scanf("%s",&now);
if (now[0]=='I') ins();
else if (now[0]=='D') del();
else if (now[2]=='K') makeSame();
else if (now[0]=='R') reverse();
else if (now[0]=='G') printf("%d\n",getSum());
else printf("%d\n",maxSum());
}
return 0;
}
[知识点]平衡树之Splay的更多相关文章
- P3391 【模板】文艺平衡树(Splay)新板子
P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转 ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...
- 平衡树(Splay):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
- BZOJ3224普通平衡树【Splay】
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 11751 Solved: 5013 Descriptio ...
- 【BZOJ3223】文艺平衡树(Splay)
题面 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...
- BZOJ 3224 - 普通平衡树 - [Treap][Splay]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...
- P3369 【模板】普通平衡树(splay)
P3369 [模板]普通平衡树 就是不用treap splay板子,好好背吧TAT #include<iostream> #include<cstdio> #include&l ...
随机推荐
- wireshark使用
http://jingyan.baidu.com/article/7f41ececede744593c095c79.html
- 22.访问者模式(Vistor Pattern)
using System; using System.Collections; namespace ConsoleApplication5 { /// <summary> /// 访问者模 ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- C# TreeView使用技巧
节点勾选设置 TreeView树中节点勾选要求: 1.不选中一个节点,则其所有的子节点都不被选中. 2.选中一个节点,则其所有的子节点都被选中. 3.当一个节点的所有子节点都没有被选中时,该节点也没有 ...
- 用脚本创建和恢复 DB2数据库
CREATE DATABASE AUTOMATIC STORAGE YES ON 'C:\' DBPATH ON 'C:\' USING CODESET GBK TERRITORY CN COLLAT ...
- 数据结构之图 Part1
Part 1 预计使用7天的时间来过掉图相关的数据结构.第一天主要是一天图的基本概念,熟练掌握定义是一切交流和沟通的基础. 1定义 1.1图 有穷非空顶点,外加边. G(V,E) Graph Vert ...
- Oracle【IT实验室】数据库备份与恢复之二:SQL*Loader
2.1 基本知识 Oracle 的 SQL* LOADER 可以将外部格式化的文本数据加载到数据库表中.通常 与 SPOOL导出文本数据方法配合使用. 1.命令格式 SQLLDR keyw ...
- WPF 实现带标题的TextBox
这篇博客将分享在WPF中如何创建一个带Title的TextBox.首先请看一下最终的效果, 实现思路:使用TextBlock+TextBox来实现,TextBlock用来显示Title. 实现代码, ...
- barabasilab-networkScience学习笔记4-无标度特征
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- python学习第一天
编码:encode 解码:decode 计算长度:len list:索引从0开始 -1可以索引最后一个元素 append添加元素 insert插入元素 pop删除末尾元素 替换的话直接赋值 list里 ...