hdu 4006/AvlTree

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

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

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

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

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<iostream>
 4 #define Max_N 1000100
 5 inline int max(int &a, int &b){
 6     return a > b ? a : b;
 7 }
 8 struct Node *null;
 9 struct Node{
10     int v, s, Height;
11     Node *ch[2];
12     inline void
13     set(int H = 0, int _v = 0, int _s = 0, Node *p = NULL){
14         v = _v, s = _s, Height = H;
15         ch[0] = ch[1] = p;
16     }
17     inline void push_up(){
18         s = ch[0]->s + ch[1]->s + 1;
19         int t1 = ch[0] == null ? -1 : ch[0]->Height;
20         int t2 = ch[1] == null ? -1 : ch[1]->Height;
21         Height = max(t1, t2) + 1;
22     }
23 };
24 struct AvlTree{
25     Node *tail, *root;
26     Node stack[Max_N];
27     int top;
28     void init(){
29         tail = &stack[0];
30         null = tail++;
31         null->set();
32         root = null;
33     }
34     Node *newNode(int v){
35         Node *p = tail++;
36         p->set(0, v, 1, null);
37         return p;
38     }
39     inline void rotate(Node* &x, int d){
40         Node *k = x->ch[!d];
41         x->ch[!d] = k->ch[d];
42         k->ch[d] = x;
43         k->s = x->s;
44         x->push_up();
45         x = k;
46     }
47     void Maintain(Node* &x, int d){
48         if (x->ch[d]->Height - x->ch[!d]->Height == 2){
49             if (x->ch[d]->ch[d]->Height - x->ch[d]->ch[!d]->Height == 1){
50                 rotate(x, !d);
51             } else if (x->ch[d]->ch[d]->Height - x->ch[d]->ch[!d]->Height == -1){
52                 rotate(x->ch[d], d), rotate(x, !d);
53             }
54         }
55     }
56     void insert(Node* &x, int v){
57         if (x == null){
58             x = newNode(v);
59             return;
60         } else {
61             int d = v > x->v;
62             insert(x->ch[d], v);
63             x->push_up();
64             Maintain(x, d);
65         }
66     }
67     int find_kth(Node *x, int k){
68         int t = 0;
69         for (; x != null;){
70             t = x->ch[0]->s;
71             if (k == t + 1) break;
72             else if (k <= t) x = x->ch[0];
73             else k -= t + 1, x = x->ch[1];
74         }
75         return x->v;
76     }
77 }Avl;
78 int main(){
79 #ifdef LOCAL
80     freopen("in.txt", "r", stdin);
81     freopen("out.txt", "w+", stdout);
82 #endif
83     char ch;
84     int i, n, k, d;
85     while (~scanf("%d %d", &n, &k)){
86         Avl.init();
87         for (i = 0; i < n; i++){
88             getchar();
89             scanf("%c", &ch);
90             if (‘I‘ == ch)
91                 scanf("%d", &d), Avl.insert(Avl.root, d);
92             else
93                 printf("%d\n", Avl.find_kth(Avl.root, Avl.root->s - k + 1));
94         }
95     }
96     return 0;
97 }

时间: 2024-08-09 10:44:38

hdu 4006/AvlTree的相关文章

HDU 4006 The kth great number (基本算法-水题)

The kth great number Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too mu

HDU 4006 求第k大数 treap

裸题,瞬秒.. #include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> #include <vector> #include <set> #include <map> #include <queue> using namespace std; #define L(id) tree[id].ch[0] #defin

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006 The kth great number AVL解法

给出动态更新数据,实时问第K个大的数值是什么? 利用AVL数据结构做的一个统计数,比较高级的数据结构内容了. 不知道题目给出的数据值是否有重复,因为我下面的程序是可以处理出现数据重复的情况的. 其中的奥妙是增加了repeat的信息,可以知道出现了当前数组多少次. 主要是知道如何维护这些数据和如何查询,维护数据的函数是pushUp,查询函数是selectKth. 其他就是一般的AVL操作.个人觉得我的AVL写的还算蛮清晰的,有需要的参考一下. #include <stdio.h> inline

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

1 /********************************************************** 2 题目: The kth great number(HDU 4006) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=4006 4 算法: 优先队列 5 ************************************************************/ 6 #include<cstdio> 7 #

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

题目 询问第K大的数 //这是我最初的想法,用multiset,AC了——好吧,也许是数据弱也有可能 //multiset运用——不去重,边插入边排序 //iterator的运用,插入的时候,如果是相同的数没直接放在相同的现有的数据后面的 #include<cstdio> #include<cstring> #include<algorithm> #include<set> using namespace std; //#define IN freopen(

hdu 4006 The kth great number/SBT

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4006 这题原先用treap写过的,现在看了sb树..本来想练习一下sbt树重复元素的插入, 搞了半天跟以前写的treap方法完全不一样/(ㄒoㄒ)/~~, 照下面的写法重复的节点被完全插入到树中了,o(╯□╰)o.. 如果重复元素很多,那会浪费太多的空间,不知到有没啥好的方法... 还是再慢慢想想吧,T_T- #include<stdio.h> #include<stdlib.h> #

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

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6982    Accepted Submission(s): 2837 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

HDU 4006: The kth great number

The kth great number ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; while (cin >> n >> k) { char ch; int t; multiset<int>s; while (