Pagini recente » Profil ana_papara | Cod sursa (job #512933) | Cod sursa (job #1123746) | Rating mike jones (fizzlesticks23) | Cod sursa (job #2013032)
#include <iostream>
#include <fstream>
#define ll long long
#define ull unsigned long long
#define pb push_back
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int NMax = 2e5 + 5;
const int sqMax = 320 + 5;
int N,M,nrInsert;
int pos[NMax];
struct elem {
int val,idx;
elem(int _val = 0,int _idx = 0) {
val = _val;
idx = _idx;
}
}heap[NMax];
void sift(int);
void percolate(int);
void swap_heap(int,int);
int main() {
in>>M;
while (M--) {
int tip,x;
in>>tip;
if (tip == 1) {
in>>x;
heap[++N] = elem(x,++nrInsert);
pos[nrInsert] = N;
percolate(N);
}
else if (tip == 2) {
in>>x;
int p = pos[x];
swap_heap(p,N);
--N;
if (heap[p].val < heap[p/2].val) {
percolate(p);
}
else {
sift(p);
}
}
else {
out<<heap[1].val<<'\n';
}
}
in.close();out.close();
return 0;
}
void sift(int node) {
int son = 0;
do {
son = 0;
int fs = node<<1,ss = fs+1;
if (fs <= N) {
son = fs;
if (ss <= N && heap[ss].val < heap[fs].val) {
son = ss;
}
}
if (son && heap[son].val < heap[node].val) {
swap_heap(node,son);
swap(node,son);
}
else {
son = 0;
}
}
while (son);
}
void percolate(int node) {
int dad = node>>1;
while (node != 1 && heap[node].val < heap[dad].val) {
swap_heap(node,dad);
node = dad;
dad = node>>1;
}
}
void swap_heap(int a,int b) {
swap(pos[heap[a].idx],pos[heap[b].idx]);
swap(heap[a],heap[b]);
}