用伸展树模拟插队比线段树快乐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. python Elasticsearch5.x使用

    文档:http://elasticsearch-py.readthedocs.io/en/master/ Elasticsearch官方API文档:https://www.elastic.co/gui ...

  2. Meavn项目中log4j的使用

    两个步骤: 1.在pom.xml中添加: <dependency> <groupId>log4j</groupId> <artifactId>log4j ...

  3. Nginx核心配置文件常用参数详解

    Nginx核心配置文件常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 关于Nginx权威文档的话童鞋们可以参考Nginx官方文档介绍:http://nginx.org/ ...

  4. 函数和常用模块【day04】:内置函数(九)

    一.11-20 11.ord(c) 功能:根据字符,找到对应的ascii值 1 2 >>> ord('a') 97 12.classmethod(function) 功能:类方法,这 ...

  5. 百度编辑器 Ueditor 如何增加模板 ?

    模板文件在这里: dialogs/template/config.js 参见:http://t.mreald.com/191 .

  6. winform,WPF 释放内存垃圾,减少资源占用方法

    [System.Runtime.InteropServices.DllImport("kernel32.dll")]        public static extern boo ...

  7. Emacs org-mode导出html出错

    不知道为什么,当org文件中含有#+TITLE:xxx时,导出会报类似下面的错误: Wrong type argument: listp, #("xxx" 0 3 (:parent ...

  8. 开始学习tensorflow

    搭建神经网络: 准备数据 定义数据输入层 定义网络隐藏层和预测层 定义loss 表达式 选择optimizer使得loss 最小 import tensorflow as tf import nump ...

  9. Java的三种代理模式:静态代理/JDK动态代理/Cglib动态代理

    1.静态代理:需要定义接口或者父类,目标对象与代理对象均实现同一接口或继承同一父类. 2.JDK动态代理:需要目标对象实现一个接口,通过动态反射的机制,生成代理对象,实现同一个接口 3.Cglib动态 ...

  10. SSM框架报错分析(一)——There is no getter for property named 'XXX' in 'class java.lang.String'

    一.发现问题 <select id="queryStudentByNum" resultType="student" parameterType=&quo ...