Pagini recente » Cod sursa (job #2373285) | Cod sursa (job #2714702) | Cod sursa (job #2893392)
#include <fstream>
#include <vector>
#include <map>
using namespace std;
long long pozitii[200001],inheap[200001];
vector<pair<long long, long long >> heap;
long long n,op,nr,poz,i;
long long introdus = 1,k;
void urca(long long poz)
{
while (poz)
{
if(heap[(poz-1)/2].first>heap[poz].first) {
swap(heap[(poz - 1) / 2], heap[poz]);
swap(inheap[heap[poz].second],inheap[heap[(poz-1)/2].second]);
poz = (poz-1)/2;
}
else
break;
}
}
void coboara(long long poz) {
if (poz * 2 + 1 >= k)
return;
long long fiu_st = heap[poz * 2 + 1].first;
if ((poz * 2 + 2 == k) || fiu_st < heap[poz * 2 + 2].first) {
if (fiu_st < heap[poz].first) {
swap(heap[poz], heap[poz * 2 + 1]);
swap(inheap[heap[poz].second],inheap[heap[poz*2+1].second]);
coboara(poz * 2 + 1);
return;
} else {
return;
}
} else {
if (heap[poz * 2 + 2].first < heap[poz].first) {
swap(heap[poz], heap[poz * 2 + 2]);
swap(inheap[heap[poz].second],inheap[heap[poz*2+2].second]);
coboara(poz * 2 + 2);
return;
} else {
return;
}
}
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>n;
for(i = 1;i<=n;i++)
{
f>>op;
if(op == 1)
{
f>>nr;
pozitii[introdus] = nr;
inheap[introdus] = k;
heap.push_back({nr,introdus});
urca(k);
introdus++;
k++;
}
else if(op == 2)
{
f>>poz;
heap[inheap[poz]] = heap[heap.size()-1];
k--;
heap.pop_back();
urca(inheap[poz]);
coboara(inheap[poz]);
} else
g<<heap[0].first<<'\n';
}
f.close();
g.close();
return 0;
}