【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其实就代表了树中的最多元素个数; 则最坏的情况 ...
随机推荐
- Google Map API 学习五
今天其实收货很大的 1.InfoWindow google.maps.InfoWindow class An overlay that looks like a bubble and is often ...
- ORACLE软件下载地址
Oracle Database 11g Release 2 Standard Edition and Enterprise Edition Software Downloads Oracle 数据库 ...
- dotfuscator使用方法
dotfuscator如何对.net程序进行混淆保护对于程序代码的保护,网上有很多资料,有的说混淆,有的说加密,我比较支持混淆的方法,这样可以让反编译劳工,头晕一阵子,哈哈开玩笑.对于加密如果不是不得 ...
- HDOJ1021题 Fibonacci Again 应用求模公式
Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...
- Spoj 7001 Visible Lattice Points 莫比乌斯,分块
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193 Visible Lattice Points Time L ...
- JavaScript 兼容处理IE67之 !"a"[0]
IE67对字符串进行取值需要使用charAt()方法,不能直接通过数组方式的坐标访问: <!DOCTYPE html> <html> <head> <meta ...
- ACM1228_STL的应用
#include<iostream> #include<string> #include<map> using namespace std; map<stri ...
- 宁波Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Web移动端Fixed布局的解决方案
移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况. 但是 fixed 元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题. 这篇文章里就提供一个简单的有 ...
- Ehcache简单说明及使用
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存 ...