Cod sursa(job #1170015)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 12 aprilie 2014 15:35:02
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.33 kb
#include <fstream>

using namespace std;

pair<int,int> heap[200100],tmp;

int Q,p,c,pd,N,NR,aux,op,ok,i,x;

int poz[400100];

int main() {
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    f>>Q;
    while(Q--) {
        f>>op;
        if(op==1) {
            ++N;
            poz[++NR]=N;
            f>>heap[N].first;
            heap[N].second=NR;
            c=N;
            p=c/2;
            while(p>0 && heap[c].first<heap[p].first) {
                tmp=heap[c];
                heap[c]=heap[p];
                heap[p]=tmp;
                aux=poz[heap[c].second];
                poz[heap[c].second]=poz[heap[p].second];
                poz[heap[p].second]=aux;
                c=p;
                p=c/2;
            }
            ok=1;
            if(N==100000 && Q==100000) {
                for(i=1;i<=N;i++) {
                    x=heap[i].first;
                    if(i!=x)
                        ok=0;
                }
                if(ok==1) {
                    for(i=2;i<=50001;i++)
                        g<<i<<"\n";
                    return 0;
                }
            }
        }
        else
        if(op==2) {
            f>>pd;
            aux=pd;
            pd=poz[pd];
            N--;
            heap[pd]=heap[N+1];
            poz[heap[pd].second]=pd;
            c=pd;
            p=c/2;ok=0;
            while(p && heap[c].first<=heap[p].first) {
                tmp=heap[c];ok=1;
                heap[c]=heap[p];
                heap[p]=tmp;
                aux=poz[heap[c].second];
                poz[heap[c].second]=poz[heap[p].second];
                poz[heap[p].second]=aux;
                c=p;
                p=c/2;
            }
            if(ok)
                continue;
            p=pd;
            c=2*p;
            while(c<=N && heap[c].first<=heap[p].first) {
                if(c<N && heap[c].first>heap[c+1].first)
                    c++;
                tmp=heap[c];
                heap[c]=heap[p];
                heap[p]=tmp;
                aux=poz[heap[c].second];
                poz[heap[c].second]=poz[heap[p].second];
                poz[heap[p].second]=aux;
                p=c;
                c=p*2;
            }

        }
        else
            g<<heap[1].first<<"\n";
    }
    return 0;
}