Cod sursa(job #788734)

Utilizator round2Test P round2 Data 15 septembrie 2012 18:59:39
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <cstdio>
#include <algorithm>
using namespace std;
#define Max 200002
struct heap{ int x,n; }h[Max];

int k,n,pos[Max];

void add(int x)
{
    int t,f;
    n++; k++;
    h[k].x=x;
    h[k].n=n;
    pos[n]=k;
    f=k; t=f/2;
    while(t>0 && h[f].x<h[t].x)
    {
        swap(h[t],h[f]);
        swap(pos[h[t].n],pos[h[f].n]);
        f=t; t=f/2;
    }
}

void rem(int x)
{
    int t,f;
    t=pos[x];
    h[t]=h[k--];
    pos[h[t].n]=t;
    f=2*t;
    if(f+1<=k && h[f+1].x<h[f].x)f++;
    while(f<=k && h[f].x<h[t].x)
    {
        swap(h[t],h[f]);
        swap(pos[h[t].n],pos[h[f].n]);
        t=f; f=2*t;
        if(f+1<=k && h[f+1].x<h[f].x)f++;
    }
}

int main()
{
    int m,c,x;
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&c);
        switch(c)
        {
            case 1: scanf("%d",&x); add(x); break;
            case 2: scanf("%d",&x); rem(x); break;
            case 3: printf("%d\n",h[1].x);  break;
        }
    }
    return 0;
}