Cod sursa(job #610221)

Utilizator tzipleatudTudor Tiplea tzipleatud Data 25 august 2011 19:12:58
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#define NMAX 200010

using namespace std;

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

int n,i,o,x,heap[NMAX],pos[NMAX],v[NMAX],nrh,nrt;

void push(int a) {
    while (a/2!=0 && v[heap[a]]<v[heap[a/2]]) {
        swap(heap[a],heap[a/2]);
        pos[heap[a]]=a;
        pos[heap[a/2]]=a/2;a/=2;
    }
}
void pop(int a) {
    int b=0;
    while (a!=b) {
        b=a;
        if (2*b<=nrh && v[heap[a]]>v[heap[b*2]]) a=b*2;
        if (2*b+1<=nrh && v[heap[a]]>v[heap[b*2+1]]) a=b*2+1;
        swap(heap[a],heap[b]);
        pos[heap[a]]=a;pos[heap[b]]=b;
    }
}

int main () {
    f >> n;
    for (i=1;i<=n;i++) {
        f >> o;
        if (o==1) {
            f >> x;
            nrh++;nrt++;
            v[nrt]=x;
            heap[nrh]=nrt;
            pos[nrt]=nrh;
            push(nrh);
        }
        if (o==2) {
            f >> x;
            v[x]=-1;push(pos[x]);
            pos[heap[1]]=0;heap[1]=heap[nrh];
            nrh--;
            pos[heap[1]]=1;
            if (nrh>1) pop(1);
        }
        if (o==3) g << v[heap[1]] << '\n';
    }
    f.close();g.close();
    return 0;
}