Cod sursa(job #2627473)

Utilizator Bodo171Bogdan Pop Bodo171 Data 10 iunie 2020 23:03:40
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;
const int nmax=200005;
const int inf=(1<<30);
int heap[nmax],where[nmax],which[nmax];
int nodes,added;
void swapOrder(int a,int b){
    swap(heap[a],heap[b]);
    swap(which[a],which[b]);
    where[which[a]]=a;
    where[which[b]]=b;
}
void up(int pos){
    while(pos!=1&&heap[pos]<heap[pos/2]){
        swapOrder(pos,pos/2);
        pos/=2;
    }
}
void down(int pos=1){
    bool changed=true;
    while(changed){
        changed=false;
        if(2*pos<=nodes){
            int minSon=2*pos;
            if(2*pos+1<=nodes&&heap[2*pos+1]<=heap[2*pos])
                minSon=2*pos+1;
            if(heap[minSon]<heap[pos]){
                changed=true;
                swapOrder(minSon,pos);
                pos=minSon;
            }
        }
    }
}
int add(int val){
    ++nodes;
    ++added;
    which[nodes]=added;
    where[added]=nodes;
    heap[nodes]=val;
    up(nodes);
}
int del(int wh){
    heap[where[wh]]=-inf;
    up(where[wh]);
    heap[1]=heap[nodes];
    which[1]=which[nodes];
    nodes--;
    if(nodes)
        down();
}
int getMin(){
    return heap[1];
}
int main()
{
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    int n;
    for(f>>n;n--;){
        int tip,x;
        f>>tip;
        if(tip!=3)
            f>>x;
        if(tip==1)
            add(x);
        if(tip==2)
            del(x);
        if(tip==3)
            g<<getMin()<<'\n';
    }
    return 0;
}