Pagini recente » Cod sursa (job #1703778) | Cod sursa (job #390128) | Cod sursa (job #2950547) | Cod sursa (job #535988) | Cod sursa (job #392420)
Cod sursa(job #392420)
#include<fstream>
#define NMAX 200003
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
typedef int heap[NMAX];
heap H;
int n,i,no,c,v,a[NMAX],an,poz[NMAX],j;
inline void swap(int &x, int &y)
{int k;
k=x;x=y;y=k;}
inline int father(int k)
{
return k/2;}
inline int l_son(int k)
{
return k*2;}
inline int r_son(int k)
{
return k*2+1;}
void sift(int n,int k)
{int son;
do
{
son=0;
if (l_son(k)<=n)
{
son=l_son(k);
if (r_son(k)<=n && a[H[r_son(k)]]<a[H[l_son(k)]])
son=r_son(k);
if (a[H[son]]>=a[H[k]])
son=0;
}
if (son)
{
swap(poz[H[k]],poz[H[son]]);
swap(H[k],H[son]);
k=son;
}
} while (son);
}
void percolate(int n,int k)
{
while (k>1&&a[H[k]]<a[H[father(k)]])
{
swap(poz[H[k]],poz[H[father(k)]]);
swap(H[k],H[father(k)]);
k=father(k);
}
}
void del(int &n, int k)
{
H[k]=H[n];
n--;
if (k>1&&a[H[k]]>a[H[father(k)]])
percolate(n,k);
else
sift(n,k);
}
void insert(int &n,int k)
{
n++;
H[n]=k;
poz[k]=n;
percolate(n,n);
}
int main()
{
fin>>no;
for (j=1;j<=no;j++)
{
fin>>c;
if (c==1)
{
an++;
fin>>a[an];
insert(n,an);
}
else if (c==2)
{
fin>>v;
del(n,poz[v]);
}
else if (c==3)
fout<<a[H[1]]<<'\n';
}
fout.close();
return 0;}