Pagini recente » Cod sursa (job #1627235) | Cod sursa (job #592571) | Cod sursa (job #685648) | Cod sursa (job #3157913) | Cod sursa (job #2056715)
#include <iostream>
#include <fstream>
#include <algorithm>
#if 1
#define pv(x) cout<<#x<<" = "<<x<<"; "
#define pn cout<<"\n"
#else
#define pv(x)
#define pn
#endif
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
#define pb push_back
#define mp make_pair
const int NMax = 2e5 + 5;
const int inf = 1e9 + 5;
const int mod = 9917;
using zint = int;
int N,nrInsert;
int pos[NMax];
struct elem {
int val,ord;
elem(int _val = 0,int _ord = 0) {
val = _val;
ord = _ord;
}
}heap[NMax];
void swap_heap(int,int);
void percolate(int);
void sift(int);
int main() {
int M;
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 idx = pos[x];
swap_heap(idx,N);
--N;
percolate(idx);
sift(idx);
}
else {
out<<heap[1].val<<'\n';
}
}
in.close();out.close();
return 0;
}
void swap_heap(int a,int b) {
swap(pos[heap[a].ord],pos[heap[b].ord]);
swap(heap[a],heap[b]);
}
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 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);
node = son;
}
else {
son = 0;
}
}
while (son != 0);
}