AcWing 839. 模拟堆(模板)
程序员文章站
2022-03-31 20:33:18
...
题目链接:点击这里
带映射关系的堆:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100010;
int h[N], ph[N], hp[N], sz;
// h[N]存储堆中的值, h[1]是堆顶,x的左儿子是2x, 右儿子是2x+1
// ph[k]存储第k个插入的点在堆中的位置
// hp[k]存储堆中下标是k的点是第几个插入的
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 u)
{
int t = u;
if(u * 2 <= sz && h[u * 2] < h[t]) t = u * 2;
if(u * 2 + 1 <= sz && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
if(u != t)
{
heap_swap(t, u);
down(t);
}
}
void up(int u)
{
while(u / 2 && h[u] < h[u / 2])
{
heap_swap(u, u / 2);
u >>= 1;
}
}
int main()
{
int n, m = 0; //m表示当前是第几个插入的数
scanf("%d", &n);
while(n--)
{
char op[5];
int k, x;
scanf("%s", op);
if(!strcmp(op, "I"))
{
scanf("%d", &x);
sz++;
m++;
ph[m] = sz;
hp[sz] = m;
h[sz] = x;
up(sz);
}
else if(!strcmp(op, "PM"))
{
printf("%d\n", h[1]);
}
else if(!strcmp(op, "DM"))
{
heap_swap(1, sz);
sz--;
down(1);
}
else if(!strcmp(op, "D"))
{
scanf("%d", &k);
k = ph[k];
heap_swap(k, sz);
sz--;
down(k), up(k);
}
else if(!strcmp(op, "C"))
{
scanf("%d%d", &k, &x);
k = ph[k];
h[k] = x;
down(k), up(k);
}
}
return 0;
}
上一篇: 真没想到还能这样玩的。