题目链接:点击打开链接

题意:

给定n个人来排队

每一个人有2个參数。身份优先级和脸皮厚度 ==

来的那个人会排到队尾

假设这个人的优先级比他前面那个人的优先级大就会和前面那个人交换位置。

交换一次脸皮厚度减1, 一直交换到队头或者脸皮厚度为0

交换完毕后下一个人才会到来。

问:

队伍最后的情况(从队头到队尾依次输出每一个人的编号)

思路:splay

维护子树的最小值。

插入时递归插入,若当前点是空就插这个位置。

然后就是裸的splay。。

==

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <queue>
#include <functional>
#include <map>
#include <iostream>
#include <set>
using namespace std;
typedef pair<int,int> pii;
#define ll int
#define inf 1000000
#define N 100005
#define L(x) tree[x].ch[0]
#define R(x) tree[x].ch[1]
#define Size(x) tree[x].siz
#define Val(x) tree[x].val
#define Father(x) tree[x].fa
#define Max(x) tree[x].max
struct node{
int ch[2], siz, fa;
int val, max; //伸展树里存的是队列的顺序
}tree[N];
int tot, root;
void Newnode(int &id, int fa, int val, int siz = 1){
node E={0,0,siz,fa,val,val};
id = tot++;
tree[id] = E;
}
void push_down(int id){}
void push_up(int id){
Size(id) = Size(L(id)) + Size(R(id)) + 1;
Max(id) = max( max(Max(L(id)), Max(R(id))), Val(id));
}
void Rotate(int id, int kind){
int y = Father(id);
push_down(y); push_down(id);
tree[y].ch[kind^1] = tree[id].ch[kind];
Father(tree[id].ch[kind]) = y;
if(Father(y))
tree[Father(y)].ch[R(Father(y))==y] = id;
Father(id) = Father(y);
Father(y) = id;
tree[id].ch[kind] = y;
push_up(y);
}
void Splay(int id, int goal){
push_down(id);
while(Father(id) != goal)
{
int y = Father(id);
if(Father(y) == goal)
Rotate(id, L(y) == id);
else
{
int kind = L(Father(y)) == y;
if(tree[y].ch[kind]==id)
{
Rotate(id, kind^1);
Rotate(id, kind);
}
else
{
Rotate(y, kind);
Rotate(id, kind);
}
}
}
push_up(id);
if(goal == 0)root = id;
}
void Insert(int &id, int val, int siz, int father){ //把值为val的点插到goal点后面
if(id == 0) {
Newnode(id, father, val);
return ;
}
if(val < Max(R(id)) || val < Val(id) || siz < Size(R(id)))
Insert(R(id), val, siz, id);
else
Insert(L(id), val, siz-Size(R(id))-1, id);
push_up(id);
} void init(){
//初始化0这个点
Father(0) = L(0) = R(0) = Size(0) = 0;
Val(0) = 0;
//默认1为最左端点
tot = 1;
Newnode(root, 0, inf);
Newnode(R(root), root, -1);
push_up(root);
}
map<int,int>mp; void put(ll id){
if(L(id))
put(L(id));
if(id > 2)
printf("%d ",mp[Val(id)]);
if(R(id))
put(R(id));
}
int main(){
ll i, u, v, n, id;
while(cin>>n){
mp.clear();
init();
for(i = 1; i <= n; i++) {
scanf("%d %d", &u, &v);
Insert(root, u, v, 0);
Splay(tot-1, 0);
mp[u] = i;
}
put(root); puts("");
}
return 0;
}
/*
2
1 0
2 1 3
1 3
2 3
3 3 5
2 3
1 4
4 3
3 1
5 2 1
1 0 4
4 1
2 2
1 3
3 2 4
4 1
2 2
1 3
3 1 4
4 1
2 2
1 3
3 0 4
1 0
2 1
3 2
4 3 4
1 0
2 1
4 2
3 3 5
1 0
2 1
4 2
3 3
5 2 6
1 0
2 1
4 2
3 3
5 2
6 4 7
2 2
3 4
5 1
7 2
6 5
1 0
4 1 ans:
1 4 2 3
1 2 4 3
1 2 3 4
4 3 2 1
3 4 2 1
3 4 5 2 1
3 6 4 5 2 1
2 4 5 3 1 7 6 */

Codeforces 38G Queue 伸展树的更多相关文章

  1. codeforces 38G - Queue splay伸展树

    题目 https://codeforces.com/problemset/problem/38/G 题意: 一些人按顺序进入队列,每个人有两个属性,地位$A$和能力$C$ 每个人进入时都在队尾,并最多 ...

  2. CodeForces 91B Queue (线段树,区间最值)

    http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...

  3. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

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

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

  5. POJ 3580 (伸展树)

    题目链接: http://poj.org/problem?id=3580 题目大意:对一个序列进行以下六种操作.输出MIN操作的结果. 解题思路: 六个操作,完美诠释了伸展树有多么吊.注意,默认使用L ...

  6. 二叉查找树,AVL树,伸展树【CH4601普通平衡树】

    最近数据结构刚好看到了伸展树,在想这个东西有什么应用,于是顺便学习一下. 二叉查找树(BST),对于树上的任意一个节点,节点的左子树上的关键字都小于这个节点的关键字,节点的右子树上的关键字都大于这个节 ...

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

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

  8. wikioi 1396 伸展树(两个模板)

    题目描写叙述 Description Tiger近期被公司升任为营业部经理.他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来 ...

  9. HDU 2475 Box 树型转线型 + 伸展树

    树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...

随机推荐

  1. shell spool

    If you wish to use SQL*Plus Command-line , you'll simply issue the sqlplus command from your shell: ...

  2. #请用索引取出下面list的指定元素:

    #!/usr/bin/python # -*- coding: utf-8 -*- L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python', ...

  3. 「Codeforces Round #441」 Classroom Watch

    Discription Eighth-grader Vova is on duty today in the class. After classes, he went into the office ...

  4. mysql语法语句

    将一个字段中的timestamp修改成可视化时间 update table set f1 = IF( LOCATE('-',f1)>0, f1, IFNULL(FROM_UNIXTIME(f1/ ...

  5. C++多重继承时调用相应的父类函数

    C++中没有super或parent关键字,想要调父类方法,只能使用明确的[父类名称::方法名] 假如要求A和B是C的父类的前提下,要使如下代码能够分别输出A和B的相关信息(虽然这个要求很少遇到... ...

  6. 如何评价 GitHub 发布的文本编辑器 Atom?

    这里是HN上的讨论:GitHub's new text editor leaked on Twitter这里是github page:Atom · GitHub 好多repo啊我不知道有没有知友了解更 ...

  7. django 用model来简化form

    django里面的model和form其实有很多地方有相同之处,django本身也支持用model来简化form 一般情况下,我们的form是这样的 from django import forms ...

  8. BindDepthStencilState

    nx sdk里面有这么一个接口 真坑 对于stencil fun op有两组值分别对应front back face 现在调用这个接口只能设置back 不能设置front跟了memory 有段全是0把 ...

  9. http://blog.csdn.net/i_bruce/article/details/39555417

    http://blog.csdn.net/i_bruce/article/details/39555417

  10. Mac eclipse安装SVN javaHL not available的解决方法

    在Mac下安装Eclipse插件svnEclipse插件后,每次打开Eclipse都会弹出如下弹出框: 提示你本机缺少JavaHL Library. 选择Eclipse→偏好设置(preference ...