#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);}
}
}

最新文章

  1. jQuery选择器引擎和Sizzle介绍
  2. 个人js
  3. tiff或tif文件的读取
  4. hdwiki 框架简介
  5. 【MySQL】常见的mysql 进程state
  6. NSRange
  7. tableView点击后取消选中效果
  8. 对原生js的一些小尝试
  9. 如何配置和使用Tomcat访问日志
  10. 简话ASP.NET Web API
  11. ZooKeeper应用理论及其应用场景
  12. Angular - -ngKeydown/ngKeypress/ngKeyup 键盘事件和鼠标事件
  13. [.NET]使用十年股价对比各种序列化技术
  14. pycharm破解
  15. StringBuilder类
  16. cocos2d-x JS 纯代码实现人物头像裁剪
  17. python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出
  18. localforage 对不同浏览器 使用不同的缓存策略 , 大大提高了性能 ,IndexedDB,WebSQL 和 localStorage 三种存储模式
  19. Sorted方法排序用法
  20. iOS开发-搜索栏UISearchBar和UISearchController

热门文章

  1. struts &lt;s:iterator&gt;两个list嵌套循环,对象属性交叉使用
  2. SICP习题练习
  3. 呐喊-Skrik
  4. Linux/Android——Input系统之InputMapper 处理 (八)【转】
  5. BZOJ_4753_[Jsoi2016]最佳团体_树形背包+01分数规划
  6. [bzoj3073]Journeys
  7. oracle10G 数据库名、实例名、ORACLE_SID 及创建数据库- hl3292转载修改(实践部分待校验)
  8. android View页面布局总结 最全总结(转)
  9. Linux系统下 为命令配置别名
  10. poj 1201 Intervals【差分约束+spfa】