Cod sursa(job #2890026)

Utilizator BluThund3rRadu George BluThund3r Data 14 aprilie 2022 10:06:33
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <vector>
#include <unordered_map>

using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");

int n, op, ord = 0, pos, x;
vector<pair<int, int>> v;
unordered_map<int, int> add_time;

void solve1(){
    in >> x;
    v.push_back({x, ++ ord});
    int posx = (int)(v.size() - 1);
    add_time[ord] = posx;
    while(posx > 0 && v[posx / 2].first > v[posx].first){
        swap(v[posx], v[posx / 2]);
        swap(add_time[v[posx].second], add_time[v[posx / 2].second]);
        posx /= 2;
    }
}

void solve2(){
    in >> pos;
    pos = add_time[pos];
    swap(v[pos], v[v.size() - 1]);
    swap(add_time[v[pos].second], add_time[v[v.size() - 1].second]);
    v.pop_back();
    add_time.erase(v[v.size() - 1].second);
    while(pos * 2 < (int)v.size()){
        int child_pos = 2 * pos;
        if(child_pos + 1 < (int)v.size())
            child_pos = (v[child_pos + 1].first < v[child_pos].first)? child_pos + 1 : child_pos;
        if(v[pos].first >= v[child_pos].first) {
            swap(v[pos], v[child_pos]);
            swap(add_time[v[pos].second], add_time[v[child_pos].second]);
            pos = child_pos;
        }
        else
            break;
    }
}

void solve3(){
    out << v[0].first << '\n';
}

int main() {
    in >> n;
    while(n --){
        in >> op;
        if(op == 1)
            solve1();
        else if(op == 2)
            solve2();
        else
            solve3();
    }


    return 0;
}