hdu 4006/AvlTree
原题链接: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的更多相关文章
- hdu 4006 The kth great number (优先队列)
/********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...
- hdu 4006 The kth great number(优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 题目大意: 第一行 输入 n k,后有 n 行,对于每一行有两种状态 ,①“I x” : 插入 ...
- hdu 4006 The kth great number
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 思路:利用优先队列的性质,将数据存入后会自动对数据进行排序 #include<stdlib ...
- HDU 4006 优先队列
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- hdu 4006 优先队列 2011大连赛区网络赛F **
签到题都要想一会 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...
- HDU 4006 The kth great number(multiset(或者)优先队列)
题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相 ...
- HDU 4006 The kth great number【优先队列】
题意:输入n行,k,如果一行以I开头,那么插入x,如果以Q开头,则输出第k大的数 用优先队列来做,将队列的大小维护在k这么大,然后每次取队首元素就可以了 另外这个维护队列只有k个元素的时候需要注意一下 ...
- HDU --- 4006
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- HDU 4006 The kth great number AVL解
提供动态更新数据.第实时QK大量的值什么? 使用AVL统计数据结构做,比较先进的数据结构的内容. 不知道给出的数据为准值是否有反复.下面的程序是因为我能够处理重复数据出现的情况下,. 了repeat的 ...
随机推荐
- nginx 代理配置文件实例
安装NGINX前要先安装PCRE正则表达式库: ./configure --prefix=/usr/local/pcre 出现以下错误 (一般./configure即可, 笔者这里是直接./conf ...
- Select的深入应用(2)
首先创建两张表. 全连接 全连接:在检索时指定多个表,将每个表用逗号分隔,这样每个表的数据行都和其他表的每行交叉产生所有可能的组合,这样就是一个全连接.所有可能的组和数即每个表的行数的乘积. 或者: ...
- 洛谷P2725 邮票 Stamps
P2725 邮票 Stamps 37通过 224提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 为什么RE?在codevs上AC的. 题目背景 给一组 ...
- mac 下安装nginx
1,mac下的依赖: pcre-8.38.tar.gz nginx-1.4.7.tar.gz 2,解压pcre:进入器解压目录. EddydeMacBook-Pro:~ eddy$ cd /Users ...
- python字符串常用操作方法
python字符串操作常用操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下. 1.去除空格str.strip():删除字符串两边的指定字符,括号的写入指定字符,默 ...
- 如何初始化一个iOS原生地图
/** 初始化一个mapView 需导入 #import <MapKit/MapKit.h> - returns: 返回一个mapView对象 */ mapView = [[MKMapV ...
- 高效率的全组合算法(Java版实现)
博客上看到的一个算法,用Java实现了一个 算法描述: 算法说明:当n大于2时,n个数的全组合一共有(2^n)-1种. 当对n个元素进行全组合的时候,可以用一个n位的二进制数表示取法. 1表示在该位取 ...
- Deal with Android phones with pattern lock on
Yesterday my colleague asked me for help...She has two android phones , one is hTC and the other is ...
- 将Application按钮从任务栏中去掉
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
- 为什么swing不适合做桌面软件
http://www.zhihu.com/question/19608871 我最近几年做的项目清一色的都是HTML5了,这篇<基于HTML5的电信网管3D机房监控应用>供参考,HTML5 ...