Pagini recente » Cod sursa (job #1552203) | Cod sursa (job #1875678) | Cod sursa (job #1822643) | Cod sursa (job #2152153) | Cod sursa (job #2815212)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<int>heap(1,0);
vector<int>ordine(1,0);
vector<int>indice(1,0);
int cnt=1;
void up(int poz){
while(heap[poz]<heap[poz/2] && poz>1){
swap(heap[poz],heap[poz/2]);
swap(ordine[indice[poz]],ordine[indice[poz/2]]);
swap(indice[poz],indice[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]);
swap(ordine[indice[poz]],ordine[indice[fiu]]);
swap(indice[poz],indice[fiu]);
poz=fiu;
}
}
void ins(int x){
heap.push_back(x);
indice.push_back(cnt);
cnt++;
ordine.push_back(heap.size()-1);
int poz=heap.size()-1;
if( poz>1 && heap[poz]<heap[poz/2] )
up(poz);
}
void del(int poz){
swap(heap[heap.size()-1],heap[poz]);
swap(ordine[indice[poz]],ordine[indice[indice.size()-1]]);
swap(indice[poz],indice[indice.size()-1]);
heap.pop_back();
up(poz);
down(poz);
}
int main()
{
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n,cerinta,x;
in>>n;
for(int i=0;i<n;i++){
in>>cerinta;
if(cerinta == 1){
in>>x;
ins(x);
// for(int i=1;i<heap.size();i++)
// cout<<heap[i]<<" ";
//cout<<'\n';
}
else if(cerinta == 2){
in>>x;
del(ordine[x]);
//cout<<"del "<<indice[ordine[x]]<<" "<<x<<"\n";
// for(int i=1;i<heap.size();i++)
// cout<<heap[i]<<" ";
// cout<<'\n';
}
else{
out<<heap[1]<<'\n';
}
}
return 0;
}