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 ...
随机推荐
- Xamarin XAML语言教程使用方法设置进度条进度
Xamarin XAML语言教程使用方法设置进度条进度 在ProgressBar中定义了一个ProgressTo方法,此方法也可以用来对进度条当前的进行进行设置,ProgressTo与Progress ...
- My first blog on cnBlogs!
以后会长期更新自己的心得体会!以此锻炼自己,奋发向前.
- JQuery插件开发格式
原地址 一.jQuery扩展 1.$.extend(object) 类似于.Net的扩展方法,用于扩展jQuery.然后就可以用$.的方式调用. $(function(){ $.extend({ fu ...
- 永久关闭WPS热点
可以通过设置WPS,关闭广告推送和热点即可,方法如下 准备:打开已经安装wps的设备 1.单击电脑左下角,找到wps 2.选中WPS Office中的配置工具 3.在弹窗中选择“高级” 4.打开之后选 ...
- Kubernentes中的身份验证
Kubernentes中的身份验证 kubernetes 系统的各组件需要使用 TLS 证书对通信进行加密,本文档使用 CloudFlare 的 PKI 工具集 cfssl 来生成 Certifica ...
- 通过CVE-2017-17215学习路由器漏洞分析,从入坑到放弃
1.基本信息: 2017/11/27,Check Point 软件技术部门报告了一个华为 HG532 产品的远程命令执行漏洞(CVE-2017-17215),Mirai的升级版变种中已经使用该漏洞.看 ...
- python 常用的模块(collections)转
collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: >>> ...
- redhat6.4 install 163 source
1) 到http://mirrors.163.com的 centos帮助文档 中下载CentOS6-Base-163.repo文件,存放到/etc/yum.repo.d中 wget http://mi ...
- Hadoop之HDFS详解
1.HDFS的概念和特性 它是一个文件系统,其次是分布式的 重要特性: 1).HDFS中的文件在物理上是分块存储(block),新版默认128M 2).客户端通过路径来访问文件,形如:hdfs://n ...
- KodExplorer介绍
KodExplorer介绍 KOD·简介 官方网站https://kodcloud.com/ KodExplorer可道云,原名芒果云,是一款基于 PHP 开发的开源 WEB 网页版轻量级私有云和在线 ...