Pagini recente » Cod sursa (job #1528124) | Registru diplome | Cod sursa (job #274405) | Cod sursa (job #223813) | Cod sursa (job #1260786)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int v[200001],p[200001],h[20001],n,nr;
int left_son(int poz){
return poz*2;
}
int right_son(int poz){
return poz*2+1;
}
void interschimbare(int x1, int x2){
swap(h[x1] , h[x2]);
swap(p[h[x1]],p[h[x2]]);
// p[h[x1]]=x2;
//p[h[x2]=x1;
}
void sus(int poz){
while(poz/2>0&&v[h[poz]] < v[h[poz/2]]){
interschimbare(poz,poz/2);
poz/=2;
}
}
void jos(int poz){
int st=left_son(poz);
while(st<=n){
int Max=poz;
if(v[h[st]]<v[h[Max]])
Max=st;
int dr=right_son(poz);
if(dr<=n&&v[h[dr]]<v[h[Max]])
Max=dr;
if(Max!=poz){
interschimbare(Max,poz);
poz=Max;
st=left_son(poz);
}
else
break;
}
}
void sterge(int poz){
if(poz/2>=1&&v[h[poz]]<v[h[poz/2]])
sus(poz);
else
jos(poz);
}
int main()
{
int t;
f>>t;
int q1,x1;
for(int q=1;q<=t;q++){
f>>q1;
switch(q1){
case 1:{ f>>v[++nr];n++;h[n]=nr;p[nr]=n; sus(n);break;}
case 2:{ f>>x1;h[p[x1]]=h[n];
p[h[n]]=p[x1];
n--;
sterge(p[x1]);
break; }
case 3:{g<<v[h[1]]<<'\n';break;}
}
}
return 0;
}