Cod sursa(job #1360759)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 25 februarie 2015 17:39:48
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include<cstdio>
#include<string>

using namespace std;

#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "heapuri";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif

const int INF = (1 << 30);
const int NMAX = 200000 + 5;

int N, M;
int V[NMAX];
int H[NMAX], top;
int P[NMAX];

void heap_up(int f) {
    int p = f / 2;

    if(!p)
        return;

    if(V[H[f]] < V[H[p]]) {
        swap(H[f], H[p]);
        swap(P[H[f]], P[H[p]]);
        heap_up(p);
    }
}

void heap_down(int p) {
    int f = 2 * p;

    if(f > top)
        return;

    if(V[H[f + 1]] < V[H[f]] && f + 1 <= top)
        f++;

    if(V[H[f]] < V[H[p]]) {
        swap(H[f], H[p]);
        swap(P[H[f]], P[H[p]]);
        heap_down(f);
    }
}

void insert(int x) {
    V[++N] = x;
    H[++top] = N;
    P[N] = top;
    heap_up(top);
}

void erase(int poz) {
    V[poz] = INF;
    heap_down(P[poz]);
    top--;
}

int main() {
    int t, x;

    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);

    scanf("%d", &M);

    while(M--) {
        scanf("%d", &t);

        if(t == 1) {
            scanf("%d", &x);
            insert(x);
            continue;
        }

        if(t == 2) {
            scanf("%d", &x);
            erase(x);
            continue;
        }

        if(t == 3) {
            printf("%d\n", V[H[1]]);
            continue;
        }
    }

    return 0;
}