#include<iostream>
#include<cstdio>
#define cin(x) scanf("%d",&x)
using namespace std;
int ch[][],key[],
cnt[],size[],sz,rt,f[];
bool get(int x)
{
return ch[f[x]][]==x;
}
void clear(int x)
{
f[x]=cnt[x]=ch[x][]=ch[x][]=size[x]=key[x]=;
}
void pushup(int x)
{
if(x)
{
size[x]=cnt[x];
if(ch[x][])size[x]+=size[ch[x][]];
if(ch[x][])size[x]+=size[ch[x][]];
}
}
void rotate(int x)
{
int old=f[x],oldf=f[old],which=get(x);
ch[old][which]=ch[x][which^];f[ch[old][which]]=old;
ch[x][which^]=old;f[old]=x;
f[x]=oldf;
if(oldf)ch[oldf][ch[oldf][]==old]=x;
pushup(old),pushup(x);
}
void splay(int x)
{
for(int fa;fa=f[x];rotate(x))
if(f[fa])
rotate(get(x)==get(fa)?fa:x);
rt=x;
}
void insert(int x)
{
if(rt==)
{
sz++;key[sz]=x;rt=sz;
cnt[sz]=size[sz]=;
f[sz]=ch[sz][]=ch[sz][]=;
return;
}
int now=rt,fa=;
while()
{
if(x==key[now])
{
cnt[now]++;
pushup(now);
pushup(fa);
splay(now);
return;
}
fa=now;
now=ch[now][x>key[now]];
if(now==)
{
sz++;
size[sz]=cnt[sz]=;
ch[sz][]=ch[sz][]=;
ch[fa][x>key[fa]]=sz;
f[sz]=fa;
key[sz]=x;
pushup(fa);
splay(sz);
return;
}
}
}
int rnk(int x)
{
int now=rt,ans=;
while()
{
if(ch[now][] && x<key[now])now=ch[now][];
else
{
ans+=size[ch[now][]];
if(x==key[now])
{
splay(now);
return ans+;
}
ans+=cnt[now];
now=ch[now][];
}
}
}
int kth(int x)
{
int now=rt;
while()
{
if(ch[now][] && x<=size[ch[now][]])now=ch[now][];
else
{
int temp=size[ch[now][]]+cnt[now];
if(temp>=x)
return key[now];
x-=temp;now=ch[now][];
}
}
}
int pre()
{
int now=ch[rt][];
while(ch[now][])now=ch[now][];
return now;
}
int next()
{
int now=ch[rt][];
while(ch[now][])now=ch[now][];
return now;
}
void del(int x)
{
rnk(x);
if(cnt[rt]>){cnt[rt]--;pushup(rt);return;}
if(!ch[rt][] && !ch[rt][]){clear(rt);rt=;return;}
if(!ch[rt][])
{
int old=rt;rt=ch[rt][];f[rt]=;clear(old);return;
}
else if(!ch[rt][])
{
int old=rt;rt=ch[rt][];f[rt]=;clear(old);return;
}
int oldrt=rt,leftbig=pre();
splay(leftbig);//leftbig无儿子,所以oldrt无左二子
ch[rt][]=ch[oldrt][];
f[ch[oldrt][]]=rt;
clear(oldrt);
pushup(rt); }
signed main()
{
// freopen("input5.in","r",stdin); int n,opt,x;
cin(n);
while(n--)
{
cin(opt);cin(x);
if(opt==)insert(x);
if(opt==)del(x);
if(opt==)printf("%ld\n",rnk(x));
if(opt==)printf("%ld\n",kth(x));
if(opt==){insert(x);printf("%ld\n",key[pre()]);del(x);}
if(opt==){insert(x);printf("%ld\n",key[next()]);del(x);}
}
}

模板—splay的更多相关文章

  1. 算法模板——splay区间反转 2

    实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...

  2. 算法模板——splay区间反转 1

    实现的功能:将序列区间反转,并维护 详见BZOJ3223 var i,j,k,l,m,n,head,a1,a2:longint; s1:ansistring; a,b,c,d,fat,lef,rig: ...

  3. 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay

    题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...

  4. 洛谷 P3391 模板Splay

    #include<bits/stdc++.h> using namespace std; #define maxn 200000 int read() { ,w=; ;ch=getchar ...

  5. [模板] Splay

    欠了好久的Splay,以后就它了. 默写真不容易,过几天估计就忘了.. 整个Splay真的精妙,不拖泥带水那种.. 前驱后继之所以不能用rk转到根,是因为这个数不一定存在.. kth中<=老忘记 ...

  6. 模板——Splay

    $Splay$ #include <bits/stdc++.h> #define inf (int)1e9 using namespace std; const int N=1e5+100 ...

  7. Splay 伸展树

    废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...

  8. [NOI2003][bzoj1507] 文本编辑器 editor [splay]

    其实看明白了就是一道水题 毕竟模板 splay敲一发,插入一个串的时候先把它构建成一棵平衡树,再挂到原来的splay上面去即可 没别的了,上代码 #include<iostream> #i ...

  9. splay最终模板

    来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...

随机推荐

  1. MVC4中给TextBoxFor设置默认值和属性(同时设置js事件)

    例如:(特别注意在设置初始值的时候 Value 中的V要大写) @Html.TextBoxFor(model => model.CustomerCode, new { Value="  ...

  2. neo4j 张一鸣 8

    头条关注 粉丝关系 张一鸣 8

  3. Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决

    Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决 EmbeddedWBWebBrowserDelphi  最近用E ...

  4. LeetCode——Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. YTU 2530: 小勇玩lol

    2530: 小勇玩lol 时间限制: 1 Sec  内存限制: 128 MB 提交: 112  解决: 77 题目描述 小勇是一个忠实的lol玩家,他有自己的战斗力计算方法,每个星期他都会算一下自己的 ...

  6. luogu 4427 求和

    bjoi 2018 求和 唯一一道可能切的题一个数组还没开long long就成0分了 题目大意: 一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k次方和,而且每次的k可能是不同的 此处 ...

  7. 8 种提升ASP.NET Web API性能的方法

    ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能. 在本文中,我将介绍8项提高 ASP.NET Web ...

  8. Avito Code Challenge 2018 A~E

    A. Antipalindrome 还以为是什么神dp结果就是分情况讨论啊 原串是一串一样的字符的话输出0,是回文串的话输出n-1,否则直接输出原串长度 #include<iostream> ...

  9. bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】

    最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include< ...

  10. 10.11NOIP模拟题(2)

    /* string水过 */ #include<bits/stdc++.h> #define N 1001 using namespace std; int n,x,y,m,pre; st ...