Pagini recente » Cod sursa (job #3223131) | Cod sursa (job #3236780) | Cod sursa (job #2705690) | Cod sursa (job #2296333) | Cod sursa (job #2575213)
#include <fstream>
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
const int N = 2e5 + 5;
int k, T;
struct nod {
int val, timp; ///valoarea, timpul la care a fost adaugat in heap
} v[N];
int poz[N]; ///poz[i] = pozitia din heap pe care se afla elementul adaugat la timpul i in heap
void adaug(int x, int timp) {
v[++k].val = x;
v[k].timp = timp;
poz[timp] = k;
int aux = k;
while (aux > 1 && v[aux].val < v[aux / 2].val) {
swap(poz[v[aux].timp], poz[v[aux / 2].timp]);
swap(v[aux], v[aux / 2]);
aux /= 2;
}
}
void sterg(int p) {
poz[v[k].timp] = p;
v[p] = v[k];
v[k] = {0, 0};
--k;
int aux = p;
while (aux * 2 <= k && (v[aux].val > v[aux * 2].val || v[aux].val > v[aux * 2 + 1].val)) {
if (v[aux * 2].val > v[aux * 2 + 1].val && aux * 2 + 1 <= k) {
swap(poz[v[aux].timp], poz[v[aux * 2 + 1].timp]);
swap(v[aux], v[aux * 2 + 1]);
aux = aux * 2 + 1;
}
else {
swap(poz[v[aux].timp], poz[v[aux * 2].timp]);
swap(v[aux], v[aux * 2]);
aux = aux * 2;
}
}
}
int main() {
int n, tip, x;
fin >> n;
for (int i = 1; i <= n; ++i) {
fin >> tip;
switch(tip) {
case 1 : fin >> x; adaug(x, ++T); break;
case 2 : fin >> x; sterg(poz[x]); break;
case 3 : fout << v[1].val << '\n'; break;
}
}
return 0;
}