Pagini recente » Cod sursa (job #126175) | Cod sursa (job #1915321) | Cod sursa (job #233585) | Cod sursa (job #643843) | Cod sursa (job #1411748)
#include <fstream>
#define DIM 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int N,h[DIM],poz[DIM],v[DIM],M,Q;
void upheap(int x){
int c=x;
int p=c/2;
while(p>=1 && v[h[p]]>v[h[c]]){
swap(h[c],h[p]);
swap(poz[h[c]],poz[h[p]]);
c=p;
p/=2;
}
}
void downheap(int x){
int p=x;
int c=p*2;
while(c<=N){
if(c+1<=N && v[h[c+1]]<v[h[c]])
c++;
if(v[h[p]]>v[h[c]]){
swap(h[c],h[p]);
swap(poz[h[c]],poz[h[p]]);
p=c;
c*=2;
}
else
break;
}
}
void insert(int x){
h[++N]=x;
poz[x]=N;
upheap(N);
}
void delete_root(int x){
v[x]=-1;
upheap(poz[x]);
swap(h[1],h[N]);
poz[h[1]]=1;
N--;
downheap(1);
}
int main(){
fin>>Q;
while(Q--){
int op;
fin>>op;
if(op==1){
fin>>v[++M];
insert(M);
continue;
}
if(op==2){
int x;
fin>>x;
delete_root(x);
continue;
}
fout<<v[h[1]]<<"\n";
}
fin.close();fout.close();
return 0;
}