Pagini recente » Cod sursa (job #1857577) | Cod sursa (job #1782082) | Cod sursa (job #1254867) | Cod sursa (job #2002710) | Cod sursa (job #2577335)
#include <fstream>
using namespace std;
ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");
int poz[200005];
struct heap {
int intrat,val;
} h[200005];
int t,k, n, m;
void inserare(int x) {
h[n+1].val=x;
t++;
h[n+1].intrat=t;
n++;
poz[t]=n;
int p = n;
bool promovare=true;
while(promovare==true) {
if(h[p].val<h[p/2].val && p > 1) {
heap aux=h[p];
h[p]=h[p/2];
h[p/2]=aux;
int auxpoz=poz[h[p].intrat];
poz[h[p].intrat]=poz[h[p/2].intrat];
poz[h[p/2].intrat]=auxpoz;
p/=2;
} else
promovare=false;
}
}
void stergere(int timp) {
int pozdesters=poz[timp];
poz[h[n].intrat] = pozdesters;
poz[timp] = 0;
h[pozdesters]=h[n];
h[n]= {0,0};
n--;
if (pozdesters == n + 1)
return;
int p = pozdesters;
bool promovare=true;
while(promovare==true) {
if(h[p].val<h[p/2].val && p > 1) {
heap aux=h[p];
h[p]=h[p/2];
h[p/2]=aux;
int auxpoz=poz[h[p].intrat];
poz[h[p].intrat]=poz[h[p/2].intrat];
poz[h[p/2].intrat]=auxpoz;
p/=2;
} else
promovare=false;
}
bool cernere=true;
int i=p;
while(cernere==true) {
if((h[i*2].val<h[i*2+1].val && i*2+1<=n || i*2+1>n && i*2<=n) && h[i*2].val<h[i].val) {
swap(h[i*2],h[i]);
swap(poz[h[i*2].intrat],poz[h[i].intrat]);
i=i*2;
} else if((h[i*2+1].val<=h[i*2].val && i*2+1<=n) && h[i*2+1].val<h[i].val) {
swap(h[i*2+1],h[i]);
swap(poz[h[i*2+1].intrat],poz[h[i].intrat]);
i=i*2+1;
} else
cernere=false;
}
}
int main() {
int n,tip,x,timp;
cin>>m;
for(int i=1; i<=m; ++i) {
if (i == 374)
tip = 1;
cin>>tip;
if(tip==1) {
cin>>x;
inserare(x);
} else if(tip==2) {
cin>>timp;
stergere(timp);
} else
cout <<h[1].val<<'\n';
for (int j = 1; j <= t; ++j)
if (h[poz[j]].val < h[poz[j] / 2].val && poz[j])
cout << i << ' ' << j << ' ' << poz[j] << " PULA\n";
}
return 0;
}