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的 ...
随机推荐
- .net Url重写
详细说明及下载dll源码路径: http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 顺带上本人写的一个小例子:http://files.cnbl ...
- TCP/IP详解学习笔记(4)-- ARP 和 RARP
1.ARP 地址解析协议(Address Resolution Protocol,ARP)是在仅知道主机的IP地址时确地址解析协议定其物理地址的一种协议. 在TCP/IP协议中,A ...
- TCP/IP详解学习笔记(14)-- TCP可靠传输的实现
1.概述 为方便描述可靠传输原理,假定数据传输只在一个方向上进行,即A发送数据,B给出确认 2.以字节为单位的滑动窗口 TCP的滑动窗口是以字节为单位的.为了便于说明,字节编号取得 ...
- .NET的三种缓存(页面缓存,控件缓存,自定义缓存)
BLL.Area bll = new BLL.Area(); protected void Page_Load(object sender, EventArgs e) { if (Cache[&quo ...
- cocos2dx 内存管理机制
持续更新吧. 刚开始看了一些. 一,CCObject 提供引用计数 1,unsinged int m_uReference; //此为CCOBject的引用计数,初始化为 1: new CCObje ...
- 如何初始化一个iOS原生地图
/** 初始化一个mapView 需导入 #import <MapKit/MapKit.h> - returns: 返回一个mapView对象 */ mapView = [[MKMapV ...
- jsp页面el表达式不起作用
web.xml中2.4版本的默认导入的standerd.jar,和jstl.jar是使用el表达式的包是启动的而2.5版本的web.xml中默认是关闭的所以在2.5的所有jsp中需要启动一下用< ...
- MySQL允许远程访问
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; flush privileges; ...
- thinkphp foreach循环生成二维数组的方法
先做个问题记录,另外下面是做的过程中遇到的一个没想明白的现象 foreach($result as $key => $val ){ $wzList[$key]['lik']=$val[0]; $ ...
- js 触发select onchange事件
select 或text的onchange事件需要手动(通过键盘输入)改变select或text的值才能触发,本文为大家介绍下使用js触发select onchange事件select 或text的o ...