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的 ...
随机推荐
- VS/Visual studio 源代码编辑器里的空处出现点号解决办法
此原因是不小心按错了键盘上的组合键Ctr+E+S, 再次按Ctr+E+S可消除.
- jQuery Mask
<script type="text/javascript" src="/assets/mask/jquery.mask.min.js"></ ...
- ASPxGridView的自动排序(写在onCustomUnboundColumnData()事件中)
//此排序写于后台,可打印出序号 protected void ASPxGridView_progoods_CustomUnboundColumnData(object sender, DevExpr ...
- Ax Grid 的显示根据用户的需求动态排序。
点击方向按钮上下移动记录. 设计思路. 以临时表TmpTable1举例. 在表中加一个real类型字段(eg:ColumnSeq)用于排序,给表建一个ColumnSeq字段的索引ColumnSeqId ...
- SQL Server 数据库设计
一.数据库设计的必要性 在实际的软件项目中,如果系统中需要存储的数据量比较大,需要设计的表比较多,表与表之间的关系比较复杂,那我们就需要进行规范的数据库设置.如果不经过数据库的设计,我们构建的数据库不 ...
- Groovy安装与入门实例
摘自: http://blog.csdn.net/dc_726/article/details/8576205 1 Groovy是什么? 来看下官网的介绍:http://groovy.codehaus ...
- pthread_cond_wait避免线程空转
多线程对同一块区域进行操作时,需要定义如下两种类型的变量: pthread_mutex_t xxx; pthread_cond_t yyy; pthread_mutex_t类型的变量,即锁,对公共区域 ...
- jQuery通过jquery.form.js插件使用AJAX提交Form表单
我简单使用了一下,jQuery Form插件有一下优点: 1.支持提交前验证. 2.支持提交后回调. 3.采用AJAX方式,有很好的用户体验 4.提交方式是灵活.只要指定要提交的form ID即可. ...
- CentOS 6.X版本升级PHP
#-----------------------------CentOS 6.X版本升级PHP------------------#! /bin/sh #1.关闭selinuxcp -rp /etc/ ...
- [Java]_函数传参的疑惑与思考
问题来源于leetcode上的两道题 Path Sum I && II,分别写了两个dfs. void dfs(TreeNode node , int sum , ArrayList& ...