题目连接

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

MZL's simple problem

Description

A simple problem
Problem Description
You have a multiple set,and now there are 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 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 two numbers,describe one operation.
The number in this set is 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

直接模拟即可,我用的平衡树。。

#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::pair;
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 *= 10) += 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的更多相关文章

  1. 2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem

    MZL's simple problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  2. mutiset HDOJ 5349 MZL's simple problem

    题目传送门 /* 这题可以用stl的mutiset容器方便求解,我对这东西不熟悉,TLE了几次,最后用读入外挂水过. 题解有O(n)的做法,还以为我是侥幸过的,后来才知道iterator it写在循环 ...

  3. hdoj 5349 MZL's simple problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5349 #include<stdio.h> int main(){ int cnt; int ...

  4. HDU 4267 A Simple Problem with Integers 多个树状数组

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

  6. hdu 3483 A Very Simple Problem

    两种构造的方式都是正确的: 1. #include<cstdio> #include<cstring> #include<algorithm> #define ma ...

  7. A simple problem 分类: 哈希 HDU 2015-08-06 08:06 1人阅读 评论(0) 收藏

    A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

  8. 【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  9. A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. redis(二)Redis适用场景,如何正确的使用

    网络IO模型 Memcached是多线程,非阻塞IO复用的网络模型,分为监听主线程和worker子线程,监听线程监听网络连接,接受请求后,将连接描述字pipe 传递给worker线程,进行读写IO, ...

  2. java.lang.NoSuchMethodException

    这个异常遇到过若干次,提示信息也比较清楚的指示出它的特点,当无法找到某一特定方法时,就会抛出该异常! 我所遇到的抛出此异常的情景主要有以下两种: 1:对应的JAVA类中没有对应的属性,也就是说在页面的 ...

  3. USACO Section 3.2 香甜的黄油 Sweet Butter

    本题是多源最短路问题 但使用弗洛伊德算法会超时 而因为边数目比较少 所以用队列优化后的迪杰斯特拉算法可以通过 #include<iostream> #include<cstring& ...

  4. Jsp,EL表达式的入门

    Jsp,EL表达式的入门 *Servlet/JSP 是两种动态的WEB资源的两种技术 使用Servlet生成HTML的页面是可以的 response.getWriter("<form ...

  5. MFC中release版本和debug版本区别

    最近MFC写了个程序,生成release版,原来正常,后来删掉了些控件再编译运行,结果竟然报内存读写错误,debug却是正常的.后来将“Project   Settings”   中   “C++/C ...

  6. MySQL初夜(乱码问题,命令行客户端使用)

    一.乱码问题 装好MySQL,并且将数据从SQLServer导入到MySQL之后,程序一直报错. 解决方案: 首先,输入命令: show variables like "character_ ...

  7. Volume serial number could associate file existence on certain volume

    When it comes to lnk file analysis, we should put more emphasis on the volume serial number. It coul ...

  8. 现代福尔摩斯 - Oxygen Forensic Suite

    各位可曾听说过智能手机取证软件Oxygen Forensic Suite,它的logo是名侦探福尔摩斯一手抽着他的招牌雪茄,一手拿着放大镜,全神贯注地正进行调查工作. 使用过它的取证人员必定会对它的提 ...

  9. leetcode 1

    题目: 最开始采用暴力解法,两个for循环遍历所有组合形式,时间复杂度为O(n2),代码省略. 进一步学习,采用hash表存储,空间换时间,时间复杂度为O(n),空间复杂度为O(n); 将数组放入ha ...

  10. SqlServer 事务和异常处理示例

    BEGIN TRANSACTION--开始事务 DECLARE @errorSun INT --定义错误计数器SET @errorSun=0 --没错为0 UPDATE dbo.Test SET te ...