Cod sursa(job #1646547)

Utilizator cristinamateiCristina Matei cristinamatei Data 10 martie 2016 16:34:42
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int N = 200003;
int h[N], pos[N], n, l, a[N], m;

void up( int x )
{
    int j = x;
    while( a[h[j]] < a[ h[j/2] ] && j/2 )
    {
        swap(h[j], h[j/2]);
        pos[ h[j] ] = j;
        pos[h[j/2] ] = j/2;
        j = j/2;
    }
}

void adauga( int x )
{
    n++;
    a[n] = x;
    l++;
    h[l] = n;
    pos[n] = l;
    up(l);
}

void down( int x )
{
    int i = 0;
    while( x != i )
    {
        if ( 2 *i <= l && a[h[x] ] > a[h[2*i]] )
            x = 2*i;
        if ( 2*i+1 <= l && a[h[x]] > a[h[2*i+1]] )
            x = 2*i+1;
        swap(h[i], h[x]);
        pos[h[i]] = i;
        pos[h[x]] = x;
    }
}

int main()
{
    int i, x, y;
    in >> m;
    for ( i = 1; i <= m; i++ )
    {
        in >> x;
        if ( x == 1 )
        {
            in >> y;
            adauga(y);
        }
        if ( x == 2 )
        {
            in >> y;
            h[pos[y]] = h[l];
            l--;
            down(x);
            up(x);
        }
        if ( x == 3 )
            out << a[h[1]]<<'\n';
    }

    return 0;
}