Pagini recente » Cod sursa (job #1104483) | Cod sursa (job #3031319) | Cod sursa (job #1040710) | Cod sursa (job #2372351) | Cod sursa (job #1042490)
#include<fstream>
using namespace std;
const int MAXN=200005;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct node{int value,cron;};
int n,heapsize,elem;
int poz[MAXN];
node h[MAXN];
int parent(int i){return (i>>1);}
int left(int i){return (i<<1);}
int right(int i){return ((i<<1)+1);}
void swap(int& a,int& b)
{
int aux=a;
a=b; b=aux;
}
void swap(node& a, node& b)
{
node aux=a;
a=b; b=aux;
}
void up_heap(int idx)
{
while (h[idx].value<h[parent(idx)].value && idx>1)
{
swap(poz[h[idx].cron],poz[h[parent(idx)].cron]);
swap(h[idx],h[parent(idx)]);
idx=parent(idx);
}
}
void down_heap(int i)
{
int smallest=i,l=left(i),r=right(i);
if (l<=heapsize && h[l].value<h[smallest].value)
smallest=l;
if (r<=heapsize && h[r].value<h[smallest].value)
smallest=r;
if (smallest!=i)
{
swap(poz[h[smallest].cron],poz[h[i].cron]);
swap(h[smallest],h[i]);
down_heap(smallest);
}
}
void insert(int x,int cron)
{
++heapsize;
h[heapsize].value=x;
h[heapsize].cron=cron;
poz[cron]=heapsize;
up_heap(heapsize);
}
void del(int cron)
{
int position=poz[cron];
poz[h[heapsize].cron]=position;
swap(h[heapsize],h[position]);
poz[h[heapsize].cron]=-1;
h[heapsize].value=h[heapsize].cron=0;
--heapsize;
down_heap(position);
}
int main()
{
fin>>n;
int i,opt,x,cron;
for (i=1; i<=n; ++i)
{
fin>>opt;
if (opt==1)
{
++elem;
fin>>x;
insert(x,elem);
}
if (opt==2)
{
fin>>cron;
del(cron);
}
else if (opt==3)
{
fout<<h[1].value<<'\n';
}
}
fin.close();
fout.close();
return 0;
}