Cod sursa(job #1813613)

Utilizator Malan_AbeleMalan Abele Malan_Abele Data 23 noiembrie 2016 08:40:23
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n,r,x;
int h[200010],nr;
int v[200010];
void adaug(int i){
    int poz=i;
    while(poz>1 && h[poz/2]>h[poz]){
        int t=h[poz/2];
        h[poz/2]=h[poz];
        h[poz]=t;
    }
}
void elimx(int &n,int x){
    h[x]=h[n];
    n--;
    int poz=x;
    while(2*poz<=n){
        int fiu=2*poz;
        if(fiu+1<=n && h[fiu+1]<h[fiu])
            fiu++;
        if(h[fiu]<h[poz]){
            int t=h[poz];
            h[poz]=h[fiu];
            h[fiu]=t;

            t=v[poz];
            v[poz]=v[fiu];
            v[fiu]=t;

            poz=fiu;
        }
        else
            poz=n+1;
    }
}
int main()
{
    in>>n;
    for(int i=1;i<=n;++i){
        in>>r;
        if(r==1){
            in>>x;
            ++nr;
            h[nr]=x;
            v[x]=nr;
            adaug(nr);
        }
        else if(r==2){
            in>>x;
            elimx(nr,v[x]);
        }
        else{
            out<<h[1]<<"\n";
        }
    }
    return 0;
}