Cod sursa(job #2603237)

Utilizator bem.andreiIceman bem.andrei Data 18 aprilie 2020 19:30:41
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <bits/stdc++.h>

using namespace std;
ifstream r("heapuri.in");
ofstream w("heapuri.out");
int g[200002], ans=1, cnt=1;
struct heap{
    int val, pos;
}v[200002];
void push(int x){
    int c=cnt;
    cnt++;
    v[c].val=x;
    v[c].pos=ans;
    while(v[c].val<v[c/2].val){
        swap(v[c], v[c/2]);
        g[v[c].pos]=c;
        g[v[c/2].pos]=c/2;
        c/=2;
    }
    g[ans]=c;
    ans++;
}
void pop(int poz){
    int c=cnt-1;
    swap(v[poz], v[c]);
    v[c].val=v[c].pos=0;
    while(v[poz].val>v[poz*2].val && v[poz*2].val!=0){
        swap(v[poz], v[poz*2]);
        g[v[poz].pos]=c;
        g[v[poz*2].pos]=c*2;
        poz*=2;
    }
    cnt--;
}
int main()
{
    int n;
    r>>n;
    for(int i=0;i<n;i++){
        int x;
        r>>x;
        if(x==1){
            int val;
            r>>val;
            push(val);
        }
        else if(x==2){
            int val;
            r>>val;
            pop(g[val]);
        }
        else{
            w<<v[1].val<<"\n";
        }
    }
    return 0;
}