Pagini recente » Cod sursa (job #786682) | Cod sursa (job #2120661) | Cod sursa (job #656975) | Cod sursa (job #1433826) | Cod sursa (job #1978201)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
#define pb push_back
#define ll long long
const int NMax = 2e5 + 5;
const int inf = 1e9 + 5;
int N,M,nrInsert;
int idx[NMax];
struct elem {
int val,ord;
elem (int _val = 0,int _ord = 0) {
val = _val;
ord = _ord;
}
}heap[NMax];
void sift(int);
void percolate(int);
void swap_heap(elem&,elem&);
int main()
{
in>>M;
/*
elem test[3] = {elem(),elem(1,10),elem(2,11)};
idx[10] = 1;
idx[11] = 2;
//swap_heap(test[1],test[2]);
for (int i=0;i<=2;++i) {
cout<<test[i].val<<' '<<test[i].ord<<' '<<idx[test[i].ord]<<'\n';;
}
*/
N = nrInsert = 0;
while (M--) {
int tip;
in>>tip;
switch (tip) {
case 1: {
int val;
in>>val;
heap[++N] = elem(val,++nrInsert);
idx[nrInsert] = N;
percolate(N);
break;
}
case 2: {
int ord;
in>>ord;
int pos = idx[ord];
swap_heap(heap[pos],heap[N]);
--N;
int dad = pos / 2;
if (heap[dad].val > heap[pos].val) {
percolate(pos);
}
else {
sift(pos);
}
break;
}
case 3: {
out<<heap[1].val<<'\n';
break;
}
}
/*
for (int i=1;i <= N;++i) {
cout<<i<<' '<<heap[i].val<<' '<<heap[i].ord<<' '<<idx[heap[i].ord]<<'\n';
}
cout<<'\n';
//*/
}
in.close();out.close();
return 0;
}
void sift(int node) {
int son = 0;
do {
son = 0;
int fs = node*2,
ss = node*2 + 1;
if (fs <= N && heap[fs].val < heap[node].val) {
son = fs;
if (ss <= N && heap[ss].val < heap[son].val) {
son = ss;
}
}
if (son) {
swap_heap(heap[son],heap[node]);
node = son;
}
}
while (son);
}
void percolate(int node) {
int dad = node / 2;
while (node != 1 && heap[dad].val > heap[node].val) {
swap_heap(heap[node],heap[dad]);
node = dad;
dad = node / 2;
}
}
void swap_heap(elem& a,elem& b) {
swap(idx[a.ord],idx[b.ord]);
swap(a,b);
}