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 ...
随机推荐
- BZOJ 2190: [SDOI2008]仪仗队
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2689 Solved: 1713[Submit][Statu ...
- Delphi常用系统函数总结
Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...
- 【BZOJ 1001】[BeiJing2006]狼抓兔子
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...
- BZOJ 4003 【JLOI2015】城池攻占
Description 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池. 这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖, ...
- springmvc 通过异常增强返回给客户端统一格式
在springmvc开发中,我们经常遇到这样的问题:逻辑正常执行时返回客户端指定格式的数据,比如json,但是遇NullPointerException空指针异常,NoSuchMethodExcept ...
- bootstrap-datepicker带中文的js文件
) { $(".datepicker").datepicker({ language: "zh-CN", autoclose: true,//选中之后自动隐藏日 ...
- 完整的社交app源码android+laravel
等想到写点什么的时候再写吧,其他看代码. https://github.com/huijimuhe/monolog-android https://github.com/huijimuhe/monol ...
- ASP.NET MVC3入门教程之第一个WEB应用程序
本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=91&extra=page%3D1 上一节,我们已经搭建好了AS ...
- 2014 todo list
1. 做好已知的各种项目,争取能成立固定团队2. 横向扩展技术学习,了解各种技术,加强技术素养3. 争取找个妹子4. 加强音律学习5. 继续发展各业余爱好6. 努力摇号 年底看收成.
- 匈牙利算法(codevs2776)
type node=^link; link=record des:longint; next:node; end; var n,m,i,t,num:longint; p:node; nd:..] of ...