题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1908

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:

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 $10^6$, and a priority P is less than $10^7$. 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.

SampleInput

2
1 20 14
1 30 3
2
1 10 99
3
2
2
0

SampleOutput

0
20
30
10
0

红黑树:

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::set;
using std::map;
using std::pair;
using std::vector;
using std::multiset;
using std::multimap;
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
const int Max_N = ;
typedef unsigned long long ull;
struct Node {
int data, s, client;
bool color;
Node *fa, *ch[];
inline void setc(int _v, int c, int i, bool _color, Node *ptr) {
data = _v, color = _color, s = i, client = c;
fa = ch[] = ch[] = ptr;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline void push_down() {
for (Node *x = this; x->s; x = x->fa) x->s--;
}
};
struct RedBlackTree {
int top;
Node *root, *null;
Node stack[Max_N], *tail, *pool[Max_N];
inline void init() {
top = ;
tail = &stack[];
null = tail++;
null->setc(, , , , NULL);
root = null;
}
inline Node *newNode(int v, int client) {
Node *x = !top ? tail++ : pool[--top];
x->setc(v, client, , , null);
return x;
}
inline void rotate(Node* &x, bool d) {
Node *y = x->ch[!d];
x->ch[!d] = y->ch[d];
if (y->ch[d] != null) y->ch[d]->fa = x;
y->fa = x->fa;
if (x->fa == null) root = y;
else x->fa->ch[x->fa->ch[] != x] = y;
y->ch[d] = x;
x->fa = y;
y->s = x->s;
x->push_up();
}
inline void insert(int v, int client) {
Node *x = root, *y = null;
while (x->s) {
x->s++;
y = x, x = x->ch[v > x->data];
}
x = newNode(v, client);
if (y != null) y->ch[v > y->data] = x;
else root = x;
x->fa = y;
insert_fix(x);
}
inline void insert_fix(Node* &x) {
while (x->fa->color) {
Node *par = x->fa, *Gp = par->fa;
bool d = par == Gp->ch[];
Node *uncle = Gp->ch[d];
if (uncle->color) {
par->color = uncle->color = ;
Gp->color = ;
x = Gp;
} else if (x == par->ch[d]) {
rotate(x = par, !d);
} else {
Gp->color = ;
par->color = ;
rotate(Gp, d);
}
}
root->color = ;
}
inline Node *find(Node *x, int data) {
while (x->s && x->data != data) x = x->ch[x->data < data];
return x;
}
inline void erase_fix(Node* &x) {
while (x != root && !x->color) {
bool d = x == x->fa->ch[];
Node *par = x->fa, *sibling = par->ch[d];
if (sibling->color) {
sibling->color = ;
par->color = ;
rotate(x->fa, !d);
sibling = par->ch[d];
} else if (!sibling->ch[]->color && !sibling->ch[]->color) {
sibling->color = , x = par;
} else {
if (!sibling->ch[d]->color) {
sibling->ch[!d]->color = ;
sibling->color = ;
rotate(sibling, d);
sibling = par->ch[d];
}
sibling->color = par->color;
sibling->ch[d]->color = par->color = ;
rotate(par, !d);
break;
}
}
x->color = ;
}
inline void erase(int data) {
Node *z = find(root, data);
if (!z->s) return;
Node *y = z, *x = null;
if (z->ch[]->s && z->ch[]->s) {
y = z->ch[];
while (y->ch[]->s) y = y->ch[];
}
x = y->ch[!y->ch[]->s];
x->fa = y->fa;
if (!y->fa->s) root = x;
else y->fa->ch[y->fa->ch[] == y] = x;
if (z != y) z->data = y->data, z->client = y->client;
y->fa->push_down();
if (!y->color) erase_fix(x);
pool[top++] = y;
}
inline Node *kth(int k) {
int t = ;
Node *x = root;
for (; x->s;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline Node *operator[](int k) {
return kth(k);
}
inline void go(int n) {
int a, b;
Node *ret = null;
if ( == n || == n) {
if ( == n && root->s) ret = rbt[root->s];
else if ( == n && root->s) ret = rbt[];
if (!ret->s) printf("0\n");
else printf("%d\n", ret->client), erase(ret->data);
} else {
scanf("%d %d", &a, &b);
insert(b, a);
}
}
}rbt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
rbt.init();
while (~scanf("%d", &n) && n) {
rbt.go(n);
}
return ;
}

sb树:

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::set;
using std::map;
using std::pair;
using std::vector;
using std::multiset;
using std::multimap;
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
const int Max_N = ;
typedef unsigned long long ull;
struct Node {
int v, p, s;
Node *ch[];
inline void setc(int _v, int _p, int _s, Node *ptr) {
v = _v, s = _s, p = _p;
ch[] = ch[] = ptr;
}
inline void push_up() {
s = ch[]->s + ch[]->s + ;
}
inline int cmp(int x) const {
return p == x ? - : x > p;
}
};
struct SBT {
int top;
Node *null, *root, *tail;
Node stack[Max_N], *pool[Max_N];
inline void init() {
top = ;
tail = &stack[];
null = tail++;
null->setc(, , , NULL);
root = null;
}
inline Node *newNode(int v, int p) {
Node *x = !top ? tail++ : pool[--top];
x->setc(v, p, , null);
return x;
}
inline void rotate(Node *&x, int d) {
Node *k = x->ch[!d]; x->ch[!d] = k->ch[d], k->ch[d] = x;
k->s = x->s;
x->push_up();
x = k;
}
inline void Maintain(Node *&x, int d) {
if (!x->s) return;
if (x->ch[d]->ch[d]->s > x->ch[!d]->s) rotate(x, !d);
else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) rotate(x->ch[d], d), rotate(x, !d);
else return;
Maintain(x, ), Maintain(x, );
}
inline void insert(Node *&x, int v, int p) {
if (!x->s) { x = newNode(v, p); return; }
x->s++;
int d = x->cmp(p);
insert(x->ch[d], v, p);
x->push_up();
Maintain(x, d);
}
inline void erase(Node *&x, int p) {
if (!x->s) return;
x->s--;
int d = x->cmp(p);
if (- == d) {
if (!x->ch[]->s || !x->ch[]->s) {
pool[top++] = x;
x = x->ch[]->s ? x->ch[] : x->ch[];
} else {
Node *ret = x->ch[];
for (; ret->ch[]->s; ret = ret->ch[]);
x->v = ret->v, x->p = ret->p;
erase(x->ch[], x->p);
}
} else {
erase(x->ch[d], p);
}
}
inline Node *kth(int k){
int t = ;
Node *x = root;
for (; x->s;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline Node *operator[](int k) {
return kth(k);
}
inline void go(int n) {
int a, b;
Node *ret = null;
if ( == n || == n) {
if ( == n && root->s) ret = sbt[root->s];
else if ( == n && root->s) ret = sbt[];
if (!ret->s) printf("0\n");
else printf("%d\n", ret->v), erase(root, ret->p);
} else {
scanf("%d %d", &a, &b);
insert(root, a, b);
}
}
}sbt;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
sbt.init();
while (~scanf("%d", &n) && n) {
sbt.go(n);
}
return ;
}

hdu 1908 Double Queue的更多相关文章

