Cod sursa(job #1499857)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 11 octombrie 2015 11:17:35
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 2e5 + 5;

int L, NR;
int A[NMax], Heap[NMax], Pos[NMax];

inline void Swap(int &a, int &b){
    int aux = a;
    a = b;
    b = aux;
}

inline void Push(int node){
    while(node >> 1 && A[Heap[node]] < A[Heap[node >> 1]]){
        Pos[Heap[node]] = node >> 1;
        Pos[Heap[node >> 1]] = node;
        Swap(Heap[node], Heap[node >> 1]);
        node >>= 1;
    }
}

inline void Pop(int node){
    int y = 0;
    while(node != y){
        y = node;
        if(y << 1 <= L && A[Heap[node]] > A[Heap[y << 1]]) node = y << 1;
        if((y << 1) + 1 <= L && A[Heap[node]] > A[Heap[(y << 1) + 1]]) node = (y << 1) + 1;
        Pos[Heap[node]] = y;
        Pos[Heap[y]] = node;
        Swap(Heap[node], Heap[y]);
    }
}

int main(){
    int n, type, x;
    fin >> n;
    for(int i = 1; i <= n; i++){
        fin >> type;
        if(type == 1){
            fin >> x;
            A[++NR] = x;
            Heap[++L] = NR;
            Pos[NR] = L;
            Push(L);
        }
        if(type == 2){
            fin >> x;
            A[x] = -1;
            Push(Pos[x]);
            Pos[Heap[1]] = 0;
            Heap[1] = Heap[L--];
            Pos[Heap[1]] = 1;
            if(L > 1){
                Pop(1);
            }
        }
        if(type == 3) fout << A[Heap[1]] << "\n";
    }
    return 0;
}