hdoj 5249 KPI(treap)

时间:2023-03-09 15:30:57
hdoj 5249 KPI(treap)

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

思路分析:使用queue记录管道中的值并使用treap能够查询第K大的功能查询第floor(m/2)+1大的数值;

对于in value操作,将value插入queue中和treap中;对于out操作,在treap中删除queue中队头元素,并在queue中使队头元素出队;

对于query操作,因为tail – head的值即为管道中的值的个数,使用treap查询第floor((tail – head) / 2) + 1大的元素即可;

代码如下:

#include <cstdio>
#include <ctime>
#include <cstring>
#include <iostream>
using namespace std; const int MAX_N = + ;
int queue[MAX_N]; struct Node{
Node *ch[];
int value, key;
int size;
int cmp(int x) const{
if (x == value) return -;
return x < value ? : ;
}
void Maintain(){
size = ;
if (ch[] != NULL) size += ch[]->size;
if (ch[] != NULL) size += ch[]->size;
}
}; void Rotate(Node *&o, int d){
Node *k = o->ch[d ^ ];
o->ch[d ^ ] = k->ch[d];
k->ch[d] = o;
o->Maintain();
k->Maintain();
o = k;
} void Insert(Node *&o, int x){
if (o == NULL){
o = new Node();
o->ch[] = o->ch[] = NULL;
o->value = x;
o->key = rand();
}
else{
int d = (x < (o->value) ? : );
Insert(o->ch[d], x);
if (o->ch[d]->key > o->key)
Rotate(o, d ^ );
}
o->Maintain();
} void Remove(Node *&o, int x){
int d = o->cmp(x); if (d == -){
Node *u = o; if (o->ch[] != NULL && o->ch[] != NULL){
int d2 = (o->ch[]->key > o->ch[]->key ? : ); Rotate(o, d2);
Remove(o->ch[d2], x);
}else{
if (o->ch[] == NULL)
o = o->ch[];
else
o = o->ch[];
delete u;
}
}else
Remove(o->ch[d], x);
if (o != NULL)
o->Maintain( );
} int Find(Node *o, int x){
while (o != NULL){
int d = o->cmp(x); if (d == -) return ;
else o = o->ch[d];
}
return ;
} int FindKth(Node *o, int k){
int l_size = (o->ch[] == NULL ? : o->ch[]->size);
if (k == l_size + )
return o->value;
else if (k <= l_size)
return FindKth(o->ch[], k);
else
return FindKth(o->ch[], k - l_size - );
} void MakeEmpty(Node *root){
if (root == NULL)
return;
if (root->ch[])
MakeEmpty(root->ch[]);
if (root->ch[])
MakeEmpty(root->ch[]);
delete root;
} int main(){
int n, case_id = , value; srand();
while (scanf("%d", &n) != EOF){
Node *root = NULL;
int head = , tail = , ans;
char str[]; printf("Case #%d:\n", ++case_id);
for (int i = ; i < n; ++i){
scanf("%s", str);
if (str[] == 'i'){
scanf("%d", &value);
queue[tail++] = value;
Insert(root, value);
}
else if (str[] == 'o')
Remove(root, queue[head++]);
else{
ans = FindKth(root, (tail - head) / + );
printf("%d\n", ans);
}
}
MakeEmpty(root);
}
return ;
}