Cod sursa(job #2890044)

Utilizator BluThund3rRadu George BluThund3r Data 14 aprilie 2022 10:54:15
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.79 kb
#include <fstream>
#include <vector>
#include <iostream>
#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]);
    add_time.erase(v[v.size() - 1].second);
    v.pop_back();
    while(pos * 2 < (int)v.size() && v.size() > 1){
        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';
}

//void test(){
//    ifstream in1("grader_test5.ok");
//    ifstream in2("heapuri.out");
//    int line = 0;
//    int x, y;
//    while(in1 >> x){
//        in2 >> y;
//        ++ line;
//        if(x != y){
//            cout << "NU " << line;
//        }
//    }
//    cout << "\nDA";
//}

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

    return 0;
}