代码说明

对于一些变量进行说明:

变量名 说明
rt 树根
ff[u] 点 \(u\) 的父节点,特别地, ff[rt]=0
ch[u][0|1] 点 \(u\) 的 左/右儿子
siz[u] 点 \(u\) 及其子树大小
val[u] 点 \(u\) 对应的值
recy[u] 点 \(u\) 对应的 val[u] 出现的次数

代码

#include<cstdio>
#include<vector>
using namespace std; #define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned #ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
} const int MAXN=1e5;
const int INF=0x3f3f3f3f; class Splay{
private:
int rt;
int ff[MAXN+5];
int ch[MAXN+5][2];
int siz[MAXN+5];
int recy[MAXN+5];
int val[MAXN+5];
vector<int>pool;
inline void release(const int x){
if(x)pool.push_back(x);
}
inline int generate(){
int ret=pool.back();
pool.pop_back();
return ret;
}
public:
Splay(){
rt=0;
rep(i,1,MAXN)pool.push_back(i);
}
inline void pushup(const int x){
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+recy[x];
}
inline void Rotate(const int x){
int y=ff[x],z=ff[y],k=(ch[y][1]==x);
ch[z][ch[z][1]==y]=x;
ff[x]=z;
ch[y][k]=ch[x][k^1];
ff[ch[x][k^1]]=y;
ch[x][k^1]=y;
ff[y]=x;
pushup(y),pushup(x);
//注意 上传 的先后顺序
}
/*
void Rotate(int x)//旋转
{
register int y=ff[x];
register int z=ff[y];
register int k=ch[y][1]==x;//x是y的左或右儿子
ch[z][ch[z][1]==y]=x;
ff[x]=z;
ch[y][k]=ch[x][k^1];
ff[ch[x][k^1]]=y;
ch[x][k^1]=y;
ff[y]=x;
pushup(y);pushup(x);
}
*/
inline void splay(const int x,const int goal=0){
int y,z;
while(ff[x]!=goal){
y=ff[x],z=ff[y];
if(z!=goal)
(ch[z][0]==y)^(ch[y][0]==x)?Rotate(x):Rotate(y);
Rotate(x);
}
if(goal==0)rt=x;
pushup(x);
}
inline void Insert(const int x){
int u=rt,fa=0;
while(u && val[u]!=x)fa=u,u=ch[u][val[u]<x];
if(u)++recy[u];
else{
u=generate();
if(fa)ch[fa][val[fa]<x]=u;
ff[u]=fa,val[u]=x;
ch[u][0]=ch[u][1]=0;
siz[u]=recy[u]=1;
}
splay(u);
}
inline void Find(const int x){
int u=rt;
if(!u)return;
while(x!=val[u] && ch[u][val[u]<x])u=ch[u][val[u]<x];
splay(u);
}
inline int Next(const int x,const int f){
Find(x);
int u=rt;
if((f && val[u]>x) || (!f && val[u]<x))return u;
for(u=ch[u][f];ch[u][f^1];u=ch[u][f^1]);
return u;
}
inline void Delete(const int x){
int pre=Next(x,0);
int suf=Next(x,1);
// printf("pre_val == %d, suf_val == %d\n",val[pre],val[suf]);
splay(pre);
splay(suf,pre);
int del=ch[suf][0];
if(recy[del]>1){
--recy[del];
splay(del);
}
else{
if(ch[suf][0])release(ch[suf][0]);
ch[suf][0]=0;
splay(suf);
//这里需要 splay 吗 ?
}
}
inline int Atrnk(int rnk){
int u=rt,y;
if(siz[u]<rnk)return -INF;
while(233){
y=ch[u][0];
if(rnk>siz[y]+recy[u]){
rnk-=siz[y]+recy[u];
u=ch[u][1];
}
else if(rnk<=siz[y])u=ch[u][0];
else return val[u];
}
}
inline int Getrnk(const int x){
Find(x);
return siz[ch[rt][0]]+1;
}
inline int Getpre(const int x){
int u=Next(x,0);
return val[u];
}
inline int Getsuf(const int x){
int u=Next(x,1);
return val[u];
}
void chk_tre(int u=-1){
if(u==-1)u=rt;
printf("Now u == %d, ff == %d, val == %d, siz == %d, lc == %d, rc == %d, recy == %d\n",u,ff[u],val[u],siz[u],ch[u][0],ch[u][1],recy[u]);
if(ch[u][0])chk_tre(ch[u][0]);
if(ch[u][1])chk_tre(ch[u][1]);
}
}Tre; int n,opt,x; signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
Tre.Insert(INF);
Tre.Insert(-INF);
// Tre.chk_tre();
// Endl;
qread(n);
while(n--){
qread(opt,x);
if(opt==1)Tre.Insert(x);
else if(opt==2)Tre.Delete(x);
else if(opt==3)writc(Tre.Getrnk(x)-1,'\n');
else if(opt==4)writc(Tre.Atrnk(x+1),'\n');
else if(opt==5)writc(Tre.Getpre(x),'\n');
else writc(Tre.Getsuf(x),'\n');
// Tre.chk_tre();
// Endl;
}
return 0;
}

