Pagini recente » Cod sursa (job #4040) | Cod sursa (job #1386200) | Cod sursa (job #132526) | Cod sursa (job #1302999) | Cod sursa (job #1973831)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define nmax 200005
int v[nmax], heap[nmax], l, poz[nmax], n;
void heapUp(int k)
{
while(k/2 && v[heap[k/2]]>v[heap[k]])
{
swap(heap[k/2], heap[k]);
poz[heap[k/2]]=k/2;
poz[heap[k]]=k;
k/=2;
}
}
void heapDown(int k)
{
while((k<=2*l && v[heap[k]]>v[heap[2*k]]) || (k<=2*l+1 && v[heap[k]]>v[heap[2*k+1]]))
{
if(v[heap[2*k]]<v[heap[2*k+1]] || 2*k+1>l)
{
swap(heap[k], heap[2*k]);
poz[heap[k]]=k;
poz[heap[2*k]]=2*k;
k=2*k;
}
else
{
swap(heap[k], heap[2*k+1]);
poz[heap[k]]=k;
poz[heap[2*k+1]]=2*k+1;
k=2*k+1;
}
}
}
void sterge(int x)
{
int k;
k=poz[x];
heap[k]=heap[l];
l--;
if(k/2 && v[heap[k/2]]>v[heap[k]])
heapUp(k);
else heapDown(k);
}
void add(int x)
{
l++;
n++;
v[n]=x;
heap[l]=n;
poz[n]=l;
heapUp(l);
}
int main()
{
int n, op, x;
fin>>n;
while(n)
{
n--;
fin>>op;
if(op==1)
{
fin>>x;
add(x);
}
else if(op==2)
{
fin>>x;
sterge(x);
}
else if(op==3)
fout<<v[heap[1]]<<'\n';
}
fin.close();
fout.close();
return 0;
}