Pagini recente » Cod sursa (job #778667) | Cod sursa (job #1504672) | Cod sursa (job #800865) | Cod sursa (job #2601242) | Cod sursa (job #976703)
Cod sursa(job #976703)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n;
int a[200010], id[200010];
int k;
void operatie1 () {
n++;
f >> a[n];
k++;
id[n] = k;
int i = n;
while (a[i] < a[i / 2]) {
int aux;
aux = a[i];
a[i] = a[i / 2];
a[i / 2] = aux;
aux = id[i];
id[i] = id[i / 2];
id[i / 2] = aux;
i = i / 2;
}
}
void operatie2 () {
int nr;
f >> nr;
for (int i = 1; i <= n; i++) {
if (id[i] == nr) {
a[i] = a[n];
id[i] = id[n];
while ((a[i] > a[i * 2] || a[i] > a[i * 2 + 1]) && (i * 2 < n)) {
if (a[i * 2] > a[i * 2 + 1] && i * 2 + 1 > 0)
{
int aux;
aux = a[i];
a[i] = a[i * 2 + 1];
a[i * 2 + 1] = aux;
aux = id[i];
id[i] = id[i * 2 + 1];
id[i * 2 + 1] = aux;
i = i * 2 + 1;
}
else {
int aux;
aux = a[i];
a[i] = a[i * 2];
a[i * 2] = aux;
aux = id[i];
id[i] = id[i * 2];
id[i * 2] = aux;
i = i * 2;
}
}
break;
}
}
n--;
}
int main()
{
int t;
f >> t;
for (int i = 1; i <= t; i++) {
int ID;
f >> ID;
if (ID == 1) {
operatie1();
}
if (ID == 2) {
operatie2();
}
if (ID == 3) {
g << a[1] << '\n';
}
}
return 0;
}