bzoj3224 普通平衡树(splay 模板)
3224: Tyvj 1728 普通平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 11427 Solved: 4878
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)
Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
84185
492737
HINT
Source
#include <cstdio>
#define Maxn 1000000
using namespace std;
int f[Maxn];//father
int ch[Maxn][];//child ; 0 for left ; 1 for right
int key[Maxn];//key
int cnt[Maxn];//value
int siz[Maxn];//size of subtree
int sz,root;//size of tree and root
//clear the ndoe void clear(int x)
{
ch[x][]=ch[x][]=f[x]=cnt[x]=key[x]=siz[x]=;
} //rightson return 1;left son return 0
int getson(int x)
{
return ch[f[x]][]==x;
} //update the size
void update(int x)
{
siz[x]=cnt[x];
if (ch[x][]) siz[x]+=siz[ch[x][]];
if (ch[x][]) siz[x]+=siz[ch[x][]];
} //retation
int rotate(int x)
{
int fa=f[x],fafa=f[fa],k=getson(x);
ch[fa][k]=ch[x][k^];f[ch[fa][k]]=fa;
ch[x][k^]=fa;f[fa]=x;
f[x]=fafa;
if (fafa)
ch[fafa][ch[fafa][]==fa]=x;
update(fa);update(x);
} //rotate until x is the root
void splay(int x)
{
for (int fa;fa=f[x];rotate(x))
if (f[fa])
rotate(getson(x)==getson(fa) ? fa : x);
root=x;
} int pre()
{
int now=ch[root][];
while(ch[now][])
now=ch[now][];
return now;
} int nex()
{
int now=ch[root][];
while(ch[now][])
now=ch[now][];
return now;
} //find x's pos
int findpos(int v)
{
int now=root,ans=;
while()
{
if (v<key[now])
now=ch[now][];
else
{
ans+=ch[now][]?siz[ch[now][]]:;
if (v==key[now])
{
splay(now);
return ans+;
}
ans+=cnt[now];
now=ch[now][];
}
}
} //find pos's x
int findx(int x)
{
int now=root;
while()
{
if (ch[now][] && x<=siz[ch[now][]])
now=ch[now][];
else
{
int temp=(ch[now][]?siz[ch[now][]]:)+cnt[now];
if (x<=temp)
return key[now];
x-=temp;
now=ch[now][];
}
}
} //ceate a new splay node
void create(int v)
{
sz++;
ch[sz][]=ch[sz][]=f[sz]=;
key[sz]=v;
cnt[sz]=;
siz[sz]=;
//root=sz;
} //insert a node
void insert(int v)
{
if (!root)
create(v),root=sz;
else
{
int now=root,fa=;
while()
{
if (key[now]==v)
{
cnt[now]++;
update(now);update(fa);
splay(now);
break;
}
fa=now;
now=ch[fa][v>key[fa]];
if (!now)
{
create(v);
f[sz]=fa;
ch[fa][v>key[fa]]=sz;
update(fa);
splay(sz);
break;
}
}
}
} void del(int x)
{
int t=findpos(x);
if (cnt[root]>)
{
cnt[root]--;
update(root);
return;
}
//none
if (!ch[root][] && !ch[root][])
{
clear(root);
root=;
return;
}
//one
if (!ch[root][])
{
int temp=root;
root=ch[root][];
f[root]=;
clear(temp);
return;
}
else
if (!ch[root][])
{
int temp=root;
root=ch[root][];
f[root]=;
clear(temp);
return;
}
//two
int pre1=pre(),temp=root;
splay(pre1);
f[ch[temp][]]=root;
ch[root][]=ch[temp][];
clear(temp);
update(root);
} int main()
{
int n,opt,x;
scanf("%d",&n);
for (int i=;i<=n;++i)
{
scanf("%d%d",&opt,&x);
switch(opt)
{
case : insert(x); break;
case : del(x); break;
case : printf("%d\n",findpos(x)); break;
case : printf("%d\n",findx(x)); break;
case : insert(x); printf("%d\n",key[pre()]); del(x); break;
case : insert(x); printf("%d\n",key[nex()]); del(x); break;
}
}
}
#include<iostream>
#include<cstdio>
#include<cstring> #define N 1000000 using namespace std;
int f[N],ch[N][],key[N],cnt[N],siz[N],sz,root; inline int read()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} inline void update(int x)
{
siz[x]=cnt[x];
if(ch[x][]) siz[x]+=siz[ch[x][]];
if(ch[x][]) siz[x]+=siz[ch[x][]];
} int pre()
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];return now;
} int nex()
{
int now=ch[root][];
while(ch[now][]) now=ch[now][];return now;
} inline int getson(int x)
{
return ch[f[x]][]==x;
} inline void rorate(int x)
{
int fa=f[x],ffa=f[fa],k=getson(x);
ch[fa][k]=ch[x][k^];f[ch[fa][k]]=fa;
ch[x][k^]=fa;f[fa]=x;f[x]=ffa;
if(ffa)ch[ffa][ch[ffa][]==fa]=x;
update(fa);update(x);
} void splay(int x)
{
for(int fa;fa=f[x];rorate(x))
if(f[fa]) rorate(getson(x)==getson(fa)?fa:x);
root=x;
} int findpos(int x)
{
int now=root,ans=;
while()
{
if(x<key[now]) now=ch[now][];
else
{
ans+=ch[now][]?siz[ch[now][]]:;
if(x==key[now])
{
splay(now);return ans+;
}
ans+=cnt[now];now=ch[now][];
}
}
} int findx(int x)
{
int now=root;
while()
{
if(ch[now][]&&x<=siz[ch[now][]]) now=ch[now][];
else
{
int tmp=(ch[now][]?siz[ch[now][]]:)+cnt[now];
if(x<=tmp) return key[now];
x-=tmp;now=ch[now][];
}
}
} void clear(int x)
{
ch[x][]=ch[x][]=cnt[x]=siz[x]=key[x]=f[x]=;
} void creat(int x)
{
sz=sz+;key[sz]=x;cnt[sz]=siz[sz]=;
ch[sz][]=ch[sz][]=f[sz]=;
} void insert(int x)
{
if(!root) creat(x),root=sz;
else
{
int now=root,fa=;
while()
{
if(key[now]==x)
{
cnt[now]++;siz[now]++;splay(now);
break;
}
fa=now;now=ch[fa][x>key[fa]];
if(!now)
{
creat(x);f[sz]=fa;ch[fa][x>key[fa]]=sz;
splay(sz);break;
}
}
}
} void del(int x)
{
int t=findpos(x);
if(cnt[root]>)
{
cnt[root]--;siz[root]--;return;
}
if(!ch[root][]&&!ch[root][])
{
clear(root);root=;return;
}
if(!ch[root][])
{
int tmp=root;root=ch[root][];f[root]=;
clear(tmp);return;
}
if(!ch[root][])
{
int tmp=root;root=ch[root][];f[root]=;
clear(tmp);return;
}
int pre1=pre(),tmp=root;splay(pre1);
ch[root][]=ch[tmp][];f[ch[tmp][]]=root;
clear(tmp);update(root);
} int main()
{
int n,opt,x;
n=read();
for(int i=;i<=n;i++)
{
opt=read();x=read();
switch(opt)
{
case :insert(x);break;
case :del(x);break;
case :printf("%d\n",findpos(x));break;
case :printf("%d\n",findx(x));break;
case :insert(x);printf("%d\n",key[pre()]);del(x);break;
case :insert(x);printf("%d\n",key[nex()]);del(x);break;
}
}
}
我的马蜂
bzoj3224 普通平衡树(splay 模板)的更多相关文章
- [luogu3369/bzoj3224]普通平衡树(splay模板、平衡树初探)
解题关键:splay模板题整理. 如何不加入极大极小值?(待思考) #include<cstdio> #include<cstring> #include<algorit ...
- bzoj3224 普通平衡树 splay模板
题目传送门 题目大意:完成一颗splay树. 思路:模板题,学着还是很有意思的. 学习splay树:蒟蒻yyb 该题模板:汪立超 #include<bits/stdc++.h> #defi ...
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- BZOJ3224:普通平衡树(Splay)
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 1. 插入x数 2. 删除x数(若有多个相同的数,因只删除一个) 3. 查询x数的排名(若有多个相 ...
- P3369 【模板】普通平衡树 (splay 模板)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- 【BZOJ3224】Tyvj 1728 普通平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3224 题意概括 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. ...
- BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...
随机推荐
- TestNG参数化测试
参数化有两种方法: 第一种:在xml文件中声明 第二种:用@DataProvider注解 先介绍第一种方法: ParameterTest类:用@Parameters({"name" ...
- CSC
CSC CSC Table of Contents 1. account 2. Contacts 3. <国家公派留学人员预订回国机票说明> 4. 回国手续 4.1. 申办及开具<留 ...
- BZOJ 1834 Luogu P2604 [ZJOI2010]网络扩容 (最小费用最大流)
题目连接: (luogu) https://www.luogu.org/problemnew/show/P2604 (bzoj) https://www.lydsy.com/JudgeOnline/p ...
- Codeforces Round #506 (Div. 3)B.Creating the Contest(dp)
B. Creating the Contest time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【Codeforces 375A】Divisible by Seven
[链接] 我是链接,点我呀:) [题意] 让你把一个包含数字1,6,8,9的数字重新组合,使得组合成的数字能被7整除 [题解] 我们先提取出来1,6,8,9各1个 然后把剩余的len-4个数字除了0之 ...
- 数据库中间件MyCat学习总结(2)——MyCat-Web原理介绍
Mycat是一个分库分表的基于java开发的数据库中间件,使用过程中需要有一个监控系统,mycat-web应运而生.mycat-web是一个使用SpringMVC + Mybatis的监控平台,使用常 ...
- Scala解析Json格式
Scala解析Json格式 代码块 Scala原生包 导入包 import scala.util.parsing.json._ def main(args: Array[String]): Unit ...
- 基础算法(java版本)
Practice Author: Dorae Date: 2018年10月11日13:57:44 转载请注明出处 具体代码请移步git 基础算法 图 Prim Kruskal Dijkstra Flo ...
- UVA_1575
https://vjudge.net/problem/UVA-1575 枚举答案(k)..对k质因数分解,质数的指数为cnt[i],若n==A(tot_cnt,tot_cnt) / A(cnt[i]& ...
- 使用CDN
CDN的全称是Content Delivery Network.中文直译过来是:内容交付网络. 它的主要意思是,将某些内容进行交付的网络.对于站点开发而言,我们所讲的内容通常指的是内容文件(比如jav ...