Cod sursa(job #1025766)

Utilizator StexanIarca Stefan Stexan Data 10 noiembrie 2013 15:43:13
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb

#include <iostream>
#include <fstream>

const int MAX_SIZE = 200010;
int V[MAX_SIZE],Heap[MAX_SIZE],P[MAX_SIZE];
int N, NR, L;

using namespace std;

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

int father(int nod){
    return nod/2;
}
int left_son(int nod){
    return nod * 2;
}
int right_son(int nod){
    return nod * 2 + 1;
}

void push(int x){
    int val;
    while (father(x) && V[Heap[x]] < V[Heap[father(x)]]) {
        val = Heap[x];
        Heap[x] = Heap[father(x)];
        Heap[father(x)] = val;
        
        P[Heap[x]] = x;
        P[Heap[father(x)]] = father(x);
        x /= 2;
    }
    
}
void pop(int x){
    int y,aux;
    
    while (x != y)
    {
        y = x;
        
        if (left_son(y) <= L && V[Heap[x]] > V[Heap[left_son(y)]])
            x = left_son(y);
        if (right_son(y) <= L && V[Heap[x]] > V[Heap[right_son(y)]])
            x = right_son(y);
        
        aux = Heap[x];
        Heap[x] = Heap[y];
        Heap[y] = aux;
        
        P[Heap[x]] = x;
        P[Heap[y]] = y;
    }
}

int main()
{
    f>>N;
    int x,cif;
    for (int i = 1; i <= N; i++) {
        f>>cif;
        if (cif != 3) {
            f>>x;
        }
        if ( cif == 1 ){
            NR++; L++;
            V[NR] = x;
            Heap[L] = NR;
            P[NR] = L;
            push(L);
        }else if(cif == 2){
            V[x] = -1;
            push(P[x]);
            
            P[Heap[1]] = 0;
            Heap[1] = Heap[L--];
            P[Heap[1]] = 1;
            if (L>1)
                pop(1);
            
        }else if (cif == 3){
            g<<V[Heap[1]]<<"\n";
        }
    }
    return 0;
}