用伸展树模拟插队比线段树快乐3倍。。

但是pojT了。别的oj可以过,直接贴代码.

每次更新时,找到第pos个人,splay到根,然后作为新root的左子树即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn 200005
using namespace std; int pre[maxn],ch[maxn][],num[maxn],key[maxn],size[maxn],tot,root;
void init(){
tot=root=;
pre[root]=ch[root][]=ch[root][]=num[root]=key[root]=size[root]=;
}
void newnode(int &r,int fa,int k,int val){
r=++tot;
ch[r][]=ch[r][]=;
num[r]=val;
key[r]=k;
pre[r]=fa;
size[root]=;
}
void pushup(int r){
size[r]=size[ch[r][]]+size[ch[r][]]+;
}
void rotate(int x,int kind){
int fa=pre[x];
ch[fa][!kind]=ch[x][kind];
pre[ch[x][kind]]=fa;
if(pre[fa])
ch[pre[fa]][ch[pre[fa]][]==fa]=x;
pre[x]=pre[fa];
pre[fa]=x;
ch[x][kind]=fa;
pushup(x);pushup(fa);
}
void splay(int r,int goal){
while(pre[r]!=goal){
if(pre[pre[r]]==goal) rotate(r,ch[pre[r]][]==r);
else {
int fa=pre[r];
int kind=ch[pre[fa]][]==fa;//????????
if(ch[fa][kind]==r){
rotate(r,!kind);
rotate(r,kind);
}
else {
rotate(fa,kind);
rotate(r,kind);
}
}
}
if(goal==) root=r;
pushup(r);pushup(root);
}
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d \n",x,ch[x][],ch[x][],pre[x],size[x],key[x]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
void Treval(int r){
if(r){
if(ch[r][]) Treval(ch[r][]);
printf("%d ",num[r]);
if(ch[r][]) Treval(ch[r][]);
}
}
void getth(int pos){
int r=root;
while(size[ch[r][]]+!=pos){
if(size[ch[r][]]+<pos)
pos-=size[ch[r][]]+,r=ch[r][];
else r=ch[r][];
}
splay(r,);
}
void insert(int pos,int val){//??
if(root==) {
newnode(root,,pos,val);
return;
}
else if(pos==){
int r=root;
while(ch[r][])
r=ch[r][];
newnode(ch[r][],r,pos,val);
splay(ch[r][],);
return;
}
else {
getth(pos);
int oldroot=root;
newnode(root,,pos,val);
ch[root][]=ch[oldroot][];
pre[ch[root][]]=root;
ch[oldroot][]=;
pushup(oldroot);
ch[root][]=oldroot;
pre[oldroot]=root;
pushup(root);
}
} int main(){
int n,pos,num;
while(scanf("%d",&n)==){
init();
for(int i=;i<=n;i++){
scanf("%d%d",&pos,&num);
insert(pos,num);
debug();
}
Treval(root);
puts("");
}
return ;
}

最新文章

  1. 拨开迷雾,找回自我:DDD 应对具体业务场景,Domain Model 到底如何设计?
  2. C# 访问MongoDB 通用方法类
  3. Python:python中math模块中提供的基本数学函数
  4. 应用 CSS3 动画实现12种风格的通知提示
  5. 点击某个按钮弹出 photoswip
  6. python(23)re函数:compile、match、search、findall
  7. HTML5实现音频播放
  8. 那些年我们没能bypass的xss filter
  9. Magento 中的多个类别的筛选产品集合
  10. boost::xml————又一次失败的尝试
  11. Jmeter对基于websocket协议的压力测试
  12. mouseout、mouseover和mouseleave、mouseenter区别
  13. freemarker报错之四
  14. Nginx+Tomca+Redis实现负载均衡、资源分离、session共享
  15. 函数多个返回值与unpack的用法
  16. su命令详解
  17. excel 八位二进制转换为十六进制公式
  18. spark生成大宽表的parquet性能优化
  19. 作业1+2.四则运算(改进后完整版,用python写的)_064121陶源
  20. Makefile 中符合的使用

热门文章

  1. 函数和常用模块【day06】:pickle模块(十二)
  2. vue props的理解
  3. java了解哪些锁
  4. contourf和contour用法区别
  5. MySQL锁解决并发问题详解
  6. readn.c
  7. 组合框QGroupBox
  8. NOIP2018TG题解
  9. vue pc端网站项目开发坑点与难度记录
  10. Spring源码学习资料