Cod sursa(job #1869909)

Utilizator BlackNestaAndrei Manaila BlackNesta Data 6 februarie 2017 11:25:43
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m;
int a[200005], ran[200005];
unordered_map <int,int> poz;

inline void Push(int x)
{
    int k, tata;
    a[++n] = x;
    poz[x] = n;
    k = n;
    while(k > 1)
    {
        tata = k / 2;
        if(a[k] >= a[tata])
            return;
        swap(a[k], a[tata]);
        swap(poz[a[k]], poz[a[tata]]);
        k = tata;
    }
}

inline void Pop(int k)
{
    int fiu;
    a[k] = a[n];
    poz[a[n]] = k;
    n--;
    while(2 * k <= n)
    {
        fiu = 2 * k;
        if(fiu + 1 <= n && a[fiu] > a[fiu + 1])
            fiu++;
        if(a[k] <= a[fiu])
            return;
        swap(a[k], a[fiu]);
        swap(poz[a[k]], poz[a[fiu]]);
        k = fiu;
    }
}

inline void Read()
{
    int i, op, x, k;
    k = 0;
    fin >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> op;
        if(op == 1)
        {
            fin >> x;
            k++;
            ran[k] = x;
            Push(x);
        }
        else if(op == 2)
        {
            fin >> x;
            x = ran[x];
            x = poz[x];
            Pop(x);
        }
        else fout << a[1] << "\n";
    }
}

int main()
{
    Read();
    return 0;
}