原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006

这道题以前用c语言写的Avltree水过了。。

现在接触了c++重写一遍。。。

由于没有删除操作故不带垃圾回收,具体如下:

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#define Max_N 1000100
inline int max(int &a, int &b){
return a > b ? a : b;
}
struct Node *null;
struct Node{
int v, s, Height;
Node *ch[];
inline void
set(int H = , int _v = , int _s = , Node *p = NULL){
v = _v, s = _s, Height = H;
ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
int t1 = !ch[]->s ? - : ch[]->Height;
int t2 = !ch[]->s ? - : ch[]->Height;
Height = max(t1, t2) + ;
}
};
struct AvlTree{
Node *tail, *root;
Node stack[Max_N];
int top;
void init(){
tail = &stack[];
null = tail++;
null->set();
root = null;
}
Node *newNode(int v){
Node *p = tail++;
p->set(, v, , null);
return p;
}
inline void rotate(Node* &x, int d){
Node *k = x->ch[!d];
x->ch[!d] = k->ch[d];
k->ch[d] = x;
x->push_up();
k->push_up();
x = k;
}
inline void Maintain(Node* &x, int d){
if (x->ch[d]->Height - x->ch[!d]->Height == ){
if (x->ch[d]->ch[d]->Height - x->ch[d]->ch[!d]->Height == ){
rotate(x, !d);
} else if (x->ch[d]->ch[d]->Height - x->ch[d]->ch[!d]->Height == -){
rotate(x->ch[d], d), rotate(x, !d);
}
}
}
inline void insert(Node* &x, int v){
if (x == null){
x = newNode(v);
return;
} else {
int d = v > x->v;
insert(x->ch[d], v);
x->push_up();
Maintain(x, d);
}
}
inline int find_kth(Node *x, int k){
int t = ;
for (; x != null;){
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x->v;
}
inline void insert(int v){
insert(root, v);
}
inline void find_kth(int k){
printf("%d\n", find_kth(root, root->s - k + ));
}
}Avl;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
char ch;
int i, n, k, d;
while (~scanf("%d %d", &n, &k)){
Avl.init();
for (i = ; i < n; i++){
getchar();
scanf("%c", &ch);
if ('I' == ch) scanf("%d", &d), Avl.insert(d);
else Avl.find_kth(k);
}
}
return ;
}

hdu 4006/AvlTree的更多相关文章

  1. hdu 4006 The kth great number (优先队列)

    /********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...

  2. hdu 4006 The kth great number(优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 题目大意: 第一行 输入 n k,后有 n 行,对于每一行有两种状态 ,①“I x” : 插入 ...

  3. hdu 4006 The kth great number

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 思路:利用优先队列的性质,将数据存入后会自动对数据进行排序 #include<stdlib ...

  4. HDU 4006 优先队列

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  5. hdu 4006 优先队列 2011大连赛区网络赛F **

    签到题都要想一会 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...

  6. HDU 4006 The kth great number(multiset(或者)优先队列)

    题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相 ...

  7. HDU 4006 The kth great number【优先队列】

    题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...

  8. HDU --- 4006

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  9. HDU 4006 The kth great number AVL解

    提供动态更新数据.第实时QK大量的值什么? 使用AVL统计数据结构做,比较先进的数据结构的内容. 不知道给出的数据为准值是否有反复.下面的程序是因为我能够处理重复数据出现的情况下,. 了repeat的 ...

随机推荐

  1. [mysq]ERROR 2006 (HY000) at line xx: MySQL server has gone away 解决方法

    vi /etc/my.cnf wait_timeout=2880000interactive_timeout = 2880000max_allowed_packet = 100M 完整配置文件 [my ...

  2. 操作系统学习笔记 对称多处理(SMP)

    SMP:一种通过复用处理器提高程序执行并行性的方式. 根据SMP,计算机系统可以分为以下四类: 单指令单数据流(SISD):一个单处理器执行一个单指令流,对保存在一个存储器中的数据进程进行操作. 单指 ...

  3. javascript代码复用模式(三)

    前面谈到了javascript的类式继承.这篇继续部分类式继承,及一些现代继承. 类式继承模式-代理构造函数 这种模式通过断开父对象与子对象之间原型之间的直接链接关系,来解决上次说到的共享一个原型所带 ...

  4. KVM: 安装Windows virtio半虚拟化驱动

    Install KVM Windows virtio para-virtualized dirver If you can't read Chinese, there's an English ver ...

  5. rpm软件包

    安装软件:rpm -i software.rpm卸载软件:rpm -e software升级形式安装:rpm -U software-new.rpmrpm支持通过http.ftp协议安装软件:rpm ...

  6. POJ C++程序设计 编程作业—类和对象 编程题#1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序输出的结 ...

  7. CentOS 6.4安装AMH面板

    复制以下代码 然后执行 或者下载wget http://amysql.com/file/AMH/3.2/amh.sh; chmod 775 amh.sh; ./amh.sh 2>&1 | ...

  8. C#去除List中集合的重复项(类型对象和单一类型)

    去除重复类型对象BookInfo示例: bookList = bookList.Distinct(new DataRowComparer()).ToList(); //去除重复书籍 /// <s ...

  9. UML类图常见关系总结

    Unified Modeling Language (UML)又称统一建模语言或标准建模语言. 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realizat ...

  10. C#关于一个程序,只可以有一种实例的方法

    方法一:使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.把program.cs文件里的Main()函数改为如下代码: 说明:程序中通过语句 System.Threading.Mutex ru ...