Cod sursa(job #1076369)

Utilizator handz.FMI Andrei Tanasescu handz. Data 10 ianuarie 2014 02:14:44
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <iostream>
#include <fstream>
using namespace std;

#define maxN 200005

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n,k,nr;
int v[maxN],heap[maxN],poz[maxN];

void insert_elem(int x)
{
    int aux;

    while( x/2 && v[heap[x]] < v[heap[x/2]] )
    {
        aux = heap[x];
        heap[x] = heap[x/2];
        heap[x/2] = aux;

        poz[heap[x]] = x;
        poz[heap[x/2]] = x/2;
        x = x/2;
    }
}

void delete_elem(int x)
{
    int aux, y = 0;

    while(x != y)
    {
        y = x;

        if( y*2<=k && v[heap[x]]>v[heap[y*2]]) x = y*2;
        if( y*2+1<=k && v[heap[x]]>v[heap[y*2+1]]) x = y*2+1;

        aux = heap[x];
        heap[x] = heap[y];
        heap[y] = aux;

        poz[heap[x]] = x;
        poz[heap[y]] = y;
    }
}

int main()
{
    int i,x,t;

    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);

    scanf("%d ", &n);

    for(i=1; i<=n; i++)
    {
        scanf("%d ", &t);

        if(t<3)
        {
            scanf("%d ", &x);
        }

        if(t==1)
        {
            k++; nr++;
            v[nr] = x;
            heap[k] = nr;
            poz[nr] = k;

            insert_elem(k);
        }

        if(t==2)
        {
            v[x] = -1;
            //assert(Pos[x] != 0);
            //assert(1<=x && x<=NR);
            insert_elem(poz[x]);

            poz[heap[1]] = 0;
            heap[1] = heap[k--];
            poz[heap[1]] = 1;
            if(k>1) delete_elem(1);
        }

        if(t==3) printf("%d\n", v[heap[1]]);
    }

    return 0;
}