Pagini recente » Cod sursa (job #1342216) | Cod sursa (job #745650) | Cod sursa (job #912562) | Cod sursa (job #2331968) | Cod sursa (job #2834148)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int a,b,c,h[400005],n,q,pos,f[400005],where[400005];
//f[i] al catelea in ordine cronologica este elementul de pe pozitia i
//where[i] pe ce pozitie este elementul al i lea in ordine cronologica
void myswap(int&a, int &b)
{
int c=a;
a=b;
b=c;
}
void heapup(int pos)
{
if(pos==1)return;
if(h[pos]<h[pos/2])
{
myswap(h[pos],h[pos/2]);
myswap(where[f[pos]],where[f[pos/2]]);
myswap(f[pos],f[pos/2]);
heapup(pos/2);
}
}
void heapdown(int pos)
{
if((pos*2>n)||(pos*2+1<=n&&min(h[pos*2],h[pos*2+1])>=h[pos])||(pos*2==n&&h[pos*2]>=h[pos]))return;
if(pos*2+1<=n)
{
int p=pos*2;
if(h[pos*2+1]<h[pos*2])p++;
myswap(h[pos],h[p]);
myswap(f[pos],f[p]);
myswap(where[f[pos]],where[f[p]]);
heapdown(p);
return;
}
myswap(h[pos],h[pos*2]);
myswap(f[pos],f[pos*2]);
myswap(where[f[pos]],where[f[pos*2]]);
heapdown(pos*2);
}
void ins(int val)
{
h[++n]=val;
f[n]=n;
where[n]=n;
heapup(n);
}
void pr()
{
fout<<h[1]<<'\n';
}
void rmv(int pos)
{
pos=where[pos];
myswap(h[pos],h[n]);
myswap(where[f[pos]],where[f[n]]);
myswap(f[pos],f[n]);
n--;
if(h[pos]<h[pos/2]&&pos>1)heapup(pos);
else if(pos*2<=n)
{
if(pos*2+1<=n)
{
if(min(h[pos*2],h[pos*2+1])<h[pos])heapdown(pos);
}
else if(h[pos*2]<h[pos])heapdown(pos);
}
}
signed main()
{
fin>>q;
while(q--)
{
fin>>a;
if(a==3){pr();continue;}
fin>>b;
if(a==1)ins(b);
else rmv(b);
}
return 0;
}