• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

image

image

单击以查看代码#includeiostream。

使用命名空间std

const int N=1e5 10

int h[N],sz,m;

int ph[N],HP[N];

void heap_swap(int a,int b)

{

swap(ph[hp[a]],ph[HP]);

swap(hp[a],HP);

swap(h[a],h);

}

向下无效(整数)

{

int t=u;

if(2 * u=SZ h[2 * u]h[t])t=2 * u;

if(2 * u 1=SZ h[2 * u 1]h[t])t=2 * u 1;

如果(t!=u) {

heap_swap(u,t);

down(t);

}

}

清空(整数)

{

while (u/2 h[u/2] h) {

heap_swap(u/2,u);

u/=2;

}

}

int main()

{

int n;

CIN n;

while (n - ) {

字符串s;

int k,x;

cin的;

if (s=='I') {

cin十世;

SZ;

m;

h[SZ]=x;

ph[m]=sz,HP[SZ]=m;

up(SZ);

}

else if(s==' PM ')cout h[1]endl;

else if (s=='DM') {

heap_swap(1,SZ);

SZ-;

向下(1);

}

else if (s=='D') {

CIN k;

k=ph[k];

heap_swap(k,SZ);

SZ-;

向下(k),向上(k);

}

else if (s=='C') {

CIN k x;

k=ph[k];

h[k]=x;

向下(k),向上(k);

}

}

返回0;

}

维护小型根堆的三个重要操作:

heap_swap:当两个节点需要交换时,除了节点的值,还需要交换和数字之间的双向指针hp和ph(HP表示堆节点指向编号节点,ph表示编号节点指向堆节点);

down:维护U节点及其下的节点,当子节点的值小于父节点的值时,用heap_swap交换两个节点;

up:维护U节点及其上面的节点,当父节点的值大于子节点的值时,用hedp_swap交换两个节点;(无论u是左孩子还是右孩子,u/2都代表同一个父节点)

删除第k个插入数时,需要先让k=ph[ k],然后用swap交换k和sz的值。如果直接用swap交换ph[ k]和sz的值,那么交换后ph[ k]指向sz,不能用down和up处理K指向的位置;(head_swap不交换数组中的下标。用k保存p[ k]后,k一直是被处理节点对应的数组下标)

Link to comment
Share on other sites