Pagini recente » Cod sursa (job #207600) | Cod sursa (job #1084976) | Cod sursa (job #2533744) | Cod sursa (job #2047946) | Cod sursa (job #713488)
Cod sursa(job #713488)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int heap[2000001], ord[2000001], m, n, k;
void urcare(int poz);
void coborare(int poz);
void adaugare(int val)
{
heap[++n]=ord[++k]=val;
urcare(n);
}
int cautare(int val, int i)
{
for(i=1; i<=n; i++)
if(heap[i]==val) return i;
return -1;
}
void stergere (int poz)
{
heap[poz]=heap[n--];
if(heap[poz>>1] > heap[poz])
urcare(poz);
else coborare(poz);
}
void urcare(int poz)
{
int temp=heap[poz], i=poz;
while(heap[i>>1] > temp && i>>1)
heap[i]=heap[i>>1], i=i>>1;
heap[i]=temp;
}
void coborare(int poz)
{
bool ok=true;
while(poz<<1<n && ok)
{
ok=false;
int Min=(heap[poz<<1]<heap[(poz<<1)+1] ? poz<<1 : (poz<<1)+1);
if(heap[Min]<heap[poz])
{
int temp=heap[poz], i=(poz<<1==Min ? poz<<1 : (poz<<1)+1);
heap[poz]=heap[i];
heap[i]=temp;
coborare(i);
ok=true;
}
}
if(poz<<1==n && heap[poz<<1]<heap[poz])
{
int temp=heap[poz];
heap[poz]=heap[poz<<1];
heap[poz<<1]=temp;
}
}
void meniu()
{
int x;
in>>m;
while(m)
{
m--;
in>>x;
switch(x)
{
case 1: in>>x;
adaugare(x);
break;
case 2: in>>x;
stergere(cautare(ord[x],1));
break;
case 3: out<<heap[1]<<"\n";
}
}
}
int main()
{
meniu();
return 0;
}