Pagini recente » Cod sursa (job #2487699) | Istoria paginii runda/3333333333333 | Cod sursa (job #2470001) | Cod sursa (job #1744489) | Cod sursa (job #2892276)
#include <fstream>
#include <vector>
#include <unordered_set>
#include <iostream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
vector <int> hip, ordine;
unordered_set <int> sterse;
int nrOperatii, nr, operatie;
void urcare(int ptr)
{
while (hip[ptr] < hip[(ptr-1)/2] && ptr > 0)
{
swap(hip[ptr], hip[(ptr-1)/2]);
ptr = (ptr-1)/2;
}
}
void coborare(int ptr)
{
int fiu;
while (true)
{
if (ptr * 2 + 2 < hip.size())
{
if (hip[ptr*2+1] < hip[ptr*2+2])
fiu = 1;
else
fiu = 2;
if (hip[ptr*2+fiu] < hip[ptr])
{
swap(hip[ptr*2+fiu], hip[ptr]);
ptr = ptr*2+fiu;
}
else
return;
}
else if (ptr * 2 +1 < hip.size())
{
if (hip[ptr*2+1] < hip[ptr])
swap(hip[ptr*2+1], hip[ptr]);
return;
}
else
return;
}
}
int main()
{
fin >> nrOperatii;
while (nrOperatii--)
{
if (nrOperatii == 758)
int cuc = 4;
fin >> operatie;
if (operatie == 1)
{
fin >> nr;
hip.push_back(nr);
ordine.push_back(nr);
urcare(hip.size()-1);
}
if (operatie == 2)
{
fin >> nr;
sterse.insert(ordine[nr-1]);
}
if (operatie == 3)
{
while(sterse.find(hip[0])!= sterse.end())
{
sterse.erase(hip[0]);
hip[0] = hip[hip.size()-1];
hip.erase(hip.end()-1);
coborare(0);
}
fout << hip[0] << '\n';
}
}
return 0;
}