Cod sursa(job #1450737)

Utilizator razvan3895Razvan-Mihai Chitu razvan3895 Data 14 iunie 2015 15:17:52
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.02 kb
#include <cstdio>
#include <iostream>
#include <cstdlib>
using namespace std;

#define switch(a, b)    a = a ^ b; b = a ^ b; a = a ^ b;


class CronHeap {
private:
    unsigned *vect;
    unsigned *posInHeap;
    unsigned *posCron;
    int dim;
    int crt;
public:
    CronHeap() {
        crt = 0;
        dim = 0;
        vect = new unsigned[1025];
        posInHeap = new unsigned[1025];
        posCron = new unsigned[1025];
    }

    CronHeap(unsigned d) {
        crt = 0;
        dim = 0;
        vect = new unsigned[d + 1];
        posInHeap = new unsigned[d + 1];
        posCron = new unsigned[d + 1];
    }

    ~CronHeap() {
        delete[] vect;
        delete[] posInHeap;
        delete[] posCron;
    }

    int pushUp(unsigned i) {
        while(i > 1 && vect[i] < vect[(i >> 1)]) {
            switch(vect[i], vect[(i >> 1)]);
            switch(posInHeap[posCron[i]], posInHeap[posCron[(i >> 1)]]);
            switch(posCron[i], posCron[(i >> 1)]);
            i = (i >> 1);
        }
        return i;
    }

    void insert(unsigned x) {
        vect[++dim] = x;
        posCron[dim] = ++crt;
        posInHeap[crt] = dim;
        pushUp(dim);
    }

    void pushDown(unsigned i) {
        while(1) {
            bool left = false, right = false;
            if ((i << 1) + 1 <= dim && vect[(i << 1) + 1] < vect[(i << 1)] && vect[i] > vect[(i << 1) + 1])
                right = true;
            if((i << 1) <= dim && vect[i] > vect[(i << 1)])
                left = true;
            if(right) {
                switch(vect[i], vect[(i << 1) + 1]);
                switch(posInHeap[posCron[i]], posInHeap[posCron[(i << 1) + 1]]);
                switch(posCron[i], posCron[(i << 1) + 1]);
                i = (i << 1) + 1;
                continue;
            }
            if(left) {
                switch(vect[i], vect[(i << 1)]);
                switch(posInHeap[posCron[i]], posInHeap[posCron[(i << 1)]]);
                switch(posCron[i], posCron[(i << 1)]);
                i = (i << 1);
                continue;
            }
            break;
        }
    }

    void remove(unsigned i) {
        vect[posInHeap[i]] = vect[dim];
        posInHeap[posCron[dim]] = posInHeap[i];
        posCron[posInHeap[i]] = posCron[dim--];
        pushDown(pushUp(posInHeap[i]));
    }

    unsigned minim() {
        return vect[1];
    }

    friend std::ostream& operator<<(std::ostream& out, CronHeap &h) {
        for(int i = 1; i <= h.crt; ++i)
            if (h.posInHeap[i] != -1)
            cout << i << ' ' << h.vect[h.posInHeap[i]] << '\n';
        return out;
    }
};

int main() {
    unsigned n, op1, op2;
    CronHeap h(200001);
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &op1);
        if(op1 == 3) {
            cout << h.minim() << '\n';
            continue;
        }
        scanf("%d", &op2);
        if(op1 == 1) {
            h.insert(op2);
        }
        else {
            h.remove(op2);
        }
    }
    return 0;
}