Pagini recente » Cod sursa (job #1599389) | Cod sursa (job #1060961) | Cod sursa (job #1714070) | Cod sursa (job #843198) | Cod sursa (job #2680718)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct Heap
{
int val,crono;
};
Heap H[200001];
int poz[200001];
int dim;
int father(int K)
{
return K/2;
}
int left_son(int K)
{
return K*2;
}
int rigth_son(int K)
{
return K*2+1;
}
int maxim()
{
return H[1].val;
}
void sift(int n, int K)
{
int son;
do
{
son=0;
if(left_son(K)<=n)
{
son=left_son(K);
if(rigth_son(K)<=n && H[rigth_son(K)].val<H[son].val)
{
son=rigth_son(K);
}
}
if(H[son].val>=H[K].val)
{
son=0;
}
if(son)
{
swap(poz[H[K].crono],poz[H[son].crono]);
swap(H[K],H[son]);
K=son;
}
}while(son);
}
void percolate(int n, int K)
{
Heap key = H[K];
while(K>1 && (key.val<H[father(K)].val))
{
poz[H[father(K)].crono]=K;
H[K]=H[father(K)];
K=father(K);
}
H[K]=key;
poz[H[K].crono]=K;
}
void inserare(int &n, int key)
{
n++;
H[n].val = key;
H[n].crono=++dim;
poz[dim]=n;
percolate(n,n);
}
void cut(int &n, int K)
{
poz[H[n].crono]=K;
H[K]=H[n];
n--;
if(K>1 && H[K].val<H[father(K)].val)
{
percolate(n,K);
}
else
{
sift(n,K);
}
}
int main()
{
int N,x,val,n=0;
fin>>N;
for(int i=1; i<=N; i++)
{
fin>>x;
if(x == 3)
{
fout<<maxim()<<'\n';
}
else
{
if(x == 1)
{
fin>>val;
inserare(n,val);
}
else
{
if(x == 2)
{
fin>>val;
cut(n,poz[val]);
}
}
}
}
return 0;
}