Cod sursa(job #1812935)

Utilizator geo_furduifurdui geo geo_furdui Data 22 noiembrie 2016 16:10:04
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>

using namespace std;
ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");
long long n,m,heap[200100],heapz[210000],l,frecv[200010];
void climb (int poz)
{ long long w=heap[poz],w1=heapz[poz];
    while(3>2)
    {
        if( heap[poz/2]>w && poz>1 )
            frecv[heapz[poz/2]]=poz,heap[poz]=heap[poz/2],heapz[poz]=heapz[poz/2],poz/=2;
        else
        {
            heap[poz]=w; heapz[poz]=w1; frecv[w1]=poz; break;
        }
    }
}
void down (int poz)
{ long long y,w=heap[poz],w1=heapz[poz];
    while(3>2)
    {
        if(poz/2+1<=m)
        {
            if(heap[poz*2]<heap[poz*2+1]) y=poz*2; else y=poz*2+1;
        } else y=poz*2;
        if (y<=m && w>heap[y])
        {
            frecv[heapz[y]]=poz; heap[poz]=heap[y]; heapz[poz]=heapz[y]; poz=y;
        }
        else
        {
            heap[poz]=w; heapz[poz]=w1; frecv[w1]=poz; break;
        }
    }
}
void read ()
{ long long p,x;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>p;
        if(p==1)
        { cin>>x;
            heap[++m]=x; l++; heapz[m]=l; frecv[l]=m;
            climb(m);
        }
        else if(p==2)
        {
            cin>>x;
            heap[frecv[x]]=heap[m];  frecv[heapz[m]]=frecv[x]; heapz[frecv[x]]=heapz[m];   m--;
            climb(frecv[x]);
            down(frecv[x]);
        }
        else cout<<heap[1]<<"\n";
    }
}
int main()
{
    read();
    cin.close();
    cout.close();
    return 0;
}