Cod sursa(job #2082896)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 6 decembrie 2017 21:27:55
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

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

const int dim = 200000;

int n, len;
int heap[dim], pos[dim], val[dim];

void push(int x) {
    int c = x, p = x / 2;
    while (p) {
        if (val[heap[p]] <= val[heap[c]])
            break;

        swap(heap[p], heap[c]);

        pos[heap[p]] = p;
        pos[heap[c]] = c;

        c = p; p = c / 2;
    }
}

void pop(int x) {
    int p = x, c = 2 * x;
    while (c <= len) {
        if (c < len && val[heap[c + 1]] <= val[heap[c]])
            ++c;

        if (val[heap[p]] <= val[heap[c]])
            break;

        swap(heap[p], heap[c]);

        pos[heap[p]] = p;
        pos[heap[c]] = c;
        p = c, c = 2 * p;
    }
}

int main() {
    int opCount;
    fin >> opCount;

    while (opCount--) {
        int op;
        fin >> op;

        if (op == 1) {
            ++n; ++len;
            fin >> val[n];
            heap[len] = n;
            pos[n] = len;
            push(len);
        }
        else if (op == 2) {
            int x;
            fin >> x;

            val[x] = -1;
            push(pos[x]);

            pos[heap[1]] = 0;
            heap[1] = heap[len--];
            pos[heap[1]] = 1;

            pop(1);
        }
        else
            fout << val[heap[1]] << '\n';
    }

    return 0;

}