Pagini recente » Cod sursa (job #480649) | Cod sursa (job #2246782) | Cod sursa (job #699639) | Cod sursa (job #1245082) | Cod sursa (job #1752778)
#include<fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,a,b,i,heap[200001],val[200001],pos[200001],nr,nrh,temp;
//heap[i]-poz i din heap retine ord cronologica in care a fost introdus un element
//val[i]-val elem cu ord cron i
//pos[i]-poz elem cu ord cron i in heap
int father(int nod){
return nod/2;
}
int ls(int nod){
return 2*nod;
}
int rs(int nod){
return 2*nod+1;
}
void percolate(int poz){
while(poz>1 && val[heap[poz]]<val[heap[father(poz)]]){
swap(heap[poz],heap[father(poz)]);
swap(pos[heap[poz]],pos[heap[father(poz)]]);
poz=father(poz);
}
}
void add(int &n,int poz){
n++;
heap[n]=poz;
percolate(n);
}
void shift(int poz){
int s;
do{
s=0;
if (ls(poz)<=nrh){
s=ls(poz);
if (rs(poz)<=nrh && val[heap[rs(poz)]]<val[heap[s]]) s=rs(poz);
if (val[heap[s]]>=val[heap[poz]]) s=0;
}
if (s!=0){
swap(heap[poz],heap[s]);
swap(pos[heap[poz]],pos[heap[s]]);
poz=s;
}
}while(s!=0);
}
void elim(int &n,int poz){
heap[poz]=heap[n];
n--;
if (poz>1 && val[heap[poz]]<val[heap[father(poz)]]) percolate(poz);
else
shift(poz);
}
int main(){
fin>>n;
for (i=1;i<=n;i++){
fin>>a;
if (a==3) fout<<val[heap[1]]<<'\n';
else
if (a==1){
fin>>b;
nr++;
val[nr]=b;
pos[nr]=nrh+1;
add(nrh,nr);
}
else
if (a==2){
fin>>b;
temp=pos[b];
swap(pos[b],pos[heap[nrh]]);
//pos[b]=0;
elim(nrh,temp);
}
}
fin.close();
fout.close();
return 0;
}