Cod sursa(job #2610438)

Utilizator nicu_ducalNicu Ducal nicu_ducal Data 4 mai 2020 21:14:35
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.49 kb
/******** Ordered Set ********
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; */

#include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << "\n"
#define all(x) (x).begin(),(x).end()
#define len length()
#define sz size()
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>

using ull = unsigned long long;
using ll = long long;
using namespace std;

const int INF = INT_MAX;
ll t, n, q, num;
vector<pii> heap;
int order[2000005], curr = 1;

void heapifyUp(int idx){
    if (idx == 0)
        return;
    if (heap[idx].fi < heap[idx / 2].fi){
        swap(order[heap[idx].se], order[heap[idx / 2].se]);
        swap(heap[idx], heap[idx / 2]);
        heapifyUp(idx / 2);
    }
}

void heapifyDown(int idx){
    if (idx >= (heap.size() - 1) / 2)
        return;
    int mi;
    if (2 * idx + 2 > heap.size() - 1)
        mi = heap[2 * idx + 1].fi;
    else
        mi = min(heap[2 * idx + 1].fi, heap[2 * idx + 2].fi);
    if (heap[idx].fi > mi){
        if (heap[2 * idx + 1].fi == mi){
            swap(order[heap[idx].se], order[heap[2 * idx + 1].se]);
            swap(heap[idx], heap[2 * idx + 1]);
            heapifyDown(2 * idx + 1);
        } else if (2 * idx + 2 <= heap.size() - 1 and mi == heap[2 * idx + 2].fi) {
            swap(order[heap[idx].se], order[heap[2 * idx + 2].se]);
            swap(heap[idx], heap[2 * idx + 2]);
            heapifyDown(2 * idx + 2);
        }
    }
}

void insert(int num){
    heap.emplace_back(num, curr);
    order[curr++] = heap.size() - 1;
    heapifyUp(heap.size() - 1);
}

void pop(int idx){
    swap(order[heap[idx].se], order[heap[heap.size() - 1].se]);
    swap(heap[idx], heap[heap.size() - 1]);
    heap.pop_back();
    heapifyUp(idx);
    heapifyDown(idx);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(); cout.tie();
    ifstream cin("heapuri.in");
    ofstream cout("heapuri.out");

    cin >> n;
    while (n--){
        cin >> q;
        switch (q) {
            case 1:
                cin >> num;
                insert(num);
                break;
            case 2:
                cin >> num;
                pop(order[num]);
                break;
            case 3:
                cout << heap[0].fi << '\n';
                break;
        }
    }

    return 0;
}