Pagini recente » Cod sursa (job #1401920) | Cod sursa (job #3235791) | Cod sursa (job #1247059) | Cod sursa (job #2541657) | Cod sursa (job #1262171)
#include<fstream>
#define NMAX 200010
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct nod
{
int val, moment;
}heap[NMAX];
int n, x, op, nrNod, pozitie[NMAX];
inline int tata(int nod)
{
if (nod%2==0) return nod/2-1;
return nod/2;
}
void insereaza(int nod, int moment)
{
int t;
heap[nrNod].val=nod; heap[nrNod].moment=moment; pozitie[moment]=nrNod; ++nrNod;
nod=nrNod-1; t=tata(nod);
while (nod!=0 && heap[t].val>heap[nod].val)
{
swap(pozitie[heap[t].moment], pozitie[heap[nod].moment]);
swap(heap[t], heap[nod]);
nod=t; t=tata(nod);
}
}
int valMinFii(int nod, int nrNod)
{
int f1=nod*2+1, f2=nod*2+2;
if (f2<nrNod)
if (heap[f1].val>heap[f2].val) return heap[f2].val;
if (f1<nrNod)
return heap[f1].val;
return heap[nod].val+1;
}
void sterge(int moment)
{
int f1, f2, nxt, nod=pozitie[moment];
pozitie[heap[nrNod-1].moment]=nod;
heap[nod]=heap[nrNod-1]; --nrNod;
while (nod*2+1<nrNod && heap[nod].val>valMinFii(nod, nrNod))
{
f1=nod*2+1; f2=nod*2+2;
if (f2<nrNod)
{
if (heap[f1].val>heap[f2].val) nxt=f2;
else nxt=f1;
swap(pozitie[heap[nod].moment], pozitie[heap[nxt].moment]);
swap(heap[nod], heap[nxt]);
nod=nxt;
}
else
if (heap[nod].val>heap[f1].val)
{
swap(pozitie[heap[nod].moment], pozitie[heap[f1].moment]);
swap(heap[nod], heap[f1]);
nod=f1;
}
}
}
int main()
{
f>>n;
for (int i=1; i<=n; ++i)
{
f>>op;
if (op==3) g<<heap[0].val<<"\n";
else
{
f>>x;
if (op==2) sterge(x);
else insereaza(x, i);
}
}
f.close();
g.close();
return 0;
}