Cod sursa(job #1599243)

Utilizator BrandonChris Luntraru Brandon Data 13 februarie 2016 18:45:16
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.89 kb
/*#include <fstream>

#define INF_VAL 1000000005
#define INF_POS 200005
#define MAXSIZE 200005

using namespace std;

ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");

class node {
    public:
    int val, pos;
};

node heap[MAXSIZE];
int heap_size, order[MAXSIZE], op, op_nr, order_size;

void insert_node(int node_val) {
    int val_pos, curr_pos;
    ++heap_size;
    heap[heap_size].val = node_val;
    val_pos = heap_size;
    curr_pos = heap_size;
    while(curr_pos >= 1) {
        if(heap[val_pos].val <= heap[curr_pos].val) {
            swap(heap[val_pos].val, heap[curr_pos].val);
        }
        val_pos = curr_pos;
        curr_pos /= 2;
    }
}

void remove_node(int pos) {
    int curr_pos;
    curr_pos = heap_size;
    heap[pos].val = INF_VAL;
    heap[pos].pos = INF_POS;
    curr_pos = pos + 1;
    while(curr_pos <= heap_size + 1) {
        if(heap[pos].val > heap[curr_pos].val) {
            if(heap[curr_pos].val > heap[curr_pos + 1].val and curr_pos < heap_size) {
                if(heap[pos].val > heap[curr_pos + 1].val) {
                    ++curr_pos;
                }
            }
            swap(heap[pos], heap[curr_pos]);
            pos = curr_pos;
        }
        curr_pos *= 2;
    }
    --heap_size;
}

void read() {
    cin >> op_nr;
    for(int i = 1; i <= op_nr; ++i) {
        cin >> op;
        if(op == 1) {
            int node_val;
            cin >> node_val;
            insert_node(node_val);
            ++order_size;
            order[order_size] = node_val;
        }
        if(op == 2) {
            int node_order;
            cin >> node_order;
            remove_node(order[node_order]);
        }
        if(op == 3) {
            cout << heap[1].val << '\n';
        }
    }
}

int main() {
    read();
    return 0;
}*/
#include <fstream>
#include <queue>

#define MAX_SIZE 200005

using namespace std;

ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");

class node {
    public:
    int val, order;
};

class cmp {
    public:
    inline bool operator () (node a, node b) {
        return a.val > b.val;
    }
};

priority_queue <node, vector <node>, cmp> prio_q;

int heap_size, op, op_nr, current_pos;
bool removed[MAX_SIZE];

void read() {
    cin >> op_nr;
    for(int i = 1; i <= op_nr; ++i) {
        cin >> op;
        if(op == 1) {
            int node_val;
            cin >> node_val;
            ++current_pos;
            prio_q.push( {node_val, current_pos} );
        }
        if(op == 2) {
            int node_order;
            cin >> node_order;
            removed[node_order] = true;
        }
        if(op == 3) {
            while(removed[prio_q.top().order] ) {
                prio_q.pop();
            }
            cout << prio_q.top().val << '\n';
        }
    }
}

int main() {
    read();
    return 0;
}