Cod sursa(job #1676489)

Utilizator GinguIonutGinguIonut GinguIonut Data 5 aprilie 2016 22:38:22
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <vector>

#define pdd pair<double, double>
#define x first
#define y second
#define mkp make_pair
#define nMax 200005
#define pb push_back
#define MOD 666013
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n, k, nr;
int heap[nMax], poz[nMax], v[nMax];
void upDate(int pozi)
{
    int po;
    while(pozi/2)
    {
        po=pozi/2;
        if(v[heap[po]]>v[heap[pozi]])
        {
            swap(heap[po], heap[pozi]);
            swap(poz[heap[po]], poz[heap[pozi]]);
            pozi=po;
        }
        else
            break;
    }
}
void downDate(int pozi)
{
    int po;
    while(pozi*2<=k)
    {
        po=pozi*2;
        if(po+1<=k && v[heap[po]]>v[heap[po+1]])
            po++;

        if(v[heap[po]]<v[heap[pozi]])
        {
            swap(heap[po], heap[pozi]);
            swap(poz[heap[po]], poz[heap[pozi]]);
            pozi=po;
        }
        else
            break;
    }
}
void insert_heap(int node)
{
    heap[++k]=node;
    poz[node]=k;
    upDate(k);
}
void extract_heap(int pozi)
{
    swap(heap[k], heap[poz[pozi]]);
    swap(poz[heap[k]], poz[pozi]);
    k--;
    downDate(poz[pozi]);
}
void solve()
{
    int op, a;
    fin>>n;

    for(int i=1;i<=n;i++)
    {
        fin>>op;

        if(op==1)
        {
            fin>>v[++nr];
            insert_heap(nr);
            continue;
        }

        if(op==2)
        {
            fin>>a;
            extract_heap(a);
            continue;
        }

        fout<<v[heap[1]]<<'\n';
    }
}
int main()
{
    solve();
    return 0;
}