Cod sursa(job #1804937)

Utilizator AhileGigel Frone Ahile Data 13 noiembrie 2016 12:06:47
Problema Heapuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include<bits/stdc++.h>
using namespace std;
#define in f
#define out g

ifstream f ("heapuri.in");
ofstream g ("heapuri.out");

int n;
int code;
int x;
int heap[1000010];
int poz;
int order[1000010];
int j;
int m[1000010];

int swapin(int a, int b) {

    int aux = order[a];
    order[a] = order[b];
    order[b] = aux;

    m[order[a]] = a;
    m[order[b]] = b;


    int aux1 = heap[a];
    heap[a] = heap[b];
    heap[b] = aux1;

}

int insertheap(int x) {

    poz++;
    heap[poz] = x;
    int i = poz;

    while(heap[i] < heap[i / 2] && i > 1) {

        swapin(i, i / 2);
        i = i / 2;

    }
}


int removeheap(int x) {

    int i = m[x];
    swapin(i, poz);
    heap[poz] = 0;
    poz--;
    while((heap[i] >= heap[2 * i] ||  heap[i] >= heap[2 * i + 1]) && i <= poz / 2) {

        if(heap[2 * i] < heap[2 * i + 1] || heap[2 * i + 1] == 0) {
            swapin(i, 2 * i);
            i *= 2;
        } else {
            swapin(i, 2 * i + 1);
            i = 2 * i + 1;
        }
    }

}

int minheap() {

    out << heap[1];
    out << "\n";

}

int main() {

    in >> n;
    for(int i = 1; i <= n; i++) {
        in >> code;
        if(code == 1) {
            in >> x;
            j++;
            order[j] = j;
            m[j] = j;
            insertheap(x);
        }
        if(code == 2) {
            in >> x;
            removeheap(x);
        }
        if(code == 3) {
            minheap();
        }
    }

}