Pagini recente » Cod sursa (job #1176515) | Cod sursa (job #74386) | Cod sursa (job #2713581) | Cod sursa (job #2280842) | Cod sursa (job #847371)
Cod sursa(job #847371)
#include<fstream>
#define NMAX 200010
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct nod
{
int val, o;
}heap[NMAX];
int op, x, n, loc[NMAX], m, i;
void Insert(int pz)
{
int tata=pz/2;
if (tata)
if (heap[tata].val>heap[pz].val)
{
swap(loc[heap[tata].o], loc[heap[pz].o]);
swap(heap[tata], heap[pz]);
Insert(tata);
}
}
void Erase(int tata)
{
int f1=tata*2, f2=tata*2+1, fmn=tata;
nod mn=heap[tata];
if (f1<=m && heap[f1].val<mn.val)
{
fmn=f1; mn=heap[f1];
}
if (f2<=m && heap[f2].val<mn.val)
{
fmn=f2; mn=heap[f2];
}
if (fmn!=tata)
{
swap(loc[heap[tata].o], loc[heap[fmn].o]);
swap(heap[tata], heap[fmn]);
Erase(fmn);
}
}
int main()
{
f>>n>>op>>x;
heap[1].val=x; heap[1].o=1; loc[1]=1; i=1; m=1;
while (--n)
{
f>>op;
if (op==3) g<<heap[1].val<<"\n";
else
{
f>>x;
if (op==1)
{
++i;
heap[++m].val=x;
heap[m].o=i;
loc[i]=m;
Insert(m);
}
else
{
loc[heap[m].o]=loc[x];
heap[loc[x]]=heap[m--];
Erase(loc[x]);
}
}
}
f.close();
g.close();
return 0;
}