  1. POJ 3481 &amp; HDU 1908 Double Queue (map运用)

    题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...

  2. HDU 1908 Double Queue(set)

    Problem Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in B ...

  3. 【HDOJ】1908 Double Queue

    双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...

  4. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  5. treap树---Double Queue

    HDU   1908 Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office i ...

  6. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  7. 【Map】Double Queue

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13258   Accepted: 5974 Des ...

  8. POJ 3481 Double Queue(STL)

    题意  模拟银行的排队系统  有三种操作  1-加入优先级为p 编号为k的人到队列  2-服务当前优先级最大的   3-服务当前优先级最小的  0-退出系统 能够用stl中的map   由于map本身 ...

  9. POJ 3481 Double Queue(set实现)

    Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...

随机推荐

  1. MVC session过期如何处理跳转

    以前我们总是会写一个基类也叫父类来判断session是否已过期然后跳转到指定的错误页面或者登陆界面,然后让所有的页面都继承这个基类,但是当我们应用到MVC项目中时,发现该方法并不会起作用.这时我们可以 ...

  2. The init method

    The init method is a special method that gets invoked when an object is instantiated. Its full name ...

  3. Ossim应用体验视频

    Ossim体验视频 近期,我写的有关Ossim应用的系列文章网友们非常关注,这里对大家提出有一些问题我制作了高清的视频和截图发布到网站,以让更多的人了解这款开源安全平台.在年后出版的教程中会详细讲解o ...

  4. div模仿select效果二:带搜索框

    项目需要,要做一个首字母快速定位的select,代码如下: HTML <div class="select_country" unselectable="on&qu ...

  5. axis2 部署webservice

    1.下载axis2-1.6.4-war.zip 2.将axis2.war防止webapps下. 3.http://localhost:8080/axis2/ 4.myeclipse创建web serv ...

  6. 模仿QQ空间 网页设计

    目的:1.通过模仿QQ空间,全自主写代码,熟悉网页设计的流程 2.熟练的掌握HTML.CSS.JS的应用 3.将在此过程中遇到的问题及其解决方法记录在此,以便取用. 开始: 一.登陆界面(index. ...

  7. Android 按键式事件

    1. package com.fish.helloworld; import android.app.Activity; import android.graphics.Color; import a ...

  8. 【转载】linux中互斥尽量用mutex,不用semaphore

    DEFINE_MUTEX是来自include/linux/mutex.h中的一个宏,用它可以定义一把互斥锁,在Linux内核中,其实是在2005年底才建立比较系统.完善的互斥锁机制,在那年冬天,来自R ...

  9. jLink V8调试exynos 4412 u-boot的几点补充

    /** ****************************************************************************** * @author    Maox ...

  10. Linux manual中命令标号的含义

    如果查看Linux manual(例如,执行:man open), 会发现文档中有这样的表达方式:read(2), write(2), lseek(2), fcntl(2)等,括号中的数值表达什么含义 ...