Pagini recente » Cod sursa (job #1087866) | Cod sursa (job #1575265) | Cod sursa (job #1468644) | Cod sursa (job #2645948) | Cod sursa (job #2154255)
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
std::vector<int> heap;
std::vector<int> heap_pos;
std::vector<int> stack_order;
int disjoint_nr;
int parent(int pos)
{
return (pos-1)/2;
}
int l_child(int pos)
{
return (pos*2)+1;
}
int r_child(int pos)
{
return (pos*2)+2;
}
void upheap(int pos)
{
int parr = parent(pos);
if(pos > 0)
{
if(heap[parr] > heap[pos])
{
int aux = heap[parr];
heap[parr] = heap[pos];
heap[pos] = aux;
aux = stack_order[heap_pos[parr]];
stack_order[heap_pos[parr]] = stack_order[heap_pos[pos]];
stack_order[heap_pos[pos]] = aux;
aux = heap_pos[parr];
heap_pos[parr] = heap_pos[pos];
heap_pos[pos] = aux;
upheap(parr);
}
}
}
void add_to_heap(int x)
{
heap.push_back(x);
heap_pos.push_back(disjoint_nr);
disjoint_nr++;
stack_order.push_back(heap.size()-1);
upheap(heap.size()-1);
}
void print_heap()
{
for(int i=0;i<heap.size();i++)
{
cout<<heap[i]<<" ";
}
cout<<"\n";
}
void down_heap(int pos)
{
int lchild = l_child(pos);
int rchild = r_child(pos);
int maxx = heap[pos];
int swp = pos;
if(lchild <= heap.size()-1 && heap[lchild] < maxx)
{
maxx = heap[lchild];
swp = lchild;
}
if(rchild <= heap.size()-1 && heap[rchild] < maxx)
{
maxx = heap[rchild];
swp = rchild;
}
if(maxx < heap[pos])
{
int aux = heap[pos];
heap[pos] = heap[swp];
heap[swp] = aux;
aux = stack_order[heap_pos[pos]];
stack_order[heap_pos[pos]] = stack_order[heap_pos[swp]];
stack_order[heap_pos[swp]] = aux;
aux = heap_pos[pos];
heap_pos[pos] = heap_pos[swp];
heap_pos[swp] = aux;
down_heap(swp);
}
}
void delete_pos(int pos)
{
heap[pos] = heap[heap.size()-1];
heap_pos[pos] = heap_pos[heap.size()-1];
heap.pop_back();
heap_pos.pop_back();
if(heap.size() != 0)
{
down_heap(pos);
}
}
int n;
int main()
{
disjoint_nr = 0;
fin>>n;
for(int i=0;i<n;i++)
{
int k1;
fin>>k1;
if(k1 == 1)
{
int k2;
fin>>k2;
add_to_heap(k2);
}
else if(k1 == 2)
{
int k2;
fin>>k2;
delete_pos(stack_order[k2-1]);
}
else if(k1 == 3)
{
fout<<heap[0]<<"\n";
}
}
}