Cod sursa(job #2567551)

Utilizator luci.tosaTosa Lucian luci.tosa Data 3 martie 2020 17:48:27
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <fstream>
#define NMAX 200005
using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int n,h[NMAX],heap_size,pos[NMAX],order;
int father(int node) {
    return node/2;
}
int left_son(int node) {
    return 2*node;
}
int right_son(int node) {
    return 2*node+1;
}
void up(int h[],int n,int k) {
    int key=h[k];
    while(k>1 && h[k]<h[father(k)]) {
        swap(pos[k],pos[father(k)]);
        h[k]=h[father(k)];
        k=father(k);
    }
    h[k]=key;
}
void down(int h[],int n,int k) {
    int son;
    do {
        son=0;
        if(left_son(k)<=n) {
            son=left_son(k);
            if(right_son(k)<=n && h[right_son(k)]<h[left_son(k)])
                son=right_son(k);
            if(h[son]>=h[k])
                son=0;
            if(son) {
                swap(h[son],h[k]);
                swap(pos[son],pos[k]);
                k=son;
            }
        }
    } while(son);
}
void insert(int h[],int &n,int key) {
    h[++n]=key;
    up(h,n,n);
}

void erase(int h[],int &n,int k) {
    h[k]=h[n];
    n--;
    if(h[k]<h[father(k)])
        up(h,n,k);
    else
        down(h,n,k);
}
int main() {
    fin>>n;
    for(int i=1; i<=n; i++) {
        int p,x;
        fin>>p;
        if(p==1) {
            fin>>x;
            pos[++order]=heap_size;
            insert(h,heap_size,x);
        }
        if(p==2) {
            fin>>x;
            erase(h,heap_size,pos[x]);
        }
        if(p==3)
            fout<<h[1]<<"\n";
    }
    return 0;
}