Cod sursa(job #2591676)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 30 martie 2020 22:51:29
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.12 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 2e5;

int posh[5 + N];

class Heap {
    private:
        int hsz;
        pair <int, int> heap[5 + N];

        void Sift_Down(int pos){
            int child;
            child = pos * 2;
            if(child + 1 <= hsz && heap[child].first > heap[child + 1].first) child++;

            while(child <= hsz && heap[child].first < heap[pos].first) {
                posh[heap[pos].second] = child;
                posh[heap[child].second] = pos;
                swap(heap[pos], heap[child]);

                pos = child;
                child = pos * 2;

                if(child + 1 <= hsz && heap[child].first > heap[child + 1].first) child++;
            }
        }

        void Sift_Up(int pos){
            int parent;
            parent = pos >> 1;

            while(heap[pos].first < heap[parent].first && parent > 0){
                posh[heap[pos].second] = parent;
                posh[heap[parent].second] = pos;
                swap(heap[pos], heap[parent]);

                pos = parent;
                parent = pos >> 1;
            }
        }

    public:
        Heap(){
            hsz = 0;
            memset(heap, 0, sizeof(heap));
        }

        void Push(int val, int posi){
            heap[++hsz] = make_pair(val, posi);
            posh[posi] = hsz;

            Sift_Up(hsz);
        }

        void Pop(int pos){
            posh[heap[hsz].second] = pos;
            posh[heap[pos].second] = -1;
            swap(heap[pos], heap[hsz]);
            hsz--;

            Sift_Up(pos);
            Sift_Down(pos);
        }

        int Get_Min(){
            return heap[1].first;
        }
}H;

int main() {
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);
    int n, i(0);
    scanf("%d", &n);

    while(n--){
        int tip, x;
        scanf("%d", &tip);

        if(tip == 1){
            scanf("%d", &x);
            H.Push(x, ++i);
        } else if(tip == 2){
            scanf("%d", &x);
            H.Pop(posh[x]);
        } else printf("%d\n", H.Get_Min());
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}