题目连接
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
红黑树:
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 #include<set> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::set; 14 using std::map; 15 using std::pair; 16 using std::vector; 17 using std::multiset; 18 using std::multimap; 19 #define all(c) (c).begin(), (c).end() 20 #define iter(c) decltype((c).begin()) 21 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 22 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 23 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 24 #define pb(e) push_back(e) 25 #define mp(a, b) make_pair(a, b) 26 const int Max_N = 200010; 27 typedef unsigned long long ull; 28 struct Node { 29 int data, s, client; 30 bool color; 31 Node *fa, *ch[2]; 32 inline void setc(int _v, int c, int i, bool _color, Node *ptr) { 33 data = _v, color = _color, s = i, client = c; 34 fa = ch[0] = ch[1] = ptr; 35 } 36 inline void push_up() { 37 s = ch[0]->s + ch[1]->s + 1; 38 } 39 inline void push_down() { 40 for (Node *x = this; x->s; x = x->fa) x->s--; 41 } 42 }; 43 struct RedBlackTree { 44 int top; 45 Node *root, *null; 46 Node stack[Max_N], *tail, *pool[Max_N]; 47 inline void init() { 48 top = 0; 49 tail = &stack[0]; 50 null = tail++; 51 null->setc(0, 0, 0, 0, NULL); 52 root = null; 53 } 54 inline Node *newNode(int v, int client) { 55 Node *x = !top ? tail++ : pool[--top]; 56 x->setc(v, client, 1, 1, null); 57 return x; 58 } 59 inline void rotate(Node* &x, bool d) { 60 Node *y = x->ch[!d]; 61 x->ch[!d] = y->ch[d]; 62 if (y->ch[d] != null) y->ch[d]->fa = x; 63 y->fa = x->fa; 64 if (x->fa == null) root = y; 65 else x->fa->ch[x->fa->ch[0] != x] = y; 66 y->ch[d] = x; 67 x->fa = y; 68 y->s = x->s; 69 x->push_up(); 70 } 71 inline void insert(int v, int client) { 72 Node *x = root, *y = null; 73 while (x->s) { 74 x->s++; 75 y = x, x = x->ch[v > x->data]; 76 } 77 x = newNode(v, client); 78 if (y != null) y->ch[v > y->data] = x; 79 else root = x; 80 x->fa = y; 81 insert_fix(x); 82 } 83 inline void insert_fix(Node* &x) { 84 while (x->fa->color) { 85 Node *par = x->fa, *Gp = par->fa; 86 bool d = par == Gp->ch[0]; 87 Node *uncle = Gp->ch[d]; 88 if (uncle->color) { 89 par->color = uncle->color = 0; 90 Gp->color = 1; 91 x = Gp; 92 } else if (x == par->ch[d]) { 93 rotate(x = par, !d); 94 } else { 95 Gp->color = 1; 96 par->color = 0; 97 rotate(Gp, d); 98 } 99 } 100 root->color = 0; 101 } 102 inline Node *find(Node *x, int data) { 103 while (x->s && x->data != data) x = x->ch[x->data < data]; 104 return x; 105 } 106 inline void erase_fix(Node* &x) { 107 while (x != root && !x->color) { 108 bool d = x == x->fa->ch[0]; 109 Node *par = x->fa, *sibling = par->ch[d]; 110 if (sibling->color) { 111 sibling->color = 0; 112 par->color = 1; 113 rotate(x->fa, !d); 114 sibling = par->ch[d]; 115 } else if (!sibling->ch[0]->color && !sibling->ch[1]->color) { 116 sibling->color = 1, x = par; 117 } else { 118 if (!sibling->ch[d]->color) { 119 sibling->ch[!d]->color = 0; 120 sibling->color = 1; 121 rotate(sibling, d); 122 sibling = par->ch[d]; 123 } 124 sibling->color = par->color; 125 sibling->ch[d]->color = par->color = 0; 126 rotate(par, !d); 127 break; 128 } 129 } 130 x->color = 0; 131 } 132 inline void erase(int data) { 133 Node *z = find(root, data); 134 if (!z->s) return; 135 Node *y = z, *x = null; 136 if (z->ch[0]->s && z->ch[1]->s) { 137 y = z->ch[1]; 138 while (y->ch[0]->s) y = y->ch[0]; 139 } 140 x = y->ch[!y->ch[0]->s]; 141 x->fa = y->fa; 142 if (!y->fa->s) root = x; 143 else y->fa->ch[y->fa->ch[1] == y] = x; 144 if (z != y) z->data = y->data, z->client = y->client; 145 y->fa->push_down(); 146 if (!y->color) erase_fix(x); 147 pool[top++] = y; 148 } 149 inline Node *kth(int k) { 150 int t = 0; 151 Node *x = root; 152 for (; x->s;){ 153 t = x->ch[0]->s; 154 if (k == t + 1) break; 155 else if (k <= t) x = x->ch[0]; 156 else k -= t + 1, x = x->ch[1]; 157 } 158 return x; 159 } 160 inline Node *operator[](int k) { 161 return kth(k); 162 } 163 inline void go(int n) { 164 int a, b; 165 Node *ret = null; 166 if (2 == n || 3 == n) { 167 if (2 == n && root->s) ret = rbt[root->s]; 168 else if (3 == n && root->s) ret = rbt[1]; 169 if (!ret->s) printf("0\n"); 170 else printf("%d\n", ret->client), erase(ret->data); 171 } else { 172 scanf("%d %d", &a, &b); 173 insert(b, a); 174 } 175 } 176 }rbt; 177 int main(){ 178 #ifdef LOCAL 179 freopen("in.txt", "r", stdin); 180 freopen("out.txt", "w+", stdout); 181 #endif 182 int n; 183 rbt.init(); 184 while (~scanf("%d", &n) && n) { 185 rbt.go(n); 186 } 187 return 0; 188 }
sb树:
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 #include<set> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::set; 14 using std::map; 15 using std::pair; 16 using std::vector; 17 using std::multiset; 18 using std::multimap; 19 #define all(c) (c).begin(), (c).end() 20 #define iter(c) decltype((c).begin()) 21 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 22 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 23 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 24 #define pb(e) push_back(e) 25 #define mp(a, b) make_pair(a, b) 26 const int Max_N = 200010; 27 typedef unsigned long long ull; 28 struct Node { 29 int v, p, s; 30 Node *ch[2]; 31 inline void setc(int _v, int _p, int _s, Node *ptr) { 32 v = _v, s = _s, p = _p; 33 ch[0] = ch[1] = ptr; 34 } 35 inline void push_up() { 36 s = ch[0]->s + ch[1]->s + 1; 37 } 38 inline int cmp(int x) const { 39 return p == x ? -1 : x > p; 40 } 41 }; 42 struct SBT { 43 int top; 44 Node *null, *root, *tail; 45 Node stack[Max_N], *pool[Max_N]; 46 inline void init() { 47 top = 0; 48 tail = &stack[0]; 49 null = tail++; 50 null->setc(0, 0, 0, NULL); 51 root = null; 52 } 53 inline Node *newNode(int v, int p) { 54 Node *x = !top ? tail++ : pool[--top]; 55 x->setc(v, p, 1, null); 56 return x; 57 } 58 inline void rotate(Node *&x, int d) { 59 Node *k = x->ch[!d]; x->ch[!d] = k->ch[d], k->ch[d] = x; 60 k->s = x->s; 61 x->push_up(); 62 x = k; 63 } 64 inline void Maintain(Node *&x, int d) { 65 if (!x->s) return; 66 if (x->ch[d]->ch[d]->s > x->ch[!d]->s) rotate(x, !d); 67 else if (x->ch[d]->ch[!d]->s > x->ch[!d]->s) rotate(x->ch[d], d), rotate(x, !d); 68 else return; 69 Maintain(x, 0), Maintain(x, 1); 70 } 71 inline void insert(Node *&x, int v, int p) { 72 if (!x->s) { x = newNode(v, p); return; } 73 x->s++; 74 int d = x->cmp(p); 75 insert(x->ch[d], v, p); 76 x->push_up(); 77 Maintain(x, d); 78 } 79 inline void erase(Node *&x, int p) { 80 if (!x->s) return; 81 x->s--; 82 int d = x->cmp(p); 83 if (-1 == d) { 84 if (!x->ch[0]->s || !x->ch[1]->s) { 85 pool[top++] = x; 86 x = x->ch[0]->s ? x->ch[0] : x->ch[1]; 87 } else { 88 Node *ret = x->ch[1]; 89 for (; ret->ch[0]->s; ret = ret->ch[0]); 90 x->v = ret->v, x->p = ret->p; 91 erase(x->ch[1], x->p); 92 } 93 } else { 94 erase(x->ch[d], p); 95 } 96 } 97 inline Node *kth(int k){ 98 int t = 0; 99 Node *x = root; 100 for (; x->s;){ 101 t = x->ch[0]->s; 102 if (k == t + 1) break; 103 else if (k <= t) x = x->ch[0]; 104 else k -= t + 1, x = x->ch[1]; 105 } 106 return x; 107 } 108 inline Node *operator[](int k) { 109 return kth(k); 110 } 111 inline void go(int n) { 112 int a, b; 113 Node *ret = null; 114 if (2 == n || 3 == n) { 115 if (2 == n && root->s) ret = sbt[root->s]; 116 else if (3 == n && root->s) ret = sbt[1]; 117 if (!ret->s) printf("0\n"); 118 else printf("%d\n", ret->v), erase(root, ret->p); 119 } else { 120 scanf("%d %d", &a, &b); 121 insert(root, a, b); 122 } 123 } 124 }sbt; 125 int main() { 126 #ifdef LOCAL 127 freopen("in.txt", "r", stdin); 128 freopen("out.txt", "w+", stdout); 129 #endif 130 int n; 131 sbt.init(); 132 while (~scanf("%d", &n) && n) { 133 sbt.go(n); 134 } 135 return 0; 136 }