Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=5349
MZL ' s simple problemdescription
A Simple problem
Problem Description
You has a multiple set,and now there is three kinds of operations:
1 x:add number x to set
2:delete the minimum number (if the set is empty now,then ignore it)
3:query the maximum number (if the set is an empty now,the answer is 0)
Input
The first line contains a number $N \ (N\leq 10^6) $,representing the number of operations.
Next $N $ line, each line contains one or both Numbers,describe one operation.
The number in this set was not greater than $10^9$.
Output
For each operation 3,output a line representing the answer.
Sample Input
8
1 2
1 3
1 4
2
2
3
2
3
Sample Output
4
0
Direct simulation can be, I use the balance tree.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <map>using std::map;using std::min;using std::find;using std::p air; Using std::vector;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 N = 1 << 20;const int INF = 0x3f3f3f3f;inline int read () {char c; int R; while (((c = GetChar ()) < ' 0 ' | | c > ' 9 ') && C ^ '-'); bool F = c = = '-'; if (f) r = 0; else R = C-' 0 '; while ((c = GetChar ()) >= ' 0 ' && C <= ' 9 ') (r *=) + = C-' 0 '; if (f) return-r; else return R;} struct Node {int V, S, c; Node *ch[2]; inline void Set (int _v, int _s, Node *p) { v = _v, s = c = _s; Ch[0] = ch[1] = p; } inline void Push_up () {s = ch[0]->s + Ch[1]->s + C; } inline int cmp (int x) const {return x = = v? -1:x > v; }};struct sizebalancetree {int top; Node *null, *root, *tail; Node Stack[n], *pool[n >> 1]; inline void init () {top = 0; Tail = &stack[0]; NULL = tail++; Null->set (0, 0, NULL); root = null; } inline Node *newnode (int v) {Node *x =!top? tail++: Pool[--top]; X->set (V, 1, 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, 0), maintain (x, 1); } inline void Insert (Node *&x, int v) {if (!x->s) {x = NewNode (v); return;} x->s++; int d = x->cmp (v); if ( -1 = = d) {x->c++; return;} Insert (X->ch[d], V); 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 ( -1 = = d) {if (X->c > 1) {x->c--; return;} else if (!x->ch[0]->s | |!x->ch[1]->s) {pool[top++] = x; x = X->ch[0]->s? X->ch[0]: x->ch[1]; } else {Node *ret = x->ch[1]; for (; ret->ch[0]->s; ret = ret->ch[0]); Erase (x->ch[1], x->v = ret->v); }} else {erase (x->ch[d], p); } if (X->s) X->pusH_up (); } inline void Insert (int v) {insert (root, v); } inline void Erase () {Erase (Root, kth (1)); } inline int kth (int k) {Node *x = root; for (int t = 0; x->s;) {T = x->ch[0]->s; if (k <= t) x = x->ch[0]; else if (t + 1 <= k && k <= T + x->c) break; else K-= t + x->c, x = x->ch[1]; } return x->v; } inline void query () {if (!root->s) {puts ("0"); return;} printf ("%d\n", KTH (Root->s)); }}sbt;int Main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif sbt.init (); int N, v, op; while (~SCANF ("%d", &n)) {sbt.init (); Rep (i, n) {op = read (); if (1 = = op) v = Read (), Sbt.insert (v); if (2 = = op) sbt.erase (); if (3 = = op) sbt.query (); }} return 0;}
Hdu 5349 MZL ' s simple problem