Cod sursa(job #2412768)

Utilizator CybotStancila Ionut-Marian Cybot Data 22 aprilie 2019 15:34:18
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.43 kb
/*#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n, c, x, k=0, pozitie=0, poz[200005];

struct Vector
{
    int pozitie;
    int heap;
}h[200005];

void hcomb(int i, int l)
{
    int v=h[i].heap, tata=i, fiu=2*tata;
    while (fiu<=l)
    {
        if (fiu<l)
        if (h[fiu].heap>h[fiu+1].heap) fiu++;
        if (v>h[fiu].heap)
        {
            h[tata].heap=h[fiu].heap;
            tata=fiu;
            fiu=2*fiu;
        }
        else fiu=l+1;
    }
    h[tata].heap=v;
}

void hins (int nr)
{
    int fiu=++k, tata=k/2;
    while (tata && h[tata].heap>nr)
    {
        h[fiu].heap=h[tata].heap;
        fiu=tata;
        tata=tata/2;
    }
    h[fiu].heap=nr;
}

void heliminare(int element)
{
    h[element]=h[k--];
    hcomb(element, k);
}

int main()
{
    f>>n;
    for (int i=1;i<=n;i++)
    {
        f>>c;
        if (c==1)
        {
            f>>x;
            hins(x);
            poz[++pozitie]=x;
        }
        else if (c==2)
        {
            f>>x;
            int pozitie=cautare(poz[x]);
            heliminare(pozitie);
        }
        else g<<h[1]<<'\n';
    }
    return 0;
}
*/
#include <bits/stdc++.h>

using namespace std;

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

const int N=200010;

int val[N],h[N],pos[N],op,q,x,NR,L,poz;
void heap_swap(int ,int),heap_up(int ),heap_down(int);

int main()
{
    fin>>q;
    for(;q;q--)
    {
        fin>>op;
        if(op==1)
        {
            fin>>val[++NR];
            h[++L]=NR;
            pos[NR]=L;
            heap_up(L);

        }
        if(op==2)
        {
            fin>>x;
            poz=pos[x];
            heap_swap(poz,L);
            L--;
            heap_up(poz);
            heap_down(poz);
        }
        if(op==3)
        {
            fout<<val[h[1]]<<'\n';
        }
    }
    return 0;
}

void heap_swap(int a,int b)
{
    swap(h[a],h[b]);
    pos[h[a]]=a;
    pos[h[b]]=b;
}

void heap_down(int p)
{
    int fiu=2*p;
    if(fiu>L)return;
    if(fiu<L)if(val[h[fiu]]>val[h[fiu+1]])fiu++;
    if(val[h[p]]>val[h[fiu]])
    {
        heap_swap(p,fiu);
        heap_down(fiu);
    }
}

void heap_up(int p)
{
    int tata=p/2;
    if(!tata)return;
    if(val[h[p]]<val[h[tata]])
    {
        heap_swap(p,tata);
        heap_up(tata);
    }
}