Cod sursa(job #2015689)

Utilizator MihaelaCismaruMihaela Cismaru MihaelaCismaru Data 26 august 2017 23:13:37
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include<fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int sz,nr;
pair<int,int>heap[200001];
int n,i,t,y,x,poz[200001],s;
void swapi( int t, int p ){
    swap( poz[ heap[t].second ], poz[ heap[p].second ] );
    swap( heap[t], heap[p] );
}
void up( int p ){
    while( p > 1 && heap[p].first < heap[p/2].first ){
        swapi( p,p/2 );
        p /= 2;
    }
    return;
}
void down( int p ){
    while( p*2 <= sz  ){
        t=p*2;
        if( t < sz && heap[t+1].first < heap[t].first ){
            t ++;
        }
        if( heap[p].first > heap[t].first ){
            swapi( t,p );
            p = t;
        }
        else{
            break;
        }
    }
    return;
}
void adauga( int val ){
    heap[++sz] = make_pair( val, ++ nr );
    poz[ nr ] = sz;
    up( sz );down( sz );
    return;
}
int elimina( int p ){
    swapi( p,sz );
    sz--;
    up( p ); down( p );
}
int main(){
    in >> n;
    for( i = 1; i <= n; i ++ ){
        in >> t;
        if( t == 1 ){
            in >> x;
            adauga( x );
        }
        if( t == 2 ){
            in >> x;
            elimina( poz[x] );
        }
        if( t == 3 ){
            out<<heap[1].first<<"\n";
        }
    }
    return 0;
}