Cod sursa(job #2492571)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 14 noiembrie 2019 22:15:44
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
#include <bits/stdc++.h>
#define DIM 200010
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,a[DIM],h[DIM],poz[DIM],i,l,x,cod,nod,tata;
int main() {
    fin>>n;
    while (n--) {
        fin>>cod;
        switch (cod) {
            case 1: {
                fin>>x;
                a[++i]=x, h[++l]=i;
                poz[i]=l;
                nod=l, tata=nod/2;
                while (tata) { //inserez nodul in heap
                    if (a[h[nod]]<a[h[tata]]) {
                        swap(h[nod],h[tata]);
                        poz[h[nod]]=nod, poz[h[tata]]=tata;
                    }
                    else
                        break;
                    nod=tata, tata/=2;
                }
                break;
            }
            case 2: {
                fin>>x;
                a[x]=-1;
                nod=poz[x], tata=nod/2;
                while (tata) { //urc nodul in radacina pentru a-l sterge
                    if (a[h[nod]]<a[h[tata]]) {
                        swap(h[nod],h[tata]);
                        poz[h[nod]]=nod, poz[h[tata]]=tata;
                    }
                    else
                        break;
                    nod=tata, tata/=2;
                }
                h[1]=h[l--], poz[h[1]]=1;
                nod=2, tata=1;
                while (nod<=l) { //sterg radacina
                    if (nod<l&&a[h[nod+1]]<a[h[nod]])
                        nod++;
                    if (a[h[tata]]>a[h[nod]]) {
                        swap(h[nod],h[tata]);
                        poz[h[nod]]=nod, poz[h[tata]]=tata;
                    }
                    else
                        break;
                    tata=nod, nod*=2;
                }
                break;
            }
            case 3: {
                fout<<a[h[1]]<<"\n";
                break;
            }
        }
    }
    return 0;
}