Cod sursa(job #1814010)

Utilizator robx12lnLinca Robert robx12ln Data 23 noiembrie 2016 16:26:51
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include<fstream>
#define DIM 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int x, Posh[DIM], op, n, m, k;
struct str{
    int val;
    int inh;
} Heap[DIM];

void up( int son ){
    int father = son / 2;
    if( father >= 1 && Heap[son].val < Heap[father].val ){
        swap( Posh[ Heap[son].inh ], Posh[ Heap[father].inh ] );
        swap( Heap[son], Heap[father] );
        up(father);
    }
    return ;
}
void down( int father ){
    int son = father * 2;
    if( son + 1 <= n && Heap[son].val > Heap[son + 1].val ){
        son++;
    }
    if( son <= n && Heap[son].val < Heap[father].val ){
        swap( Posh[ Heap[son].inh ], Posh[ Heap[father].inh ] );
        swap( Heap[son], Heap[father] );
        down( son );
    }
    return ;
}

int main(){
    fin >> m;
    for( int i = 1; i <= m; i++ ){
        fin >> op;
        if( op == 1 ){
            fin >> x;
            n++;
            k++;
            Heap[n].val = x;
            Heap[n].inh = k;
            Posh[k] = n;
            up( n );
        }
        if( op == 2 ){
            fin >> x;
            int pos = Posh[x];
            swap( Posh[ Heap[pos].inh ], Posh[ Heap[n].inh ] );
            swap( Heap[pos], Heap[n] );
            Posh[ Heap[n].inh ] = 0;
            n--;
            down( pos );
        }
        if( op == 3 ){
            fout << Heap[1].val << "\n";
        }
    }
    return 0;
}