Cod sursa(job #3163332)

Utilizator brianabucur11Briana Bucur brianabucur11 Data 31 octombrie 2023 11:49:27
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

pair <int,int> v[200005];
int n, x, a, k, t, poz[200005];

void add (int x)
{
    v[++k].first=x;
    t++;
    v[k].second=t;
    poz[v[k].second]=k;
    int m=k;
    while (v[m/2].first>v[m].first && m>=1 && m<=k)
    {
        swap(v[m/2],v[m]);
        swap(poz[v[m/2].second],poz[v[m].second]);
        m/=2;
    }
}

void del (int x)
{
    swap(v[poz[x]],v[k]);
    int m=poz[x];
    poz[v[poz[x]].second]=poz[v[k].second];
    k--;
    while (v[m].first>v[m*2].first && m>=1 && m*2<=k)
    {
        swap(v[m],v[m*2]);
        swap(poz[v[m].second],poz[v[m*2].second]);
        m*=2;
    }
    while (v[m].first>v[m*2+1].first && m>=1 && m*2+1<=k)
    {
        swap(v[m],v[m*2+1]);
        swap(poz[v[m].second],poz[v[m*2+1].second]);
        m*=2;
        m++;
    }
    while (v[m/2].first>v[m].first && m>=1 && m<=k)
    {
        swap(v[m/2],v[m]);
        swap(poz[v[m/2].second],poz[v[m].second]);
        m/=2;
    }
}

int main()
{
    fin >> n;
    for (int i=1; i<=n; i++)
    {
        fin >> x;
        if (x==3)
            fout << v[1].first << '\n';
        else
        {
            fin >> a;
            if (x==1)
                add(a);
            else
                del(a);
        }

    }
    return 0;
}