Cod sursa(job #1490859)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 24 septembrie 2015 11:53:22
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <fstream>
#include <vector>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
using namespace std;

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

int n, m;
int h[200001], nr[200001], poz[200001];

void Read();
void Insert(int el, int ord);
void Delete(int ord);

int main()
{
    Read();
    is.close();
    os.close();
    return 0;
}

void Read()
{
    int m, tip, el;
    is >> m;
    for ( int i = 1; i <= m; ++i )
    {
        is >> tip;
        if ( tip != 3 )
        {
            is >> el;
            if ( tip == 1 )
                Insert(el, i);
            else
                Delete(poz[el]);
        }
        else
            os << nr[h[1]] << "\n";
    }
}

void Delete(int ord)
{
    if ( ord == n )
    {
        --n;
        return;
    }
    h[ord] = h[n];
    poz[h[n--]] = ord;
    int up = ord, down = 2 * ord;
    while ( down <= n && ( nr[h[up]] > nr[h[down]] || ( down + 1 <= n && nr[h[up]] > nr[h[down + 1]] ) ) )
    {
        if ( down < n && nr[h[down]] > nr[h[down + 1]] )
            ++down;
        swap(poz[h[down]], poz[h[up]]);
        swap(h[down], h[up]);
        up = down;
        down *= 2;
    }
}

void Insert(int el, int ord)
{
    nr[ord] = el;
    h[++n] = ord;
    poz[ord] = n;
    int up = n / 2, down = n;
    while ( up && nr[h[down]] < nr[h[up]] )
    {
        swap(poz[h[down]], poz[h[up]]);
        swap(h[down], h[up]);
        down = up;
        up /= 2;
    }
}