Cod sursa(job #1405365)

Utilizator 4ONI2015oni2015 4ONI2015 Data 29 martie 2015 09:53:21
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;
set<pair<int, int>>s;
int n, x, t, i, cnt, a[200005],h[200005],poz[200005];
void heapup(int nod)
{
    int tata = nod / 2;
    if(!tata)
        return;
    if(a[h[tata]] > a[h[nod]])
    {
        swap(poz[h[nod]], poz[h[tata]]);
        swap(h[nod], h[tata]);
        heapup(tata);
    }
}
void heapdown(int nod)
{
    int fs=2*nod;
    if(fs>cnt)
        return;
    if(fs!=cnt&&a[h[fs]]>a[h[fs+1]])fs++;
    if(a[h[nod]]>a[h[fs]])
    {
        swap(h[nod],h[fs]);
        swap(poz[h[fs]],poz[h[nod]]);
        heapdown(fs);
    }
}
int main()
{
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);
    scanf("%d", &n);
    for(;n;n--)
    {
        scanf("%d", &t);
        if(t == 1)
        {
            scanf("%d", &a[++i]);
            h[++cnt] = i;
            poz[i] = cnt;
            heapup(cnt);
            continue;
        }
        if(t == 2)
        {
            scanf("%d", &x);
            poz[h[cnt]] = poz[x];
            h[poz[x]] = h[cnt];
            cnt--;
            heapdown(poz[x]);
            continue;
        }
        printf("%d\n", a[h[1]]);
    }
    return 0;
}