用伸展树模拟插队比线段树快乐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 ;
}

poj2828 伸展树模拟的更多相关文章

  1. hdu3436 splaytree树模拟队列+离散化缩点

    数据较大,需要先把每个top不会操作到的段缩成一个点,记录其开始和结束的位置,和top能操作到的点一起建立一颗伸展树模拟 然后就是普通的队列模拟操作 /* 不会被top操作到的区间就缩点 通过spla ...

  2. Hdu3487-Play with Chain(伸展树分裂合并)

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

  3. Poj3468-A Simple Problem with Integers(伸展树练练手)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  4. hdu4453-Looploop(伸展树)

    题目有很多图,不好粘贴..... 题意:给出N个数和K1,K2的值,最开始指针指向第一个数,有6种操作 add x : 给前K2个数都增加x reverse : 翻转前K1个数 insert x : ...

  5. 【BBST 之伸展树 (Splay Tree)】

    最近“hiho一下”出了平衡树专题,这周的Splay一直出现RE,应该删除操作指针没处理好,还没找出原因. 不过其他操作运行正常,尝试用它写了一道之前用set做的平衡树的题http://codefor ...

  6. BZOJ 1269 文本编辑器editor(伸展树)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1269 思路 伸展树(\(\text{splay}\))功能比较齐全的模板,能较好的体现 \( ...

  7. 伸展树的基本操作——以【NOI2004】郁闷的出纳员为例

    前两天老师讲了伸展树……虽然一个月以前自己就一直在看平衡树这一部分的书籍,也仔细地研读过伸展树地操作代码,但是就是没写过程序……(大概也是在平衡树的复杂操作和长代码面前望而生畏了)但是今天借着老师布置 ...

  8. HYSBZ 1500 维修数列(伸展树模板)

    题意: 题解:典型伸展树的题,比较全面. 我理解的伸展树: 1 伸展操作:就是旋转,因为我们只需保证二叉树中序遍历的结果不变,所以我们可以旋转来保持树的平衡,且旋转有左旋与右旋.通过这种方式保证不会让 ...

  9. 【bzoj1251】序列终结者(伸展树)

    [bzoj1251]序列终结者(伸展树) Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我 ...

随机推荐

  1. Kubernetes Ingress管理

    目录 Ingress介绍 1.Pod漂移问题 2.端口管理问题 3.域名分配及动态更新问题 Nginx Ingress配置 1.部署默认后端 2.部署Ingress Controller 3.部署In ...

  2. parted分区工具用法

    parted分区工具用法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 随着生产环境中数据量的增大,我们对硬盘的容量也有很大的需求,当硬盘的容量大于2T(工业上的最大磁盘2.2TB ...

  3. 多目标遗传算法 ------ NSGA-II (部分源码解析)二元锦标赛选择 tourselect.c

    tourselect.c  文件中共有两个函数: selection (population *old_pop, population *new_pop) individual* tournament ...

  4. C++面试集锦( 面试被问到的问题 )

    1. C 和 C++ 区别 2. const 有什么用途     主要有三点: 1:定义只读变量,即常量 2:修饰函数的参数和函数的返回值 3: 修饰函数的定义体,这里的函数为类的成员函数,被cons ...

  5. Git fetch & pull

    转:https://blog.csdn.net/qq_36113598/article/details/78906882 1.简单概括 先用一张图来理一下git fetch和git pull的概念: ...

  6. 2018牛客网暑期ACM多校训练营(第一场)D Two Graphs(图)

    题意 给两个图G1和G2,求G2的子图中与G1同构的数目. 分析 首先n=8,那么n!的算法问题不大.枚举G1的每个点,在G2中找同构的顶点序列.需要注意的是G1存在自同构的情况,所以对G1本身进行一 ...

  7. Codeforces 526D Om Nom and Necklace (KMP)

    http://codeforces.com/problemset/problem/526/D 题意 给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A ...

  8. JAVA中equals方法与hashCode方法学习

    首先参考文章:http://www.oschina.net/translate/working-with-hashcode-and-equals-methods-in-java 1,equals方法的 ...

  9. IPv4套接字地址结构

    一.IPv4套接字地址结构(POSIX定义) (1)长度字段sin_len是为增加对OSI协议的支持而随4.3BSD-Reno添加的:并不是所有的厂家都支持套接字地址结构的长度字段,而且POSIX规范 ...

  10. AAA游戏中雪的实现

    现在越来越多的AAA游戏中都实现了逼真的雪的效果,比如战神4.地平线:黎明时分.荒野大镖客:救赎2.古墓丽影:崛起等都实现了不错的雪地效果.今天我们就来探究一下他们的实现方式.现在主流的实现方式都是通 ...