Pagini recente » Cod sursa (job #1650081) | Cod sursa (job #478117) | Cod sursa (job #2246954) | Cod sursa (job #1391433) | Cod sursa (job #1845715)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n, q, x;
int a[200005], hp[200005];
int poz[200005], l, nr;
void siftup(int x) {
while (x/2 && a[hp[x]] < a[hp[x/2]]) {
swap(hp[x], hp[x/2]);
poz[hp[x]] = x;
poz[hp[x/2]] = x/2;
x/=2;
}
}
void siftdown(int x) {
int y;
while (x != y) {
y = x;
if (y*2 <= l && a[hp[x]] > a[hp[y*2]])
x *= 2;
if (y*2+1 <= l && a[hp[x]] > a[hp[y*2+1]])
x = y*2+1;
swap(hp[x], hp[y]);
poz[hp[x]] = x;
poz[hp[y]] = y;
}
}
int main() {
f >> n;
while (n--) {
f >> q;
if (q == 1) {
f >> x;
l++, nr++;
poz[nr] = l;
a[nr] = x;
hp[l] = nr;
siftup(l);
}
else if (q == 2) {
f >> x;
a[x] = -1;
siftup(poz[x]);
poz[hp[1]] = 0;
hp[1] = hp[l];
l--;
poz[hp[1]] = 1;
if (l>1)
siftdown(1);
}
else if (q == 3) {
g << a[hp[1]] << '\n';//
}
}
return 0;
}