维护一个集合,初始时集合为空,支持如下几种操作:
“I x”,插入一个数x;
“PM”,输出当前集合中的最小值;
“DM”,删除当前集合中的最小值(数据保证此时的最小值唯一);
“D k”,删除第k个插入的数;
“C k x”,修改第k个插入的数,将其变为x;
现在要进行N次操作,对于所有第2个操作,输出当前集合的最小值。
输入格式
第一行包含整数N。
接下来N行,每行包含一个操作指令,操作指令为”I x”,”PM”,”DM”,”D k”或”C k x”中的一种。
输出格式
对于每个输出指令“PM”,输出一个结果,表示当前集合中的最小值。
每个结果占一行。
数据范围
1≤N≤105
−109≤x≤109
数据保证合法。
输入样例:
8
I -10
PM
I -10
D 1
C 2 8
I 6
PM
DM
输出样例:
-10
6
难度: 简单
时/空限制: 1s / 64MB
#include <iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=100010;
int h[N],siz;
int ph[N];//第k个插入数的下标
int hp[N];//堆里插入的第几个点
void heap_swap(int a,int b)
{
swap(ph[hp[a]],ph[hp[b]]);
swap(hp[a],hp[b]);
swap(h[a],h[b]);
}
void down(int x)
{
int t=x;
if(x*2<=siz&&h[x*2]<h[t])t=x*2;
if(x*2+1<=siz&&h[x*2+1]<h[t])t=2*x+1;
if(x!=t)
{
heap_swap(x,t);
down(t);
}
}
void up(int x)
{
while(x/2&&h[x/2]>h[x])
{
heap_swap(x/2,x);
x/=2;
}
}
int main()
{
int n;
int m=0;
cin>>n;
while(n--)
{
char op[10];
cin>>op;
int k,x;
if(!strcmp(op,"I"))
{
cin>>x;
siz++;
m++;
ph[m]=siz;
hp[siz]=m;
h[siz]=x;
up(siz);
}
else if(!strcmp(op,"PM"))printf("%d\n",h[1]);
else if(!strcmp(op,"DM"))
{
heap_swap(1,siz);
siz--;
down(1);
}
else if(*op=='D')
{
cin>>k;
k=ph[k];
heap_swap(k,siz);
siz--;
down(k);
up(k);
}
else
{
cin>>k>>x;
k=ph[k];
h[k]=x;
down(k),up(k);
}
}
return 0;
}