扩展版

这个板子是针对有 pushdown()pushup() 操作的 Splay ,这里的模板题目是 POJ3580

#include<cstdio>
#include<vector>
using namespace std; #define rep(i,__l,__r) for(signed i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define FILEOI
// #define int long long
// #define int unsigned #ifdef FILEOI
# define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
# undef MAXBUFFERSIZE
# define cg (c=fgetc())
#else
# define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
} const int MAXN=1e5;
const int INF=0x3f3f3f3f; class Splay{
private:
struct node{
int ff,ch[2],siz,val,lag,delta,minn;
node(){ff=ch[0]=ch[1]=siz=val=lag=delta=minn=0;}
};
// node t[MAXN+5];
#define ch(x,i) t[x].ch[i]
vector<node>t;
int rt,ncnt,__siz; vector<int>pol;
#define SUBBUFFERSIZ 100005
inline void release(const int u){
if(u)pol.push_back(u);
}
inline void addBuffer(){
fep(i,__siz+SUBBUFFERSIZ,__siz+1)pol.push_back(i);
__siz+=SUBBUFFERSIZ;
}
inline int generate(){
if(pol.empty())addBuffer();
int ret=pol.back();
pol.pop_back();
if(ret>ncnt)++ncnt,t.push_back(node());
return ret;
}
public:
Splay(){
rt=ncnt=__siz=0;
addBuffer();
t.push_back(node());
t[0].minn=INF;//pay attention !
}
inline void pushup(const int x){
t[x].siz=t[ch(x,0)].siz+t[ch(x,1)].siz+1;
t[x].minn=Min(t[x].val,Min(t[ch(x,0)].minn,t[ch(x,1)].minn));
}
inline void add(const int u,const int delta){
if(!u)return;
t[u].val+=delta;
t[u].delta+=delta;
t[u].minn+=delta;
}
inline void revers(const int u){
if(!u)return;
swap(ch(u,0),ch(u,1));
t[u].lag^=1;
}
inline void pushdown(const int x){
if(t[x].delta){
add(ch(x,0),t[x].delta);
add(ch(x,1),t[x].delta);
t[x].delta=0;
}
if(t[x].lag){
revers(ch(x,0));
revers(ch(x,1));
t[x].lag=0;
}
}
inline void rotate(const int x){
const int y=t[x].ff,z=t[y].ff,k=(ch(y,1)==x);
pushdown(y),pushdown(x);
if(z)ch(z,ch(z,1)==y)=x;
t[x].ff=z;
ch(y,k)=ch(x,k^1);
t[ch(x,k^1)].ff=y;
ch(x,k^1)=y;
t[y].ff=x;
pushup(y),pushup(x);
}
inline void splay(const int x,const int goal=0){
int y,z;
pushdown(x);
//是否需要 pushdown ?
while(t[x].ff!=goal){
y=t[x].ff,z=t[y].ff;
if(z!=goal)
(ch(z,0)==y)^(ch(y,0)==x)?rotate(x):rotate(y);
rotate(x);
}
if(goal==0)rt=x;
pushup(x);
}
inline int rnkFind(int rnk,const int f=0){
int u=rt,y;
if(!u || t[u].siz<rnk)return 0;
while(233){
pushdown(u);
y=ch(u,0);
if(rnk>t[y].siz+1){
rnk-=t[y].siz+1;
u=ch(u,1);
}
else if(rnk<=t[y].siz)u=ch(u,0);
else return f?splay(u),u:u;
}
}
inline void rnkInsert(const int rnk,const int x){
rnkFind(rnk,1);
int u=rt,ins=generate(),ext=ch(rt,1); // printf("Now new node ins == %d, u == %d\n",ins,u); if(u)ch(u,1)=ins;
t[ins].ff=u;
ch(ins,0)=0,ch(ins,1)=ext;
if(ext)t[ext].ff=ins;
t[ins].siz=1,t[ins].val=t[ins].minn=x;
t[ins].lag=t[ins].delta=0; // printf("the new node's info :\n");
// printf("ins == %d, lc == %d, rc == %d, ff == %d, val == %d, siz == %d, delta == %d, lag == %d\n",ins,t[ins].ch[0],t[ins].ch[1],t[ins].ff,t[ins].val,t[ins].siz,t[ins].delta,t[ins].lag); splay(ins);
}
inline void rnkDelete(const int rnk){
int pre=rnkFind(rnk-1);
int suf=rnkFind(rnk+1);
splay(pre);
splay(suf,pre);
release(ch(suf,0));
ch(suf,0)=0;
splay(suf);
}
inline void Reverse(const int lrnk,const int rrnk){
int l=lrnk-1,r=rrnk+1;
int ln=rnkFind(l);
int rn=rnkFind(r);
splay(ln,0);
splay(rn,ln);
revers(ch(rn,0));
return;
}
inline void modify(const int lrnk,const int rrnk,const int delta){
int l=lrnk-1,r=rrnk+1;
int ln=rnkFind(l);
int rn=rnkFind(r);
splay(ln); // printf("After the first splay, the tree :\n");
// chk_tre();
// Endl; splay(rn,ln);
// printf("After the second splay, the tree :\n");
// chk_tre();
// Endl; // printf("modify : ln == %d, rn == %d\n",ln,rn);
// printf("add node %d, delta == %d\n",ch(rn,0),delta);
add(ch(rn,0),delta);
splay(rn);
return;
}
inline int Getminn(const int lrnk,const int rrnk){
int l=lrnk-1,r=rrnk+1;
int ln=rnkFind(l);
int rn=rnkFind(r);
splay(ln);
splay(rn,ln);
return t[ch(rn,0)].minn;
}
inline void Revolve(const int lrnk,const int rrnk,const int k){
Reverse(rrnk-k+1,rrnk);
Reverse(lrnk,rrnk-k);
Reverse(lrnk,rrnk);
}
inline int Atrnk(const int rnk){
// rnkFind(rnk,1);
int u=rnkFind(rnk);
return t[u].val;
}
inline void chk_tre(const int u=-1,int rnk=0){
// if(rnk>10)return; if(u==-1)return chk_tre(rt); if(ch(u,0))chk_tre(ch(u,0),rnk); rnk+=t[ch(u,0)].siz;
printf("rnk == %d\n",++rnk);
printf("Now u == %d, lc == %d, rc == %d, ff == %d, val == %d, siz == %d, delta == %d, lag == %d\n",u,t[u].ch[0],t[u].ch[1],t[u].ff,t[u].val,t[u].siz,t[u].delta,t[u].lag);
Endl; if(ch(u,1))chk_tre(ch(u,1),rnk);
}
inline void Writtre(const int u=-1){
if(u==-1)return Writtre(rt);
pushdown(u);
if(ch(u,0))Writtre(ch(u,0));
if(t[u].val!=-INF && t[u].val!=INF)
printf("%d ",t[u].val);
if(ch(u,1))Writtre(ch(u,1));
}
}tre; int n,m; signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
qread(n);
tre.rnkInsert(0,-INF);
rep(i,1,n)tre.rnkInsert(i,qread());
tre.rnkInsert(n+1,INF); // tre.chk_tre();
// Endl; qread(m); char opt[20];
int x,y;
while(m--){
scanf("%s",opt+1);
x=qread();
if(opt[1]=='D')tre.rnkDelete(x+1);
else y=qread();
if(opt[1]=='A')tre.modify(x+1,y+1,qread());
else if(opt[1]=='R' && opt[4]=='E')tre.Reverse(x+1,y+1);
else if(opt[1]=='R')tre.Revolve(x+1,y+1,qread()%(y-x+1));
else if(opt[1]=='I')tre.rnkInsert(x+1,y);
else if(opt[1]=='M')writc(tre.Getminn(x+1,y+1),'\n');
// puts("After this option, the tre : ");
// tre.chk_tre();
// tre.Writtre();
// Endl;
}
return 0;
}

