Pagini recente » Cod sursa (job #3207076) | Cod sursa (job #1171237) | Cod sursa (job #262521) | Cod sursa (job #589746) | Cod sursa (job #793906)
Cod sursa(job #793906)
#include<fstream>
#include<algorithm>
#define N 200010
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int heap[N],v[N],poz[N];
void down(int n, int nod)
{
int son;
do
{
son=0;
if(nod*2<=n)
{
son=nod*2;
if (nod*2+1<=n && heap[nod*2+1]<heap[nod*2])son=nod*2+1;
if (heap[son]>=heap[nod])son=0;
}
if(son)
{
swap(heap[nod], heap[son]);
poz[heap[son]]=son;
poz[heap[nod]]=nod;
nod=son;
}
} while (son);
}
void up(int n, int nod) {
int key=heap[nod];
while (nod>1 && key<heap[nod/2]) {
heap[nod]=heap[nod/2];
poz[heap[nod]]=nod;
nod/=2;
}
heap[nod]=key;
poz[key]=nod;
}
int main()
{
int nr,i,elem,n=0,x;
short op;
in>>nr;
for(i=0;i<nr;i++)
{
in>>op;
if(op!=3)in>>elem;
else out<<heap[1]<<"\n";
if(op==1)
{
heap[++n]=elem;
poz[heap[n]]=n;
v[n]=elem;
up(n,n);
}
if(op==2)
{
x=poz[v[elem]];
heap[x]=heap[n];
--n;
if (x>1 && heap[x]>heap[x/2]) up(n, x);
else down(n, x);
}
}
}