BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
题意简述
模板题啦~
代码
//普通平衡树(Splay)
#include <cstdio>
int const N=1e5+10;
int rt,ndCnt;
int ch[N][2],fa[N],val[N],siz[N],cnt[N];
int wh(int p) {return p==ch[fa[p]][1];}
void create(int p,int v) {ch[p][0]=ch[p][1]=fa[p]=0; val[p]=v; cnt[p]=1,siz[p]=1;}
void update(int p) {if(p) siz[p]=cnt[p]+siz[ch[p][0]]+siz[ch[p][1]]; else siz[p]=0;}
void rotate(int p)
{
int q=fa[p],r=fa[q],w=wh(p);
fa[ch[q][w]=ch[p][w^1]]=q;
fa[ch[p][w^1]=q]=p;
fa[p]=r; if(r) ch[r][q==ch[r][1]]=p;
update(p),update(q);
}
void splay(int p)
{
for(int q;q=fa[p];rotate(p)) if(fa[q]) rotate(wh(p)==wh(q)?q:p);
update(rt=p);
}
int find(int v)
{
int p=rt,res=0;
while(true)
if(val[p]>v) p=ch[p][0];
else
{
res+=siz[ch[p][0]];
if(val[p]==v) {splay(p); return res+1;}
res+=cnt[p],p=ch[p][1];
}
}
int rank(int x)
{
int p=rt;
while(true)
if(siz[ch[p][0]]>=x) p=ch[p][0];
else
{
int sizL=siz[ch[p][0]]+cnt[p];
if(sizL>=x) return val[p];
x-=sizL,p=ch[p][1];
}
}
int pre(int v)
{
int p=rt,res;
while(p)
if(val[p]<v) res=val[p],p=ch[p][1];
else p=ch[p][0];
return res;
}
int nxt(int v)
{
int p=rt,res;
while(p)
if(val[p]>v) res=val[p],p=ch[p][0];
else p=ch[p][1];
return res;
}
void ins(int v)
{
if(rt==0) {create(rt=++ndCnt,v); return;}
int p=rt,q=0;
while(true)
{
if(val[p]==v) {cnt[p]++; update(p),update(q); splay(p); break;}
q=p; p=ch[p][val[p]<v];
if(p==0)
{
create(++ndCnt,v);
fa[ch[q][val[q]<v]=ndCnt]=q;
update(q); splay(ndCnt);
break;
}
}
}
void del(int v)
{
int p=rt;
while(true)
if(val[p]==v) break;
else p=ch[p][val[p]<v];
splay(p);
if(cnt[p]>1) {cnt[p]--; return;}
int sCnt=(ch[p][0]>0)+(ch[p][1]>0);
if(sCnt==0) rt=0;
if(sCnt==1) fa[rt=ch[p][ch[p][0]==0]]=0;
if(sCnt==2)
{
int q=ch[p][0];
while(ch[q][1]) q=ch[q][1];
splay(q);
fa[ch[q][1]=ch[p][1]]=q;
update(q);
}
ch[p][0]=ch[p][1]=0; fa[p]=-1;
}
int main()
{
int q; scanf("%d",&q);
while(q--)
{
int opt,x;
scanf("%d%d",&opt,&x);
//printf("\n-------- %d %d Begin.\n",opt,x);
if(opt==1) ins(x);
if(opt==2) del(x);
if(opt==3) printf("%d\n",find(x));
if(opt==4) printf("%d\n",rank(x));
if(opt==5) printf("%d\n",pre(x));
if(opt==6) printf("%d\n",nxt(x));
//for(int i=1;i<=ndCnt;i++) printf("%d fa=%d son=%d,%d val=%d siz=%d cnt=%d\n",i,fa[i],ch[i][0],ch[i][1],val[i],siz[i],cnt[i]);
}
return 0;
}
Version2
//普通平衡树
#include <cstdio>
#include <algorithm>
using namespace std;
inline char gc()
{
static char now[1<<16],*S,*T;
if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
return *S++;
}
inline int read()
{
int x=0,f=1; char ch=gc();
while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=gc();}
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int const N=1e5+10;
int const INF=0x7FFFFFFF;
int rt,ndCnt;
int fa[N],ch[N][2],val[N],cnt[N],siz[N];
int wh(int p) {return p==ch[fa[p]][1];}
void update(int p) {siz[p]=siz[ch[p][0]]+cnt[p]+siz[ch[p][1]];}
void create(int p,int x) {fa[p]=ch[p][0]=ch[p][1]=0; val[p]=x,cnt[p]=siz[p]=1;}
void rotate(int p)
{
int q=fa[p],r=fa[q],w=wh(p);
fa[ch[q][w]=ch[p][w^1]]=q;
fa[p]=r; if(r) ch[r][wh(q)]=p;
fa[ch[p][w^1]=q]=p;
update(p),update(q);
}
void splay(int p,int &k)
{
int r=fa[k];
for(int q;(q=fa[p])!=r;rotate(p)) if(fa[q]!=r) rotate(wh(p)==wh(q)?q:p);
update(k=p);
}
void ins(int x)
{
int p=rt,q=0;
while(p&&val[p]!=x) q=p,p=ch[p][val[p]<x];
if(p) {splay(p,rt),cnt[rt]++,siz[rt]++; return;}
create(p=++ndCnt,x);
fa[p]=q; if(q) ch[q][val[q]<x]=p,update(q);
splay(p,rt);
}
void del(int x)
{
int p=rt; while(val[p]!=x) p=ch[p][val[p]<x];
splay(p,rt); if(cnt[p]>1) {cnt[rt]--,siz[rt]--; return;}
if(ch[p][0]*ch[p][1]==0) {fa[rt=ch[p][ch[p][1]>0]]=0; return;}
int q=ch[p][0]; while(ch[q][1]) q=ch[q][1];
splay(q,rt); fa[ch[rt][1]=ch[p][1]]=rt;
}
int find(int x)
{
int p,res=0;
for(p=rt;val[p]!=x;p=ch[p][val[p]<x])
if(val[p]<x) res+=siz[ch[p][0]]+cnt[p];
return res+siz[ch[p][0]]+1;
}
int rank(int x)
{
int p=rt,res=0;
while(true)
{
int sizL=siz[ch[p][0]]+cnt[p];
if(x<=siz[ch[p][0]]) p=ch[p][0];
else if(x<=sizL) return val[p];
else x-=sizL,p=ch[p][1];
}
}
int pre(int x)
{
int res=-INF;
for(int p=rt;p;p=ch[p][val[p]<x])
if(val[p]<x) res=max(res,val[p]);
return res;
}
int nxt(int x)
{
int res=INF;
for(int p=rt;p;p=ch[p][val[p]<=x])
if(val[p]>x) res=min(res,val[p]);
return res;
}
int main()
{
int T=read();
while(T--)
{
int opt=read(),x=read();
if(opt==1) ins(x);
else if(opt==2) del(x);
else if(opt==3) printf("%d\n",find(x));
else if(opt==4) printf("%d\n",rank(x));
else if(opt==5) printf("%d\n",pre(x));
else if(opt==6) printf("%d\n",nxt(x));
}
return 0;
}
BZOJ3224/洛谷P3391 - 普通平衡树(Splay)的更多相关文章
- [洛谷P3391] 文艺平衡树 (Splay模板)
初识splay 学splay有一段时间了,一直没写...... 本题是splay模板题,维护一个1~n的序列,支持区间翻转(比如1 2 3 4 5 6变成1 2 3 6 5 4),最后输出结果序列. ...
- 洛谷P3391文艺平衡树(Splay)
题目传送门 转载自https://www.cnblogs.com/yousiki/p/6147455.html,转载请注明出处 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 ...
- BZOJ3223/洛谷P3391 - 文艺平衡树
BZOJ链接 洛谷链接 题意 模板题啦~2 代码 //文艺平衡树 #include <cstdio> #include <algorithm> using namespace ...
- 洛谷 P3391 文艺平衡树
题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 --b ...
- 洛谷P3391 文艺平衡树 (Splay模板)
模板题. 注意标记即可,另外,涉及区间翻转操作,记得设立首尾哨兵. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int ...
- 洛谷 P3391 【模板】文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay
[阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...
- (treap)[bzoj3224][洛谷3369][cogs1829]Tyvj 1728 普通平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...
- 洛谷P3369普通平衡树(Treap)
题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...
随机推荐
- Python---多线程(threading)
1. 概述 threading提供线程相关操作,python当前版本的多线程库没有实现优先级.线程组,线程也不能被停止.暂停.恢复和中断 threading提供的类: Thread,Lock,Rloc ...
- 剑指offfer:二维数组中的查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成这样一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 例如: 1 2 ...
- freemarker常用值格式化方法
freemarker常用的值格式化方法: 1.${price?string('0.00')} 对price进行格式化,小数点后不足2位用0补足. 比如:price=1 输出:1.00 2.${pric ...
- 错误:This function has none of DETERMINISTIC... 的解决
问题: 在MySQL创建了一个批量插入的存储过程,在代码中调用的时候报错误信息: error code [1418];This function has none of DETERMINISTIC, ...
- android EditText设置
EditText输入的文字为电话号码 Android:phoneNumber=”true” //输入电话号码 //自动弹出键盘 ((InputMethodManager)getSystemServi ...
- grep使用技巧一:模式pattern为字符串文件
pattern文件: antc areq bdos bogt …… igs.txt文件: abmf 298.4725 16.2623 abpo 47.2292 -19 ...
- 八大排序算法Java实现
本文对常见的排序算法进行了总结. 常见排序算法如下: 直接插入排序 希尔排序 简单选择排序 堆排序 冒泡排序 快速排序 归并排序 基数排序 它们都属于内部排序,也就是只考虑数据量较小仅需要使用内存的排 ...
- mongodb监控常用方法
列举mongodb监控的常用命令 1.监控统计 mongostat 可用于查看当前QPS/内存使用/连接数,以及多个shard的压力分布 命令参考 ./mongostat --port 27071 - ...
- Jmeter3.1 使用技巧
一.JMeter官网 下载地址 http://jmeter.apache.org/download_jmeter.cgi Jmeter wiki https://wiki.apache.org/jme ...
- 使用document.execCommand复制内容至剪贴板
API https://developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand 兼容性 http://caniuse.com/#se ...