HDU 4006 The kth great number AVL Solution

Source: Internet
Author: User

HDU 4006 The kth great number AVL Solution

When dynamic data update is provided, what is the maximum K value in real time?

The AVL data structure is used for a relatively advanced data structure.

I don't know if the data value given by the question is repeated, because the program below can process repeated data.

The secret here is that the repeat information is added to know how many times the current array appears.

We mainly know how to maintain the data and how to query it. The data maintenance function is pushUp, and the query function is selectKth.

Others are general AVL operations. I personally think that my AVL writing is quite clear. For more information, see.


#include 
 
  inline int max(int a, int b) { return a > b? a : b; }inline int min(int a, int b) { return a < b? a : b; }struct Node{int key, h, size, repeat;//repeat for record the repeated key timesNode *left, *right;explicit Node(int val = 0) : left(NULL), right(NULL), key(val), repeat(1), h(1), size(1){}};inline int getHeight(Node *n){if (!n) return 0;return n->h;}inline int getSize(Node *n){if (!n) return 0;return n->size;}inline int getBalance(Node *n){if (!n) return 0;return getHeight(n->left) - getHeight(n->right);}inline void pushUp(Node *n){if (!n) return ;n->size = getSize(n->left) + getSize(n->right) + n->repeat;n->h = max(getHeight(n->left), getHeight(n->right)) + 1;}inline Node *leftRotate(Node *x){Node *y = x->right;x->right = y->left;y->left = x;pushUp(x);pushUp(y);return y;}inline Node *rightRotate(Node *x){Node *y = x->left;x->left = y->right;y->right = x;pushUp(x);pushUp(y);return y;}inline Node *balanceNode(Node *n){if (!n) return n;int balance = getBalance(n);if (balance > 1){if (getBalance(n->left) < 0) n->left = leftRotate(n->left);n = rightRotate(n);}else if (balance < -1){if (getBalance(n->right) > 0) n->right = rightRotate(n->right);n = leftRotate(n);}return n;}Node *insert(Node *rt, int val){if (!rt) return new Node(val);if (rt->key == val) rt->repeat++;else if (rt->key < val) rt->left = insert(rt->left, val);else rt->right = insert(rt->right, val);pushUp(rt);return balanceNode(rt);}int selectKth(Node *rt, int k){int lSize = getSize(rt->left);if (k <= lSize) return selectKth(rt->left, k);else if (lSize + rt->repeat < k)return selectKth(rt->right, k - lSize - rt->repeat);return rt->key; // lSize < k <= lSize+rt->repeat}void deleteTree(Node *rt){if (rt){if (rt->left) deleteTree(rt->left);if (rt->right) deleteTree(rt->right);delete rt; rt = NULL;}}int main(){int n, k, val;char c;while (scanf("%d %d", &n, &k) != EOF){Node *tree = NULL;for (int i = 0; i < n; i++){getchar();// get rid of '\n'c = getchar();if ('I' == c){scanf("%d", &val);tree = insert(tree, val);}else{printf("%d\n", selectKth(tree, k));}}deleteTree(tree);}return 0;}
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.