Pagini recente » Cod sursa (job #1574099) | Cod sursa (job #1203076) | Cod sursa (job #1885605) | Cod sursa (job #1709950) | Cod sursa (job #2811488)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<int>heap;
vector<int>ordine;
void up(int poz){
while(heap[poz]<heap[poz/2] && poz!=1){
swap(heap[poz],heap[poz/2]);
poz/=2;
}
}
void down(int poz){
int l = heap.size();
while(poz*2 < l){
int fiu = 2*poz;
fiu+=(l>fiu+1 && heap[fiu]>heap[fiu+1]);
if(heap[fiu]>=heap[poz])
break;
swap(heap[poz],heap[fiu]);
poz=fiu;
}
}
void ins(int x){
heap.push_back(x);
int poz=heap.size()-1;
if(heap[poz]>heap[poz/2])
up(poz);
}
void del(int poz){
swap(heap[heap.size()-1],heap[poz]);
heap.pop_back();
up(poz);
down(poz);
}
int main()
{
ifstream in("heapuri.in");
ofstream out("heapuri.out");
heap.push_back(0);
int n,cerinta,x;
in>>n;
for(int i=0;i<n;i++){
in>>cerinta;
if(cerinta == 1){
in>>x;
ins(x);
ordine.push_back(x);
}
else if(cerinta == 2){
in>>x;
int pozitie;
for(int i=0;i<ordine.size();i++){
if(ordine[x-1] == heap[i]){
pozitie = i;
break;
}
}
del(pozitie);
}
else{
out<<heap[1]<<'\n';
}
}
return 0;
}