Pagini recente » Cod sursa (job #2971654) | Cod sursa (job #498199) | Cod sursa (job #2906778) | Cod sursa (job #2124318) | Cod sursa (job #949715)
Cod sursa(job #949715)
#include<cstdio>
typedef int Heap[200010];
inline void swp(int &x,int &y) { int k = x; x = y; y = k; }
Heap H;
int order[200010] = {0};
void sift (Heap H,int N,int K)
{
int son;
do
{
son = 0;
if ((2*K) <= N)
{
son = 2*K;
if ( ( (2*K+1) <= N) && (H[2*K] > H[2*K+1]) )
son = 2*K + 1;
if (H[son] >= H[K])
son = 0;
}
if (son)
swp(H[K],H[son]);
}
while (son);
}
void percolate (Heap H,int N,int K)
{
int key = H[K];
while ( (K>1) && (key < H[K/2]) )
{
H[K] = H[K/2];
K = K/2;
}
H[K] = key;
}
void add (Heap H,int &N,int K)
{
H[++N] = K;
percolate(H,N,K);
}
void cut (Heap H,int &N,int K)
{
H[K] = H[N];
--N;
if ( (K>1) && (H[K] < H[K/2]))
percolate(H,N,K);
else sift(H,N,K);
}
int main()
{
int id = 0;
int N = 0,t,x,sch;
FILE *f,*g;
f = fopen("heapuri.in","r");
g = fopen("heapuri.out","w");
fscanf(f,"%d",&t);
for (int i=1;i<=t;i++)
{
fscanf(f,"%d",&x);
if (x<=2) fscanf(f,"%d",&sch);
switch (x)
{
case 1: { add (H,N,sch); order[++id] = sch; break; }
case 2: { cut (H,N,order[sch]); break; }
case 3: { fprintf(g,"%d\n",H[1]); break; }
}
}
fclose(f);
fclose(g);
return 0;
}