Cod sursa(job #1315947)

Utilizator costyv87Vlad Costin costyv87 Data 13 ianuarie 2015 13:07:46
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <cstdio>
#include <algorithm>
FILE *f,*g;
using namespace std;

int v[200200],nn;
int pos[200200];
int H[200200],nr;
int x,q,T,type;

void up(int x)
{

    while ( x/2  && v[H[x]] < v[H[x/2]])
    {
        swap(H[x],H[x/2]);
        swap(pos[H[x]],pos[H[x/2]]);
        x/=2;
    }
}


void down(int x)
{
    int y=0;

    while (x!=y)
    {
        y = x;
        if (2*y<=nr && v[H[2*y]]<v[H[x]]) x = 2*y;
        if (2*y+1<=nr && v[H[2*y+1]]<v[H[x]]) x = 2*y+1;

        swap(H[x],H[y]);
        swap(pos[H[x]],pos[H[y]]);
    }
}

void ins(int val)
{
    H[++nr] = nn;
    pos[nn] = nr;

    up(nr);
}

void del(int p)
{
    v[p] = 1000000100;
    down(pos[p]);
    nr--;
}


int main()
{
    f=fopen("heapuri.in","r");
    g=fopen("heapuri.out","w");

    fscanf(f,"%d",&T);

    for (q=1;q<=T;q++)
    {
        fscanf(f,"%d",&type);

        switch (type)
        {
            case 1:
            {
                fscanf(f,"%d",&v[++nn]);
                ins(v[nn]);
                break;
            }
            case 2:
            {
                fscanf(f,"%d",&x);
                del(x);
                break;
            }
            case 3:
            {
                fprintf(g,"%d\n",v[H[1]]);
                break;
            }
            break;
        }

    }

    return 0;
}