Codeforces 38G Queue 伸展树
题目链接:点击打开链接
题意:
给定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 伸展树的更多相关文章
- codeforces 38G - Queue splay伸展树
题目 https://codeforces.com/problemset/problem/38/G 题意: 一些人按顺序进入队列,每个人有两个属性,地位$A$和能力$C$ 每个人进入时都在队尾,并最多 ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- 【BBST 之伸展树 (Splay Tree)】
最近“hiho一下”出了平衡树专题,这周的Splay一直出现RE,应该删除操作指针没处理好,还没找出原因. 不过其他操作运行正常,尝试用它写了一道之前用set做的平衡树的题http://codefor ...
- POJ 3580 (伸展树)
题目链接: http://poj.org/problem?id=3580 题目大意:对一个序列进行以下六种操作.输出MIN操作的结果. 解题思路: 六个操作,完美诠释了伸展树有多么吊.注意,默认使用L ...
- 二叉查找树,AVL树,伸展树【CH4601普通平衡树】
最近数据结构刚好看到了伸展树,在想这个东西有什么应用,于是顺便学习一下. 二叉查找树(BST),对于树上的任意一个节点,节点的左子树上的关键字都小于这个节点的关键字,节点的右子树上的关键字都大于这个节 ...
- HYSBZ 1500 维修数列(伸展树模板)
题意: 题解:典型伸展树的题,比较全面. 我理解的伸展树: 1 伸展操作:就是旋转,因为我们只需保证二叉树中序遍历的结果不变,所以我们可以旋转来保持树的平衡,且旋转有左旋与右旋.通过这种方式保证不会让 ...
- wikioi 1396 伸展树(两个模板)
题目描写叙述 Description Tiger近期被公司升任为营业部经理.他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来 ...
- HDU 2475 Box 树型转线型 + 伸展树
树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...
随机推荐
- python 设计模式之代理模式
代理模式在一般形式上是一个类函数接口.代理可以是这些事物的接口:网络连接,存储的对象,文件,或者其他资源(昂贵的或者不容易复制的). 一个众所周知的代理模式的例子就是引用计数的指针对象. 代理模式是结 ...
- [POI2008]Mirror Trap
题目大意: 一个$n(n\le10^5)$个顶点的格点多边形,每条边平行于网格线,每个角度数均为$90^\circ$或$270^\circ$,周长小于$3\times10^5$,每个顶点可以安装激光发 ...
- linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql.
linux下安装php报错configure: error: Cannot find MySQL header files under /usr/include/mysql. 2013-03-04 1 ...
- AutoCAD中导入图片
导入图片涉及到两个关键的问题:如何将图片放置到指定的位置或范围内:如何修改图片的路径类型,如相对路径.绝对路径. 本文以AutoCAD 2018位演示截图来源. 1 将图片放置到指定的位置或范围内 ( ...
- 【iOS开发-55】图片轮播案例:scrollView的分页、滚动栏、利用代理控制定时器和Page Control以及多线程问题
案例: (1)用storyboard布局,这里用了三样东西. --UIScrollView就是我们准备存放滚动图片的容器. --Page Control就是控制页数的那几个小点点.能够设置有多少个点. ...
- Java源码阅读Stack
Stack(栈)实现了一个后进先出(LIFO)的数据结构.该类继承了Vector类,是通过调用父类Vector的方法实现基本操作的. Stack共有以下五个操作: put:将元素压入栈顶. pop:弹 ...
- Hive日期格式转换用法
如果想把 20180123 转换成 2018-01-23,可以使用: select from_unixtime(unix_timestamp('${p_date}','yyyymmdd'),'yyyy ...
- easyui datagrid 批量编辑和提交数据
easyui datagrid 行编辑和提交方,废话就不多说了,直接上代码 <div style="margin: 5px;"> <table id=" ...
- ubuntu18.04 安装mysql时'root'@'localhost'无法获取登录权限
查看一下user表,错误的起因就是在这里, root的plugin被修改成了auth_socket,用密码登陆的plugin应该是mysql_native_password mysql> sel ...
- JAVA Eclipse中的Android程序如何使用线程
我们先单独定义一个java类,名字可以任意取(比如叫做ClientHeartBeat类,我当前在做一个socket通信的客户端,我们假定需要一个可以测试心跳的程序),注意他要继承Thread,然后重载 ...