Cod sursa(job #1424324)

Utilizator GinguIonutGinguIonut GinguIonut Data 23 aprilie 2015 23:16:23
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#define dim 200001
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[dim],poz[dim],v[dim],i,n,x,k1,y,pozi,nr;
int update(int k)
{
    while(k/2>0)
    {
        if(v[heap[k]]<v[heap[k/2]])
        {
            swap(poz[heap[k]],poz[heap[k/2]]);
            swap(heap[k],heap[k/2]);
            k/=2;
        }
        else
            return 0;
    }
    return 0;
}
void downdate(int k)
{
    while(2*k<=k1)
    {
        int po=2*k;
        if(po+1<=k1&&v[heap[po]]>v[heap[po+1]])
            po++;
        if(v[heap[po]]<v[heap[k]])
        {
            swap(poz[heap[k]],poz[heap[po]]);
            swap(heap[k],heap[po]);
            k=po;
        }
        else
            break;
    }
}
int main()
{
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>x;
        if(x==1)
        {
            fin>>v[++nr];
            heap[++k1]=nr;
            poz[nr]=k1;
            update(k1);
            continue;
        }
        if(x==2)
        {
            fin>>y;
            heap[poz[y]]=heap[k1];
            poz[heap[k1]]=poz[y];
            k1--;
            downdate(poz[y]);
            continue;
        }
        if(x==3)
            fout<<v[heap[1]]<<'\n';
    }
    return 0;
}