Pagini recente » Cod sursa (job #1389599) | Rating Morosan Andrei (andreimorosan) | Cod sursa (job #1630706) | Cod sursa (job #1857937) | Cod sursa (job #2314594)
#include <fstream>
#define DIM 200000
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,cod,x,a[DIM],h[DIM],poz[DIM],c,p,i,l;
int main() {
fin>>n;
while (n--) {
fin>>cod;
if (cod==1) {
fin>>x;
a[++i]=x;
h[++l]=i;
poz[i]=l;
c=l; p=c/2;
while (p!=0) { //urc elementul in heap
if (a[h[c]]<a[h[p]]) {
swap(h[c],h[p]);
poz[h[c]]=c;
poz[h[p]]=p;
}
else
break;
c=p; p/=2;
}
continue;
}
if (cod==2) {
fin>>x;
a[x]=-1; //sterg elementul ducandu-l in radacina
c=poz[x];
p=c/2;
while (p!=0) {
if (a[h[c]]<a[h[p]]) {
swap(h[c],h[p]);
poz[h[c]]=c;
poz[h[p]]=p;
}
else
break;
c=p; p/=2;
}
h[1]=h[l--];
poz[h[1]]=1;
p=1; c=2;
while (c<=l) { //sterg radacina
if (c<l&&a[h[c+1]]<a[h[c]])
c++;
if (a[h[p]]>a[h[c]]) {
swap(h[c],h[p]);
poz[h[c]]=c;
poz[h[p]]=p;
}
else
break;
p=c; c*=2;
}
continue;
}
if (cod==3)
fout<<a[h[1]]<<"\n";
}
return 0;
}