Pagini recente » Cod sursa (job #568780) | Cod sursa (job #572315) | Cod sursa (job #2839126) | Cod sursa (job #2436210) | Cod sursa (job #2475660)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define Nmax 2000005
int N, i, Heap[Nmax], Val[Nmax], P[Nmax];
bool cmp(int x, int y) {
return x < y;
}
void Swap(int x, int y) {
swap(Heap[x], Heap[y]);
swap(P[Heap[x]], P[Heap[y]]);
}
void Up(int k) {
int f = k / 2;
if(f && cmp(Val[Heap[k]], Val[Heap[f]])) {
Swap(k, f);
Up(f);
}
}
void Down(int k) {
int s = k * 2;
if(s + 1 <= N && cmp(Val[Heap[s]], Val[Heap[s + 1]]))s++;
if(s <= N && cmp(Val[Heap[s]], Val[Heap[k]])) {
Swap(k, s);
Down(s);
}
}
void Insert(int x) {
i++;
Val[i] = x;
N++;
Heap[N] = i;
P[i] = N;
Up(N);
}
void Erase(int x) {
Swap(x, N);
N--;
Down(x);
}
int main() {
int M;
fin >> M;
int operation, number;
for(; M; M--) {
fin >> operation;
if(operation == 1)fin >> number, Insert(number);
if(operation == 2)fin >> number, Erase(number);
if(operation == 3)fout << Val[Heap[1]] << "\n";
}
}