Pagini recente » Cod sursa (job #2121240) | Cod sursa (job #932187) | Cod sursa (job #696196) | Cod sursa (job #1746882) | Cod sursa (job #1908154)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int NH, H[200001], NR;
int P[200001];//poz elementului introdus al i - lea in heap
int INP[200001]; // al catelea a fost introdus el de pe poz i din heap
// nr = nr elemente introduse in heap
void add(int x){
int poz,parinte;
NH++;
NR++;
H[NH] = x;
P[NR] = NH;
INP[NH] = NR;
poz = NH;
parinte = poz / 2;
while( H[parinte] > H[poz] && parinte != 0){
swap(H[parinte],H[poz]);
swap(P[INP[parinte]],P[INP[poz]]);
swap(INP[parinte],INP[poz]);
poz = parinte;
parinte = poz / 2;
}
}
void del(int x){
int poz, d;
poz = P[x];
swap(H[poz],H[NH]);
swap(P[INP[poz]],P[INP[NH]]);
swap(INP[poz],INP[NH]);
NH--;
d = poz * 2;
while (1){
d=2*poz;
if (d>NH)
break;
if (d+1 <= NH && H[d+1] < H[d])
d++;
if (H[poz]>H[d]){
swap(H[poz],H[d]);
swap(P[INP[poz]],P[INP[d]]);
swap(INP[poz],INP[d]);
poz=d;
}
else
break;
}
}
int main()
{
int n,i,v, x;
f >> n;
for(i = 1; i <= n; i++){
f >> v;
switch(v){
case 1:
f >> x;
add(x);
break;
case 2:
f >> x;
del(x);
break;
case 3:
g << H[1] << '\n';
break;
}
}
return 0;
}