Cod sursa(job #2418404)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 4 mai 2019 20:56:05
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include<cstdio>
#define N 200001
#define M 10000000
int x,y,z,t,n,r,j,p[N],v[N],h[N],e=-1,l;
char q[M];
inline int B()
{
  	int s=0;
  	for(e++;q[e]>='0'&&q[e]<='9';e++)
  		s=s*10+q[e]-'0';
  	return s;
}
inline void S(int x)
{
    int i,d=x>999999999?10:x>99999999?9:x>9999999?8:x>999999?7:x>99999?6:x>9999?5:x>999?4:x>99?3:x>9?2:1;
    for(i=d-1;i>=0;x/=10,i--)
        q[l+i]=x%10+48;
    q[l+d]=10,l+=d+1;
}
inline void A(int t)
{
	for(;t>1&&v[h[t]]<v[h[t>>1]];h[t]^=h[t>>1]^=h[t]^=h[t>>1],p[h[t]]=t,p[h[t>>1]]=t>>1,t>>=1);
}
int main()
{
	freopen("heapuri.in","r",stdin),freopen("heapuri.out","w",stdout),fread(q,1,M,stdin),n=B();
	while(n--)
	{
		z=B();
      	if(z==3)
            S(v[h[1]]);
      	else
		{
		  	r=B();
            if(z==1)
                v[++t]=r,h[++j]=t,p[t]=j,A(j);
            else
                for(v[r]=-1,A(p[r]),p[h[1]]=y=0,h[1]=h[j--],p[h[1]]=x=1;x!=y;z=h[x],h[x]=h[y],h[y]=z,p[h[x]]=x,p[h[y]]=y)
				{
					y=x;
                    if((y<<1)<=j&&v[h[x]]>v[h[y<<1]])
                        x=y<<1;
                    if((y<<1)<j&&v[h[x]]>v[h[1+(y<<1)]])
                        x=(y<<1)+1;
				}
		}
	}
	fwrite(q,1,l,stdout);
}