「模板」Splay的更多相关文章

  1. 「模板」 FHQ_Treap

    「模板」 FHQ_Treap 我也是偶然发现我还没发过FHQ_Treap的板子. 那就发一波吧. 这个速度实在不算快,但是不用旋转,并且好写. 更重要的是,Splay 可以做的事情它都可以做!比如区间 ...

  2. 「模板」 线段树——区间乘 && 区间加 && 区间求和

    「模板」 线段树--区间乘 && 区间加 && 区间求和 原来的代码太恶心了,重贴一遍. #include <cstdio> int n,m; long l ...

  3. 「模板」 FHQ_Treap 区间翻转

    「模板」 FHQ_Treap 区间翻转 没有旋转的 Treap 实现区间操作的功能,很好理解,也很好写,只是速度不算太快. 对于要翻转的区间,把整棵 Treap(存有区间 \([1,n]\) 的信息) ...

  4. 「模板」 树链剖分 HLD

    「模板」 树链剖分 HLD 不懂OOP的OIer乱用OOP出人命了. 谨此纪念人生第一次类套类. 以及第一次OI相关代码打过200行. #include <algorithm> #incl ...

  5. 「模板」「讲解」Treap名次树

    Treap实现名次树 前言 学平衡树的过程可以说是相当艰难.浏览Blog的过程中看到大量指针版平衡树,不擅长指针操作的我已经接近崩溃.于是,我想着一定要写一篇非指针实现的Treap的Blog. 具体如 ...

  6. 「模板」AC自动机

    目录 说明 普通版本 询问更改版 拓扑优化版本 说明 这篇博客只挂模板,具体分析请膜拜大佬 hyfhaha 大佬. 普通版本 题目传送门 #include<cstdio> #include ...

  7. 「模板」可持久化 HFQ-Treap

    老师用的是静态数组的写法,开了很多数组- 其实个人更倾向于 struct 或者用 class 封装起来. 但是鉴于太难打 好吧,是我懒得打. 然后就借鉴了老师的模板,写出了属于自己的 压行 风格. 代 ...

  8. Solution -「LOJ #138」「模板」类欧几里得算法

    \(\mathcal{Description}\)   Link.   \(T\) 组询问,每次给出 \(n,a,b,c,k_1,k_2\),求 \[\sum_{x=0}^nx^{k_1}\left\ ...

  9. Solution -「LOJ #141」回文子串 ||「模板」双向 PAM

    \(\mathcal{Description}\)   Link.   给定字符串 \(s\),处理 \(q\) 次操作: 在 \(s\) 前添加字符串: 在 \(s\) 后添加字符串: 求 \(s\ ...

随机推荐

  1. centos 7 安装 Vue

    一.安装npmyum install -y npm 二.更新组件yum update openssl 三.安装Vue最新稳定版本npm install vue最新稳定 CSP 兼容版本npm inst ...

  2. UVA12716-连续区间内反向寻因子法

    在涉及的题目中如果需要使用连续区间内的数据的因数,可以放弃使用%这种低效的方案,从因数的角度进行,UVA12716中对于代码的优化就利用了这个小技巧. 原题:https://vjudge.net/pr ...

  3. "Chrome的network中无法显示OPTIONS请求"的解决方案

    目录 #事故现场 #分析及解决方法 #参考 #事故现场 在前端发送一个跨域请求的时候,要先发送个options请求,从而获知服务端是否允许该跨域请求. 跨域资源共享标准新增了一组 HTTP 首部字段, ...

  4. mysql 表中数据不存在则插入,否则更新数据

    在很多时候我们会操作数据库表,但是在向表中插入数据时,会遇到表中已经存在该id的数据或者没有该id的数据的情况,没有该id的数据的情况时直接插入就OK,遇到已经存在该id的数据的情况则更新该id的数据 ...

  5. Python的字典、列表合并

    字典合并: 在日常工作中需要对字典进行合并操作,下面提供几个操作方法 1.使用dict(a,**b) 例: >>> a={'a':1,'b':2} >>> b={' ...

  6. 3.Docker 操作镜像

    获取镜像 之前提到过,Docker Hub 上有大量的高质量的镜像可以用,这里我们就说一下怎么获取这些镜像. 从 Docker 镜像仓库获取镜像的命令是 docker pull.其命令格式为: doc ...

  7. spring boot no identifier specified for entity

    定义Id 时,引用的是 import org.springframework.data.annotation.Id;  实际应该引入: import javax.persistence.Id;

  8. Eclipse项目工程导入到IDEA继续开发-超详细

    现在Java开发的主流工具是IDEA,不是说Eclipse,各有各的特色.不过我现在深深的爱上了idea这个工具. 但是之前很多项目都是用eclipse开发的,现在就转入到idea中进行继续开发. 1 ...

  9. 浅谈radis

    1.概述 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API 从2010年3月15日起,Redis的开发工作由VM ...

  10. Eclipse的使用配置

    Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.目前许多开发者开发时仍会选择使用Eclipse,很多初学者刚开始接触Java也是从使用Eclipse开始的.本篇博客主要介绍Eclip ...