Cod sursa(job #1074716)

Utilizator dan.ghitaDan Ghita dan.ghita Data 7 ianuarie 2014 21:40:12
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_map>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
unordered_map<int, int> h;
int n=0, v[200002], m, p, x, t, del[200002];
void up()
{
    while(v[p]<v[p/2]&&p>1)
        swap(h[v[p]], h[v[p/2]]), swap(v[p], v[p/2]), p/=2;
}

void down()
{
    int s;
    while(2*p<=n)
    {
        s=2*p;
        if(2*p+1>n)
        {
            if(v[p]>v[s]) swap(h[v[p]], h[v[s]]), swap(v[p], v[s]);
            break;
        }
        else
        {
            if(v[s]<=v[s+1]&&v[p]>v[s]) swap(h[v[p]], h[v[s]]), swap(v[p], v[s]);
            else if(v[p]>v[s+1]) swap(h[v[p]], h[v[s+1]]), swap(v[p], v[s+1]), s++;
        }
        p=s;
    }
}

void push(int x)
{
    v[++n]=x;
    h[x]=n;
    p=n;
    up();
}

void pop(int poz)
{
    v[poz]=v[n--];
    h[v[poz]]=poz;
    p=poz;
    up();
    down();
}

int main()
{
    f>>m;
    int i=0;
    while(m--)
    {
        f>>t;
        if(t==1) f>>x, push(x), del[++i] = x;
        if(t==2) f>>x, pop(h[del[x]]);
        if(t==3) g<<v[1]<<'\n';
    }
    return 0;
}