Cod sursa(job #1411748)

Utilizator ovidiuz98Zamfir Ovidiu ovidiuz98 Data 31 martie 2015 22:08:00
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>
#define DIM 200005

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int N,h[DIM],poz[DIM],v[DIM],M,Q;
void upheap(int x){
    int c=x;
    int p=c/2;
    while(p>=1 && v[h[p]]>v[h[c]]){
        swap(h[c],h[p]);
        swap(poz[h[c]],poz[h[p]]);
        c=p;
        p/=2;
    }
}
void downheap(int x){
    int p=x;
    int c=p*2;
    while(c<=N){
        if(c+1<=N && v[h[c+1]]<v[h[c]])
            c++;
        if(v[h[p]]>v[h[c]]){
            swap(h[c],h[p]);
            swap(poz[h[c]],poz[h[p]]);
            p=c;
            c*=2;
        }
        else
            break;
    }
}
void insert(int x){
    h[++N]=x;
    poz[x]=N;
    upheap(N);
}
void delete_root(int x){
    v[x]=-1;
    upheap(poz[x]);
    swap(h[1],h[N]);
    poz[h[1]]=1;
    N--;
    downheap(1);
}
int main(){
    fin>>Q;
    while(Q--){
        int op;
        fin>>op;
        if(op==1){
            fin>>v[++M];
            insert(M);
            continue;
        }
        if(op==2){
            int x;
            fin>>x;
            delete_root(x);
            continue;
        }
        fout<<v[h[1]]<<"\n";
    }
    fin.close();fout.close();
    return 0;
}