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的 ...
随机推荐
- HP Mobile Center 1.01 Related System Requirements
最近要开始使用HP Mobile Center,以下是我在官网上搜集的配置信息,包含软硬件. Reference: http://mobilecenterhelp.saas.hp.com/en/la ...
- BZOJ1996 合唱队 区间DP
OJ地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1996 设dp(i,j,k)代表在理想结果中[i,j]段最后添加的是i或j(k=0or1) ...
- Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表。
Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表,服务器需要安装如下的补丁,才可正常访问. http://www.microsoft.com ...
- 洛谷P1519 穿越栅栏 Overfencing
P1519 穿越栅栏 Overfencing 69通过 275提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 USACO是100分,洛谷是20分 为什么 ...
- docker学习(一)
atomic使用有点费劲,我改为centos7来做为学习环境. 1 安装 epel源就自带,目前版本是1.10.3 yum -y install docker docker version Clien ...
- 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client
在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client 阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...
- 安装package.js
- 移动端rem自适应布局关键代码
function resi() { var html = document.querySelector("html"); var wW = document.body.client ...
- DNS服务器:主要介绍DNS的服务原理以及安装及其主从配置
DNS服务器:主要介绍DNS的服务原理以及安装及其主从配置 一.DNS简介 1.DNS DNS是域名系统(Domain Name System)的简称,它是一个将域名和IP相互映射的分布式数据库 ...
- silverlight水印
1.自定义类 using System; using System.Net; using System.Windows; using System.Windows.Controls; using Sy ...