Pagini recente » Cod sursa (job #1044775) | Cod sursa (job #1460227) | Cod sursa (job #2845657) | Cod sursa (job #430295) | Cod sursa (job #2672962)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct Node{
int value,index;
};
vector <Node> heap;
int whereheap[200010];
void swapn(int poz1, int poz2)
{
swap(heap[poz1],heap[poz2]);
swap(whereheap[heap[poz1].index],whereheap[heap[poz2].index]);
}
void insert(Node x)
{
heap.push_back(x);
whereheap[x.index] = heap.size() - 1;
int n = heap.size()-1;
while(n != 1 and heap[n].value < heap[n/2].value) {
swapn(n,n/2);
n /= 2;
}
}
void del(int index)
{
int position = whereheap[index];
swapn(position, heap.size() - 1);
heap.pop_back();
while(position != 1 and heap[position].value < heap[position / 2].value) {
swapn(position,position / 2);
position /= 2;
}
while(2 * position < heap.size())
{
if(2 * position + 1 >= heap.size() or heap[2 * position].value < heap[2 * position + 1].value)
{
if(heap[position].value > heap[2 * position].value)
{
swapn(position, 2 * position);
position *= 2;
}
else break;
}
else
{
if(heap[position].value > heap[2 * position + 1].value)
{
swapn(position, 2 * position + 1);
position *= 2;
position ++;
}
else break;
}
}
}
int main() {
Node first;
heap.push_back(first);
int n, cnt = 0;
fin >> n;
while(n--)
{
int op;
fin >> op;
if(op == 1)
{
int x;
fin >> x;
cnt ++;
Node current;
current.value = x;
current.index = cnt;
insert(current);
}
if(op == 2)
{
int x;
fin >> x;
del(x);
}
if(op == 3)
{
fout << heap[1].value << "\n";
}
}
return 0;
}