Pagini recente » Cod sursa (job #1684858) | Cod sursa (job #2725935) | Cod sursa (job #1943645) | Cod sursa (job #2053091) | Cod sursa (job #1950871)
#include <bits/stdc++.h>
#define NMAX 200005
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
int H[NMAX],P[NMAX],V[NMAX],N,NR;
void UpHeap(int k){
while((k >> 1) > 0 && V[H[k]] < V[H[k >> 1]]){
swap(H[k],H[k >> 1]);
P[H[k]] = k;
P[H[k >> 1]] = k >> 1;
k = k >> 1;
}
}
void DownHeap(int k){
int son = 0;
while( son != k){
son = k;
if(V[H[k]] > V[H[k << 1]] && (k << 1) <= N)
son = k << 1;
if(V[H[k]] > V[H[(k << 1) + 1]] && (k << 1) + 1 <= N && V[H[k << 1]] < V[H[(k << 1) + 1]])
son = (k << 1) + 1;
swap(H[k],H[son]);
P[H[k]] = k;
P[H[son]] = son;
}
}
int main()
{
ios :: sync_with_stdio(false);
int n,t,x,k = 0;
fin >> n;
while(n--){
fin >> t;
if(t == 1){
fin >> x;
V[++NR] = x;
H[++N] = NR;
P[NR] = N;
UpHeap(N);
// fout << x << " " << V[H[1]] << "\n";
}
if(t == 2){
fin >> x;
int p = P[x];
P[H[N]] = p;
swap(H[p],H[N]);
N--;
UpHeap(p);
DownHeap(p);
}
if(t == 3)
fout << V[H[1]] << "\n";
}
return 0;
}