Codeforces 38G Queue 伸展树
来源:互联网 发布:手机上怎么改淘宝评价 编辑:程序博客网 时间:2024/06/06 04:14
题目链接:点击打开链接
题意:
给定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].maxstruct 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);elseInsert(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;}/*21 02 131 32 33 352 31 44 33 15 211 044 12 21 33 244 12 21 33 144 12 21 33 041 02 13 24 341 02 14 23 351 02 14 23 35 261 02 14 23 35 26 472 23 45 17 26 51 04 1ans:1 4 2 31 2 4 31 2 3 44 3 2 13 4 2 13 4 5 2 13 6 4 5 2 12 4 5 3 1 7 6*/
0 0
- Codeforces 38G Queue 伸展树
- 【CodeForces】38G Queue 伸展树
- codeforces 38G Queue splay
- Codeforces Beta Round #38 G Queue 动态树
- hdu-3436-Queue-jumpers-伸展树
- hdu 4441 Queue Sequence(伸展树)
- hdu-3436 Queue-jumpers(伸展树)
- 【POJ3481】Double Queue——伸展树
- codeforces Gym 100431G Persistent Queue
- hdu 4441 Queue Sequence (伸展树splay+树状数组)
- POJ 3481 Double Queue 伸展树splay + 删除节点
- Hdu 5493 Queue【伸展树/二分+树状数组】
- 伸展树:双层伸展
- 伸展树
- 伸展树
- 伸展树
- 伸展树
- 伸展树
- 笔记71--OOM问题解决方案
- uva 11551 - Experienced Endeavour(矩阵快速幂)
- kmp算法
- 转换流
- .h头文件、 .lib库文件、 .dll动态链接库文件之间的关系
- Codeforces 38G Queue 伸展树
- 【java基础】[数组]
- Java进阶之欧拉工程 第十四篇【 最长的collatz序列】
- 数据结构:堆
- Adapter——notifyDataSetChanged
- uva 1546 - Complete the sequence!(牛顿插值法)
- 关于STM32中RTC的校准方法
- 【DataStrcutre】Introduction and description of Binary Trees
- HDOJ 题目2973 YAPTCHA(数学,威尔逊定理)