Pagini recente » Cod sursa (job #755554) | Cod sursa (job #1746569) | Cod sursa (job #552943) | Cod sursa (job #1526618) | Cod sursa (job #553008)
Cod sursa(job #553008)
#include<fstream>
#define nmax 200010
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,l,nr,c,z;
int hp[nmax],poz[nmax],a[nmax];
void upheap(int nod){
int aux;
while(a[hp[nod]]<a[hp[nod/2]]){
aux=hp[nod];
hp[nod]=hp[nod/2];
hp[nod/2]=aux;
poz[hp[nod/2]]=nod/2;
poz[hp[nod]]=nod;
nod/=2;
}
}
void downheap(int x){
int aux, y = 0;
while (x != y){
y = x;
if (y*2<=l && a[hp[x]]>a[hp[y*2]]) x = y*2;
if (y*2+1<=l && a[hp[x]]>a[hp[y*2+1]]) x = y*2+1;
aux = hp[x];
hp[x] = hp[y];
hp[y] = aux;
poz[hp[x]] = x;
poz[hp[y]] = y;
}
}
int main(){
f>>n;
for(int i=1;i<=n;i++){
f>>c;
if(c==1){
l++,nr++;
f>>z;
a[nr]=z;
hp[l]=nr;
poz[nr]=l;
upheap(l);
}
if(c==2){
f>>z;
a[z]=-1;
upheap(poz[z]);
poz[hp[1]]=0;
hp[1]=hp[l--];
poz[hp[1]]=1;
downheap(1);
}
if(c==3)
g<<a[hp[1]]<<'\n';
}
g.close();
return 0;
}