[知识点]平衡树之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 ...
随机推荐
- Android OkHttp完全解析 --zz
参考文章 https://github.com/square/okhttp http://square.github.io/okhttp/ 泡网OkHttp使用教程 Android OkHttp完全解 ...
- Android中Service 使用详解(LocalService + RemoteService)
Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- 在RedHat.Enterprise.Linux_v6.3系统中安装Oracle_11gR2教程
在RedHat.Enterprise.Linux_v6.3系统中安装Oracle_11gR2教程 本教程提供PDF格式下载: 在RedHat.Enterprise.Linux_v6.3系统中安装Ora ...
- slf4i + logback 配置
一.所需jar包: slf4j-api-1.6.1.jar logback-classic-0.9.24.jar logback-core-0.9.24.jar 二.logback.xml配置示例: ...
- WebView相关
Android WebView使用基础 Android WebView中的JavaScript代码使用 很不错的例子:android webview js交互 第一节 (java和js交互)
- MySQL模糊查询:LIKE模式和REGEXP模式
MySQL模糊查询提供了两种模式:LIKE模式和REGEXP模式. LIKE模式 LIKE模式是使用的LIKE 或 NOT LIKE 比较运算符进行模糊查询. SELECT 字段 FROM 表 WHE ...
- Jquery获取select,dropdownlist,checkbox下拉列表框的值
jQuery获取 Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); ...
- RDS MySQL 全文检索相关问题的处理
RDS MySQL 全文检索相关问题 1. RDS MySQL 对全文检索的支持 2. RDS MySQL 全文检索相关参数 3. RDS MySQL 全文检索中文支持 3.1 MyISAM 引擎表 ...
- 让sql语句不排序,按照in语句的顺序返回结果
SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509) ORDER BY INSTR(',443,419,431,440, ...
- java gui 之容器组件
演示Frame和Panel package unit7; import java.awt.*; public class SimpleFrame { public static void main(S ...