bzoj 3224 splay模板题4
再刷水题我就废了。。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define lc(x) ch[x][0]
#define rc(x) ch[x][1]
#define inf 0x3f3f3f3f
#define N 200005
using namespace std;
int ch[N][];
int k[N];int cnt,root;
int size[N],fa[N];
void push_up(int x)
{
size[x]=size[ch[x][]]+size[ch[x][]]+;
}
void rotate(int p)
{
int q=fa[p],y=fa[q],x=(p==ch[q][]);
ch[q][x]=ch[p][x^];fa[ch[q][x]]=q;
ch[p][x^]=q;fa[q]=p;
fa[p]=y;
if(y)
{
if(q==ch[y][])ch[y][]=p;
else ch[y][]=p;
}
push_up(q);push_up(p);
return ;
}
void splay(int x)
{
for(int y;y=fa[x];rotate(x))
{
if(fa[y])
{
if((ch[y][]==x&&ch[fa[y]][]==y)||(ch[y][]==x&&ch[fa[y]][]==y))rotate(y);
else rotate(x);
}
}
root=x;
}
int pre(int v)
{
int x=root;int tmp=-inf;
while(ch[x][k[x]<v])
{
if(k[x]<v)tmp=k[x];
x=ch[x][k[x]<v];
}if(k[x]<v)tmp=k[x];
return tmp;
}
int suc(int v)
{
int x=root;int tmp=inf;
while(ch[x][k[x]<=v])
{
if(k[x]>v)tmp=k[x];
x=ch[x][k[x]<=v];
}if(k[x]>v)tmp=k[x];
return tmp;
}
int find(int z)
{
int x=root;
if(k[x]==z)return x;
while(ch[x][k[x]<z])
{
x=ch[x][k[x]<z];
if(k[x]==z)return x;
}
return ;
}
void insert(int z)
{
int x=root;size[x]++;
while(ch[x][k[x]<z])x=ch[x][k[x]<z],size[x]++;
cnt++;ch[x][k[x]<z]=cnt;k[cnt]=z;size[cnt]=;fa[cnt]=x;splay(cnt);
}
void del(int x)
{
splay(x);
if(!ch[x][])
{
root=ch[x][];fa[ch[x][]]=;
}
else if(!ch[x][])
{
root=ch[x][];fa[ch[x][]]=;
}
else
{
fa[ch[x][]]=;int tmp=ch[x][];
while(ch[tmp][])tmp=ch[tmp][];
splay(tmp);ch[tmp][]=ch[x][];fa[ch[x][]]=tmp;
push_up(tmp);
}
return ;
}
int fd(int kk,int x)
{
int l=ch[kk][];int r=ch[kk][];
if(size[l]+==x)return k[kk];
if(size[l]>=x)return fd(l,x);
return fd(r,x-size[l]-);
}
int pr(int z)
{
int ans=;
int x=root;
while(x)
{
if(k[x]<=z)
{
ans+=size[ch[x][]]+;
x=ch[x][];
}
else x=ch[x][];
}
return ans;
}
void yu()
{
root=;k[]=inf;cnt=;size[]=;
insert(-inf);
}
int main()
{
yu();
int n;
scanf("%d",&n);
for(int o=;o<=n;o++)
{
int t1,t2;
scanf("%d%d",&t1,&t2);
if(t1==)
{
insert(t2);
}
else if(t1==)
{
int x1=find(t2);
if(x1!=)del(x1);
}
else if(t1==)
{
printf("%d\n",pr(t2-));
}
else if(t1==)
{
printf("%d\n",fd(root,t2+));
}
else if(t1==)
{
printf("%d\n",pre(t2));
}
else
{
printf("%d\n",suc(t2));
}
}
return ;
}
bzoj 3224 splay模板题4的更多相关文章
- bzoj 1588 splay模板题
用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...
- bzoj 3223 splay模板题3
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...
- bzoj 1208 splay模板题2
自己yy了找前驱和后继,学了学怎么删除...(反正就是练模板) #include<iostream> #include<cstdio> #include<cstring& ...
- BZOJ 3224 平衡树模板题
Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int n, ...
- 【BZOJ 3196】二逼平衡树 线段树套splay 模板题
我写的是线段树套splay,网上很多人写的都是套treap,然而本蒟蒻并不会treap 奉上sth神犇的模板: //bzoj3196 二逼平衡树,支持修改某个点的值,查询区间第k小值,查询区间某个值排 ...
- 【BZOJ 3188】【Coci 2011】Upit Splay模板题
转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...
- BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题
题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...
- BZOJ 1588 平衡树 模板题
Treap: //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int si ...
- PKU-3580 SuperMemo(Splay模板题)
SuperMemo 题目链接 Your friend, Jackson is invited to a TV show called SuperMemo in which the participan ...
随机推荐
- NLog 通过http保存日志
from:https://github.com/NLog/NLog/wiki/WebService-target Example config: <nlog throwExceptions='t ...
- Mysql数据库之Binlog日志使用总结
binlog二进制日志对于mysql数据库的重要性有多大,在此就不多说了.下面根据本人的日常操作经历,并结合网上参考资料,对binlog日志使用做一梳理: 一.binlog日志介绍1)什么是binlo ...
- iOS崩溃调试的使用和技巧总结
在iOS开发调试过程中以及上线之后,程序经常会出现崩溃的问题.简单的崩溃还好说,复杂的崩溃就需要我们通过解析Crash文件来分析了,解析Crash文件在iOS开发中是比较常见的. 现在网上有很多关于解 ...
- 临时表之IF-ELSE
1.解决输出单列到临时表 场景:存储过程传入id,id为缺省的过滤条件,如果id为0,则查找出tt表中的所有id作为过滤条件 目的:id不为0时,过滤id 解决:用case when来代替if els ...
- KeyBord事件从Activtiy层往下分发详细过程代码示例
step1:调用Activity成员函数dispatchKeyEvent public boolean dispatchKeyEvent(KeyEvent event) { // Let action ...
- swift 集合类型(一)
基本的数组结构Array: var shoppingList: String[] = ["Eggs", "Milk"] 这个shoppingList和传统意义上 ...
- Android项目,从web上取下汉字,中文部分乱码
Android项目,从web上取下汉字,中文部分乱码. 常见问题,搜索一下,网上有很多办法解决.如果还没有试过这个办法,可以尝试一下. BufferedReader in = new Buffered ...
- Core Web API上使用Swagger提供API文档
在ASP.NET Core Web API上使用Swagger提供API文档 我在开发自己的博客系统(http://daxnet.me)时,给自己的RESTful服务增加了基于Swagger的AP ...
- Linux 守护进程三
.打开telnet工具,登录服务器,登录校验成功以后, linux服务器会在终端和服务器之间,建立一个会话期session .在这个会话期中,默认启动一个shell程序 .在会话期中有n个进程组 sh ...
- sql中去除重复的项
方法一:group by (取最小的id)select min(id) id,T from Table_1 group by T 方法二:union (不需要id)select T from Tab ...