Pagini recente » Cod sursa (job #1195240) | Cod sursa (job #1775439) | Cod sursa (job #1209154) | Cod sursa (job #2842307) | Cod sursa (job #2363168)
#include<bits/stdc++.h>
using namespace std;
#define N 200010
#define M 1000000010
int arb[N*2],t,c, pos[N], timp[N], pp,L;
void heap_push(int x)
{
if(x==1 || arb[x] > arb[x/2]) return;
else
{
swap(arb[x], arb[x/2]);
swap(pos[timp[x]],pos[timp[x/2]]);
swap(timp[x],timp[x/2]);
heap_push(x/2);
}
}
void hash_echilibrate(int x)
{
if(x*2>L) return;
if(arb[x]> arb[x*2] && (arb[x*2]<arb[x*2+1] || x*2+1>L))
{
swap(arb[x], arb[x*2]);
swap(pos[timp[x]],pos[timp[x*2]]);
swap(timp[x],timp[x*2]);
hash_echilibrate(x*2);
return;
}
if(x*2+1<=L && arb[x]> arb[x*2+1] )
{
swap(arb[x], arb[x*2+1]);
swap(pos[timp[x]],pos[timp[x*2+1]]);
swap(timp[x],timp[x*2+1]);
hash_echilibrate(x*2+1);
return;
}
return;
}
int main()
{
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
int x;
cin>>t;
while(t--)
{
cin>>c;
if(c==1)
{
cin>>x;
L++;pp++;
timp[L] = pp;
arb[L] = x;
pos[pp] = L;
heap_push(L);
}
if(c==2)
{
cin>>x;
arb[pos[x]] = M;
hash_echilibrate(pos[x]);
}
if (c==3)
{
cout<<arb[1]<<'\n';
}
}
return 0;
}