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