Pagini recente » Cod sursa (job #1561501) | Cod sursa (job #2022897) | Cod sursa (job #109897) | Cod sursa (job #53966) | Cod sursa (job #3354531)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define nmax 200005
int Heap[nmax];
int pos[nmax];//al k-lea element inserat se afla pe pozitia __ in heap
int pos_ins[nmax];//pe pozitia i se afla al __ element inserat
int heap_size;
int ins_size;
void percolate(int i){
while (i > 1 && Heap[i] < Heap[i/2]){
swap(Heap[i], Heap[i/2]);
swap(pos_ins[i], pos_ins[i/2]);
pos[pos_ins[i]] = i;
pos[pos_ins[i/2]] = i / 2;
i /= 2;
}
}
void sift(int i){
while(i * 2 <= heap_size){
int best = i * 2;
if(i * 2 + 1 <= heap_size && Heap[best] > Heap[i * 2 + 1]){
best = i * 2 + 1;
}
if(Heap[i] > Heap[best]){
swap(Heap[i] , Heap[best]);
swap(pos_ins[i], pos_ins[best]);
pos[pos_ins[i]] = i;
pos[pos_ins[best]] = best;
}
else break;
i *= 2;
}
}
int main()
{
int n;
fin >> n;
while(n--){
int c;
fin >> c;
if(c == 1){
int x;
fin >> x;
heap_size++;
ins_size++;
Heap[heap_size] = x;
pos_ins[heap_size] = ins_size;
pos[ins_size] = heap_size;
percolate(heap_size);
}
else if(c == 2){
int x;
fin >> x;
int p = pos[x];
Heap[p] = Heap[heap_size];
pos[pos_ins[heap_size]] = p;
pos_ins[p] = pos_ins[heap_size];
heap_size--;
percolate(p);
sift(p);
}
else if(c == 3){
fout << Heap[1] << '\n';
}
}
return 0;
}