Cod sursa(job #1557122)

Utilizator diana97Diana Ghinea diana97 Data 26 decembrie 2015 19:34:14
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include<iostream>
#include<fstream>
#define MAXN 200001
#define FIN "heapuri.in"
#define FOUT "heapuri.out"
#define in f
#define out g

using namespace std;

ifstream f(FIN);
ofstream g(FOUT);

int h[MAXN];
int p[MAXN];
int length;
int n;
int v[MAXN];



int swap(int firstPos, int secondPos)
{
    v[h[firstPos]] = secondPos;
    v[h[secondPos]] = firstPos;

    int aux = h[firstPos];
    h[firstPos] = h[secondPos];
    h[secondPos] = aux;

    return 0;
}

int insert(int value)
{
    length++;
    v[value] = length;
    h[length] = value;
    int position = length;
    while(h[position] <  h[position / 2] && position > 1)
    {
        swap(position, position / 2);
        position /= 2;
    }

    return 0;
}

int heapify(int pos)
{
    if(2 * pos <= length)
    {
        if(h[pos] > h[2 * pos])
        {
            swap(pos, 2 * pos);
            heapify(2 * pos);
        }
    }

    if(2 * pos + 1 <= length)
    {
        if(h[pos] > h[2 * pos + 1])
        {
            swap(pos, 2 * pos + 1);
            heapify(2 * pos + 1);
        }
    }
}

int erase(int pos)
{
    swap(pos, length);
    length--;

    heapify(pos);

    return 0;
}

int getMax()
{
    return h[1];
}

int main()
{
    in >> n;

    int x, y;
    int index = 0;
    for(int i = 1; i <= n; i++)
    {
        in >> x;

        switch(x)
        {
            case 1:
                in >> y;
                index++;
                p[index] = y;
                insert(y);
                break;
            case 2:
                in >> y;
                erase(v[p[y]]);
                break;
            case 3:
                out << getMax() << "\n";
                break;
        }


    }

    return 0;
}