【POJ3481】【splay】Double Queue
Description
The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with the lowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:
| 0 | The system needs to stop serving |
| 1 K P | Add client K to the waiting list with priority P |
| 2 | Serve the client with the highest priority and drop him or her from the waiting list |
| 3 | Serve the client with the lowest priority and drop him or her from the waiting list |
Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.
Input
Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.
Output
For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.
Sample Input
2
1 20 14
1 30 3
2
1 10 99
3
2
2
0
Sample Output
0
20
30
10
0
Source
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib> const int MAXN = + ;
const int INF = 0x7fffffff;
const int SIZE = ;
const int maxnode = + ;
using namespace std;
typedef int ll;
struct SPLAY{
struct Node{
int val, size;
int num;//num代表编号
Node *parent, *ch[]; Node(){
val = size = ;
num = ;
parent = ch[] = ch[] = NULL;
}
int cmp(){
if (parent == NULL) return -;
if (parent->ch[]->num == num) return ;
if (parent->ch[]->num == num) return ;
}
void update(){
size = ;
if (ch[] != NULL) size += ch[]->size;
if (ch[] != NULL) size += ch[]->size;
}
}*root, *nil, _nil, mem[];//我写内存池就没成功过 /*struct MEMPOOR{//内存池
queue< Node >Q;
Node mem[maxnode];
Node *nil; void init(Node *&t){
for (int i = 0; i < maxnode; i++) Q.push( mem[i] );
nil = t;
}
Node *get(){
Node *p = &(Q.front());
Q.pop();
p->parent = p->ch[0] = p->ch[1] = nil;
p->num = p->val = 0;
p->size = 1;
return p;
}
//回收内存
void push(Node *&p){
Q.push(*(p));
p->parent->ch[p->cmp()] = nil;
}
}poor;*/
int tot, cnt;//计数
/*void debug(){//测试内存池
poor.init();
Node *a = poor.get();
a->val = 10;
Node *b = poor.get();
b->val = 5;
a->ch[0] = b;
printf("%d\n", poor.Q.size());
printf("%d\n", a->val);
printf("%d\n", b->cmp());
}*/
Node *NEW(){
Node *p = &mem[cnt++];
p->parent = p->ch[] = p->ch[] = nil;
p->num = p->val = ;
p->size = ;
return p;
}
void init(){
//循环哨兵的定义
nil = &_nil;
_nil.parent = _nil.ch[] = _nil.ch[] = nil; tot = ;
cnt = ;
root = nil;
insert(root, -INF, -INF);
insert(root, INF, INF);
}
void rotate(Node *t, int d){
Node *p = t->parent;//t的右旋对于p来说也是右旋
t = p->ch[d ^ ];
p->ch[d ^ ] = t->ch[d];
t->ch[d]->parent = p;
t->ch[d] = p;
t->parent = p->parent;
//注意,这里要更新的原因在于t并不是引用
if (t->parent != nil){
if (t->parent->ch[] == p) t->parent->ch[] = t;
else if (t->parent->ch[] == p) t->parent->ch[] = t;
}
p->parent = t;
if (t->parent == nil) root = t;
//不用换回去了...
p->update();
t->update();
}
//将x旋转为y的子树
void splay(Node *x, Node *y){
while (x->parent != y){
if (x->parent->parent == y) rotate(x, (x->cmp() ^ ));
else{//不然连转两下
rotate(x->parent, x->parent->cmp() ^ );
rotate(x, x->cmp() ^ );
}
x->update();
}
}
//编号和权值
void insert(Node *&t, int num, int val){
if (t == nil){
t = NEW();
t->val = val;
t->num = num;
return;
}
Node *x = t;
while (){
int dir = (val > x->val);
if (x->ch[dir] == nil){
x->ch[dir] = NEW();
x->ch[dir]->val = val;
x->ch[dir]->num = num;
x->ch[dir]->parent = x;
splay(x->ch[dir], nil);
return;
}else x = x->ch[dir];
}
return;
}
void debug(){
/*init();
root = nil;
insert(root, 1, 1);
//printf("%d", root->val);
insert(root, 2, 2);
insert(root, 0, 0);
insert(root, 4, 4);
print(root);
//printf("%d\n", root->size); */
}
//找到val值为k的数的名次
int find(Node *t, int k){
if (t == nil) return -;//-1代表找不到
int tmp = t->ch[]->size;
if (k == t->val) return tmp + ;
else if (k < t->val) return find(t->ch[], k);
else return find(t->ch[], k) + tmp + ;
}
//找到第k小的权值并将其splay到根
void get(Node *t, Node *y, int k){
Node *x = t;
while (){
int tmp = x->ch[]->size;
if ((tmp + ) == k) break;
if (k <= tmp) x = x->ch[];
else {x = x->ch[]; k -= tmp + ;}
}
splay(x, y);
}
//删除val值为k的节点
void Delete(int k){
int tmp = find(root, k);
get(root, nil, tmp - );
get(root, root, tmp + );
//卡出来
//poor.push(root->ch[1]->ch[0]);
root->ch[]->ch[] = nil;
root->ch[]->update();
root->update();
return;
}
void print(Node *t){
if (t == nil) return;
print(t->ch[]);
printf("%d ", t->val);
print(t->ch[]);
} }A;
int n, m; void work(){
//A.root = nil;
A.init();//A.tot记录了A中的元素个数
int t;
while (scanf("%d", &t) && t){
if (t == ){
if (A.tot == ) {printf("0\n");continue;}
A.get(A.root, A.nil, A.tot + );
printf("%d\n", A.root->num);
A.Delete(A.root->val);
A.tot--;
}else if (t == ){
if (A.tot == ) {printf("0\n");continue;}
A.get(A.root, A.nil, );
printf("%d\n", A.root->num);
A.Delete(A.root->val);
A.tot--;
}else{
int val, num;
scanf("%d%d", &num, &val);
A.insert(A.root, num, val);
A.tot++;
}
}
} int main(){
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
work();
//A.debug();
return ;
}
【POJ3481】【splay】Double Queue的更多相关文章
- BZOJ4012 [HNOI2015]开店 【动态点分治 + splay】
题目链接 BZOJ4012 题解 Mychael并没有A掉,而是T掉了 讲讲主要思路 在点分树上每个点开两棵\(splay\), 平衡树\(A\)维护子树中各年龄到根的距离 平衡树\(B\)维护子树中 ...
- bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】
四舍五入就是个暴力. 对于线段树的每个区间都开一棵按权值排序的splay 对于第二个操作,二分一下,每次查询mid的排名,复杂度 $ O(nlog(n)^{3}) $ 其余的操作都是$ O(nlog( ...
- 【Map】Double Queue
Double Queue Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13258 Accepted: 5974 Des ...
- 【BZOJ1492】【NOI2007】货币兑换(动态规划,CDQ分治,Splay)
[BZOJ1492][NOI2007]货币兑换(动态规划,CDQ分治,Splay) 题面 BZOJ 洛谷 Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券 ...
- 【BZOJ1500】【NOI2005】维修数列(Splay)
[BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...
- 【BZOJ1058】【ZJOI2007】报表统计(链表,堆,Splay)
[BZOJ1058][ZJOI2007]报表统计 题面 题目描述 Q的妈妈是一个出纳,经常需要做一些统计报表的工作.今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一. 经过仔细观 ...
- 【hihocoder 1329】平衡树·Splay(Splay做法)
[题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 插入操作:-,记住每次插入之后都要把它放到根节点去就好; 询问操作:对于询问 ...
- 【DataStructure】Description and usage of queue
[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...
- 【洛谷】3960:列队【Splay】
P3960 列队 题目描述 Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为 ...
- 【hihocoder 1329】 平衡树·Splay(set做法)
[题目链接]:http://hihocoder.com/problemset/problem/1329 [题意] [题解] 因为一开始是空的树,所以; n其实就代表了树中的最多元素个数; 则最坏的情况 ...
随机推荐
- 前端程序员:月薪 5K 到 5 万,我干了啥
高贵的前端程序猿们: 如何在前端开发这种高精尖的技术领域找到心仪的工作?实现在咖啡馆喝喝咖啡敲敲代码就能升职加薪.买房买车.迎娶白富美走上人生巅峰的职业梦想?这篇<进化论:从 0 到 100,前 ...
- TMS320VC5509A DSP学习路线(持续更新)
step 1:芯片的数据手册(data sheet) 参考资料:sprs205k_TMS320VC5509A Fixed-Point Digital Signal Processor Data Man ...
- 青蛙的约会 - poj 1061(扩展欧几里得)
分析:这个东西在数论里面应该叫做不定方程式,可以搜一下,有很精彩的证明,先求出来方程式的一组特解,然后用这组特解来求通解,但是求出来特解之后怎么求这些解里面的最小非负x值?我们知道 x = x0 + ...
- Cookie 和Session
会话: 简单理解: 用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 会话要解决的问题: 每个用户与服务器进行交互的过程中,各自会有一些数据,程序 ...
- Ehcache专栏
http://www.iteye.com/blogs/subjects/ehcache
- ASP中Utf-8与Gb2312编码转换乱码问题的解决方法 页面编码声明
ASP程序在同一个站点中,如果有UTF-8编码的程序,又有GB2312编码的程序时,在浏览UTF-8编码的页面后,再浏览当前网站GB2312的页面,GB2312编码的页面就会出现乱码 出现这样的问题是 ...
- 洛谷 P1040 加分二叉树
题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...
- shell获取 linux 系统的位数
getconf LONG_BIT 直接返回 32 或者 64
- PNP8550(3.3V DC蜂鸣器) - 原理图系列
一.截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb2Jpbl9ITEo4MA==/font/5a6L5L2T/fontsize/400/fi ...
- 使用gulp在开发过程中合理导出zip文件
最近一段时间一直在做桌面混合应用,跟以往做web端不同的是,无法再通过在浏览器上输入内部的域名来随时跟踪开发版本的状况了,只能通过打包代码让产品或领导随时跟踪进度. 这里就涉及到一些问题了: 1,需要 ...