Cod sursa(job #2894862)

Utilizator RaduAntoneoAntonio Alexandru Radu RaduAntoneo Data 28 aprilie 2022 15:08:03
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
using namespace std;
 
vector<int> heap;
int pozitii[200001];
unordered_set<int> de_sters; 

void urca(int poz) {
    while(poz) {
        int tata = (poz - 1) / 2;
        if(heap[tata] > heap[poz]) {
            swap(heap[tata], heap[poz]);
            poz = tata;
        }
        else break;
    }
}

void adauga(int x) {
    heap.push_back(x);
    urca(heap.size() - 1);
}
 
void coboara(int poz) {
    int st = poz * 2 + 1;
    int dr = st + 1;
    if(st >= heap.size())
        return;
    if(heap[st] < heap[dr]) {
        if(heap[st] < heap[poz]) {
            swap(heap[poz], heap[st]);
            coboara(st);
        }
    } 
    else if(heap[dr] < heap[poz]) {
        swap(heap[poz], heap[dr]);
        coboara(dr);
        }
    return;
}

ifstream f("heapuri.in");
ofstream g("heapuri.out");
#define cin f
#define cout g
 
int main() {
    int t, op, nr, cnt = 1;
    cin >> t;
    while(t--) {
        cin >> op;
        if(op == 3) {
            while(de_sters.find(heap[0]) != de_sters.end()) {
                de_sters.erase(heap[0]);
                heap[0] = heap[heap.size() - 1];
                heap.pop_back();
                coboara(0);
            }
            cout << heap[0] << '\n';
        }
        else {
            cin >> nr;
            if(op == 1) {adauga(nr); pozitii[cnt++] = nr;}
            else de_sters.insert(pozitii[nr]);
        }
    }
}