Pagini recente » Cod sursa (job #501893) | Cod sursa (job #1171511) | Cod sursa (job #3234077) | Cod sursa (job #2511432) | Cod sursa (job #726475)
Cod sursa(job #726475)
#include <fstream>
using namespace std;
int heap[200001],pos[200001];
int father(int k){
return k/2;}
int leftson(int k){
return 2*k;}
int rightson(int k){
return 2*k+1;}
void sift(int h[200001], int n, int k){
int son;
do{
son=0;
if(leftson(k)<=n)
son=leftson(k);
if(rightson(k)<=n&&pos[h[rightson(k)]]<pos[h[leftson(k)]])
son=rightson(k);
if(pos[h[son]]>pos[h[k]])
son=0;
if(son){
swap(h[k],h[son]);
swap(heap[k],heap[son]);
k=son;}}
while(son);}
void percolate(int h[200001],int n, int k){
int key,nod;
nod=k;
key=pos[h[k]];
while(k>1&&key<pos[h[father(k)]]){
h[k]=h[father(k)];
heap[father(k)]=k;
k=father(k);}
h[k]=nod;
heap[nod]=k;}
void insert(int h[200001],int &n,int key){
n++;
h[n]=key;
heap[key]=n;
percolate(h,n,n);}
void cut(int h[200001],int &n,int k){
h[k]=h[n];
heap[h[k]]=k;
n--;
if(k>1&&pos[h[k]]>pos[h[father(k)]])
percolate(h,n,k);
else sift(h,n,k);}
int main(){
int i,o,x,n,np,nh,h[200001];
np=nh=0;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>n;
for(i=1;i<=n;i++){
f>>o;
if(o<3)
f>>x;
if(o==1){
np++;
pos[np]=x;
insert(h,nh,np);}
if(o==2)
cut(h,nh,heap[x]);
if(o==3)
g<<pos[h[1]]<<"\n";}
f.close(); g.close();
return 0;}