Pagini recente » Cod sursa (job #2952825) | Cod sursa (job #367413) | Cod sursa (job #1930404) | Cod sursa (job #477630) | Cod sursa (job #955982)
Cod sursa(job #955982)
#include <fstream>
#include <iostream>
#include <cassert>
using namespace std;
#define N 200005
#define in "heapuri.in"
#define out "heapuri.out"
#define xx first
#define yy second
#define oo (1 << 30)
typedef pair <int, int> date;
int ord[N], n, elem, hs; //ord - pozitia in heap a elemntului al i-lea adaugat
date H[N]; //first - elem, second - pozitia in ord a elementului
void swap(date &x, date &y) {
date aux = x;
x = y;
y = aux;
}
void swap(int &x, int &y) {
int aux = x;
x = y;
y = aux;
}
void insert(int x) {
H[++hs].xx = x;
H[hs].yy = ++elem;
ord[elem] = hs;
int k = hs;
while (k / 2 > 0 && H[k / 2].xx > H[k].xx) {
swap (ord[H[k].yy], ord[H[k / 2].yy]);
swap (H[k], H[k / 2]);
k /= 2;
}
}
void eject(int i) {
H[i] = H[hs];
H[hs--].xx = oo;
ord[H[i].yy] = i;
int k = i, fiu = 0;
while (fiu <= hs) {
if (H[k].xx < H[2 * k + 1].xx && H[k].xx > H[2 * k].xx)
fiu = 2 * k;
else
if (H[k].xx > H[2 * k + 1].xx && H[k].xx < H[2 * k].xx)
fiu = 2 * k;
else
if (H[k].xx > H[2 * k].xx && H[k].xx > H[2 * k + 1].xx) {
if (H[2 * k].xx < H[2 * k + 1].xx)
fiu = 2 * k;
else
fiu = 2 * k + 1;
}
else
break;
swap (ord[H[k].yy], ord[H[fiu].yy]);
swap (H[k], H[fiu]);
k = fiu;
}
}
int main() {
ifstream fin (in);
fin >> n;
ofstream fout (out);
for (int i = 1; i < N; ++i)
H[i].xx = oo;
for (int i = 0; i < n; ++i) {
int t;
fin >> t;
if (t == 3) {
fout << H[1].xx << "\n";
continue;
}
int x;
fin >> x;
if (t == 1)
insert(x);
if (t == 2) {
cout << H[ord[x]].xx << " " << H[ord[x]].yy << " " << x << "\n";
eject(ord[x]);
}
}
fcloseall();
return 0;
}