Cod sursa(job #1208423)

Utilizator mihail.jianuJianu Mihail mihail.jianu Data 15 iulie 2014 18:08:05
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.37 kb
#include<cstdio>
const int N=200000;
int pos_to_heapPos[N+1],heapPos_to_pos[N+1];
void swapInt(int&x,int&y){
    int aux=x;
    x=y;
    y=aux;
}
class Heap{
    public:
        int v[N+1];
        int l;
        void swap(int&p1,int p2){
            swapInt(this->v[p1],this->v[p2]);
            swapInt(heapPos_to_pos[p1],heapPos_to_pos[p2]);
            pos_to_heapPos[heapPos_to_pos[p1]]=p1;
            pos_to_heapPos[heapPos_to_pos[p2]]=p2;
            p1=p2;
        }
        void push(int x,int pos){
            int p=l+1;
            this->v[++this->l]=x;
            pos_to_heapPos[pos]=l;
            heapPos_to_pos[l]=pos;
            while(this->v[p]<this->v[p/2])
                this->swap(p,p/2);
        }
        void pop(int p){
            this->v[p]=this->v[this->l];
            heapPos_to_pos[p]=heapPos_to_pos[l];
            pos_to_heapPos[heapPos_to_pos[p]]=p;
            while(this->v[p]<this->v[p/2])
                this->swap(p,p/2);
            while(p*2<=this->l)
                if(p*2+1<=this->l)
                    if(this->v[p*2]<this->v[p*2+1])
                        if(this->v[p*2]<this->v[p])
                            this->swap(p,p*2);
                        else
                            break;
                    else
                        if(this->v[p*2+1]<this->v[p])
                            this->swap(p,p*2+1);
                        else
                            break;
                else
                    if(this->v[p*2]<this->v[p])
                        this->swap(p,p*2);
                    else
                        break;
        }
        int top(){
            return this->v[1];
        }
};
Heap heap;
FILE*in,*out;
int noQueries,queryType,x,time;
void scan(){
    fscanf(in,"%d",&noQueries);
}
void init(){
    in=fopen("heapuri.in","r");
    out=fopen("heapuri.out","w");
    scan();
}
void scanQuery(){
    fscanf(in,"%d",&queryType);
    if(queryType!=3)
        fscanf(in,"%d",&x);
}
void initQuery(){
    scanQuery();
}
void solveQuery(){
    if(queryType==1)
        heap.push(x,++time);
    else if(queryType==2)
        heap.pop(pos_to_heapPos[x]);
    else
        fprintf(out,"%d\n",heap.top());
}
int main(){
    init();
    while(noQueries!=0){
        initQuery();
        solveQuery();
        noQueries--;
    }
    return 0;
}