Pagini recente » Cod sursa (job #1350945) | Cod sursa (job #1726048) | Cod sursa (job #3318479) | Cod sursa (job #1969612) | Cod sursa (job #1899726)
#include <iostream>
#include <fstream>
#define nmax 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int a[nmax], pos[nmax];
int n, in;
int father(int nod)
{
return nod/2;
}
int left_son(int nod)
{
return 2*nod;
}
int right_son(int nod)
{
return 2*nod+1;
}
void shift(int k)
{
int son;
do
{
son=0;
if(left_son(k)<=n)
{
son=left_son(k);
if(right_son(k)<=n && a[son]>a[right_son(k)])
{
son=right_son(k);
}
if(a[k]<=a[son])
{
son=0;
}
}
if(son)
{
swap(a[k], a[son]);
k=son;
}
}
while(son);
}
void percolate(int k)
{
int key=a[k];
while(k>1 && key<a[father(k)])
{
a[k]=a[father(k)];
k=father(k);
}
a[k]=key;
}
void cut(int k)
{
a[k]=a[n];
--n;
if(k>1 && a[k]>a[father(k)])
{
percolate(k);
}
else
{
shift(k);
}
}
void insereaza(int k)
{
a[++n]=k;
percolate(n);
}
void citire()
{
int p, k, t;
f>>p;
while(p>0)
{
f>>t;
if(t==1)
{
f>>k;
insereaza(k);
pos[++in]=k;
}
else
{
if(t==2)
{
f>>k;
for(int i=1; i<=n; ++i)
{
if(a[i]==pos[k])
{
cut(i);
}
}
pos[k]=0;
}
else g<<a[1]<<"\n";
}
p--;
}
}
int main()
{
citire();
return 0;
}