Cod sursa(job #2268778)

Utilizator andrei20003Ionescu Andrei andrei20003 Data 25 octombrie 2018 12:04:19
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>

using namespace std;

int h[200001],poz[200001],v[200001],nh;

void swapp(int p,int q) {
    swap(h[p],h[q]);
    poz[h[p]]=p;
    poz[h[q]]=q;
}

void coboara(int p) {
    int fs=2*p,fd=2*p+1,bun=p;
    if (fs<=nh && v[h[fs]]<v[h[bun]])
        bun=fs;
    if (fd<=nh && v[h[fd]]<v[h[bun]])
        bun=fd;
    if (bun!=p) {
        swapp(p,bun);
        coboara(bun);
    }
}

void urca(int p) {
    if (p>1 && v[h[p]]<v[h[p/2]])
        swapp(p,p/2),urca(p/2);
}

void sterge(int p) {
    swapp(p,nh--);
    urca(p);
    coboara(p);
}

void adauga(int x) {
    h[++nh]=x;
    poz[x]=nh;
    urca(nh);

}

int main()
{
    int n,i,a,tip,nrad=0,p,val;
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d", &n);
    for (i=0;i<n;i++) {
        scanf("%d", &tip);
        if (tip==1) {
            scanf("%d", &val);
            v[++nrad]=val;
            adauga(nrad);
        }
        if (tip==2)
            scanf("%d", &p),sterge(poz[p]);
        if (tip==3)
            printf("%d\n", v[h[1]]);
    }
    return 0;

}