Cod sursa(job #1645841)

Utilizator robx12lnLinca Robert robx12ln Data 10 martie 2016 14:01:52
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include<fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int Pos[200005],n,m,op,x,y,k;
struct str{
    int pos;
    int val;
} Heap[200005];

str make_str( int pos, int val ) {
    str my_str = { pos, val };
    return my_str;
}

void up( int son ) {
    int father = son / 2;

    if( father >= 1 && Heap[son].val < Heap[father].val ){
        swap( Pos[ Heap[son].pos ], Pos[ Heap[father].pos ] );
        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( Pos[ Heap[son].pos ], Pos[ Heap[father].pos ] );
        swap( Heap[son], Heap[father] );
        down( son );
    }
    return ;
}

int main(){
    fin >> m;
    for( int i = 1; i <= m; i++ ){
        fin >> op;
        switch(op){
            case 1: {
                fin >> x;
                Heap[++n] = make_str( ++k, x );
                Pos[k] = n;
                up( n );
                break;
            }
            case 2: {
                fin >> x; y = Pos[x];
                swap( Pos[ Heap[y].pos ], Pos[ Heap[n].pos ] );
                swap( Heap[n], Heap[y] ); Pos[ Heap[n].pos ] = 0;
                n --; down( y );
                break;
            }
            case 3: {
                fout << Heap[1].val << "\n";
                break;
            }
        }
    }
    return 0;
}