Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4006
Question:
Given N operations, there are two types of operations: insert and query the k-th largest number. N <= 1000000
Solution: Because this question has not been deleted, it becomes very simple. You only need to maintain a small top heap with a size of K.
However, if there is a delete operation, it becomes more complicated. So I wrote this question using SBT, and I wrote it twice in anger. I wrote it three more times tomorrow evening at half past ten, gold, there is nothing to say about the test template.
Test data:
8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q
C producer code:
#include <stdio.h>#include <string.h>#define MAX 1000010int n,m;struct SBT {int left,right,size,key;void Init() {left = right = 0;size = 1;}}a[MAX];int tot,root;void left_rotate(int &t) {int k = a[t].right;a[t].right = a[k].left;a[k].left = t;a[k].size = a[t].size;a[t].size = a[a[t].left].size + a[a[t].right].size + 1;t = k;}void right_rotate(int &t) {int k = a[t].left;a[t].left = a[k].right;a[k].right = t;a[k].size = a[t].size;a[t].size = a[a[t].left].size + a[a[t].right].size + 1;t = k;}void maintain(int &t,int flag) {if (flag == 0) {if (a[a[a[t].left].left].size > a[a[t].right].size) right_rotate(t);else if (a[a[a[t].left].right].size > a[a[t].right].size)left_rotate(a[t].left),right_rotate(t);else return;}else {if (a[a[a[t].right].right].size > a[a[t].left].size)left_rotate(t);else if (a[a[a[t].right].left].size > a[a[t].left].size)right_rotate(a[t].right),left_rotate(t);else return;}maintain(a[t].left,0);maintain(a[t].right,1);maintain(t,0);maintain(t,1);}void insert(int &t,int v) {if (t == 0)t = ++tot,a[t].Init(),a[t].key = v;else {a[t].size++;if (v < a[t].key)insert(a[t].left,v);else insert(a[t].right,v);maintain(t,v>=a[t].key);}}int del(int &t,int v) {if (!t) return 0;a[t].size--;if (v == a[t].key || v < a[t].key && !a[t].left|| v > a[t].key && !a[t].right) {if (a[t].left && a[t].right) {int p = del(a[t].left,v+1);a[t].key = a[p].key;return p;}else {int p = t;t = a[t].left + a[t].right;return p;}}else return del(v<a[t].key?a[t].left:a[t].right,v);}int find(int t,int k) {if (k <= a[a[t].left].size)return find(a[t].left,k);if (k > a[a[t].left].size + 1)return find(a[t].right,k-a[a[t].left].size-1);return a[t].key;}int main(){int i,j,k;while (scanf("%d%d",&n,&m) != EOF) {tot = root = 0;char ope[10];while (n--) {scanf("%s",ope);if (ope[0] == 'I') {scanf("%d",&k);insert(root,k);}else printf("%d\n",find(root,a[root].size+1-m));}}}
This article is original in zeroclock, but it can be reproduced, because we are brothers.