Pagini recente » Cod sursa (job #77887) | Cod sursa (job #1982677) | Cod sursa (job #2142765) | Cod sursa (job #2617318) | Cod sursa (job #2617708)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct nod
{
int x,y;
};
vector<nod> heap;
int parinte(int x)
{
return (x-1)/2;
}
int fiu_st(int x)
{
return 2*(x+1)-1;
}
int fiu_dr(int x)
{
return 2*(x+1);
}
void insrt(nod n)
{
heap.push_back(n);
int i=heap.size();
heap[i-1].x=i;
i--;
while(i>0&&heap[parinte(i)].y>heap[i].y)
{
nod aux=heap[i];
heap[i]=heap[parinte(i)];
heap[parinte(i)]=aux;
i=parinte(i);
}
}
void heapify(int i)
{
int l = fiu_st(i);
int r = fiu_dr(i);
if (l < heap.size() && heap[l].y < heap[i].y)
{
nod aux=heap[i];
heap[i]=heap[l];
heap[l]=aux;
heapify(l);
}
if (r < heap.size() && (heap[r].y < heap[i].y&&heap[r].y<heap[l].y))
{
nod aux=heap[i];
heap[i]=heap[r];
heap[r]=aux;
heapify(r);
}
}
nod del_min()
{
nod rez;
if(heap.size()==1)
{
rez=heap[0];
heap.erase(heap.begin());
return rez;
}
rez = heap[0];
heap[0] = heap[heap.size()-1];
heap.erase(heap.end()-1);
heapify(0);
return rez;
}
void del(int i)
{
heap[i].y = INT_MIN;
while (i != 0 && heap[parinte(i)].y > heap[i].y)
{
nod aux=heap[i];
heap[i]=heap[parinte(i)];
heap[parinte(i)]=aux;
i = parinte(i);
}
del_min();
}
int main()
{
int n;
fin>>n;
int i=0;
while(n--)
{
int op;
fin>>op;
if(op==1)
{
nod x;
fin>>x.y;
insrt(x);
}
else if(op==2)
{
int x;
fin>>x;
int i=0;
while(x!=heap[i].x)
i++;
del(i);
}
else
{
fout<<heap[0].y<<"\n";
}
}
return 0;
}