Cod sursa(job #2892875)

Utilizator ViAlexVisan Alexandru ViAlex Data 23 aprilie 2022 20:53:42
Problema Heapuri cu reuniune Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.13 kb
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;


class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char *nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser &operator>>(int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser &operator>>(long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

struct node {
    int value;
    node *next_sibling, *left_child;

    explicit node(int _value) : value(_value), next_sibling(nullptr), left_child(nullptr) {

    }

    void insert(node *other) {
        if (left_child == nullptr) {
            left_child = other;
        } else {
            other->next_sibling = left_child;
            left_child = other;
        }
    }
};

node *merge(node *a, node *b) {
    if (a == nullptr) {
        return b;
    }
    if (b == nullptr) {
        return a;
    }
    if (a->value > b->value) {
        a->insert(b);
        return a;
    } else {
        b->insert(a);
        return b;
    }
}

node *two_pass_merge(node *x) {
    if (x == nullptr || x->next_sibling == nullptr) {
        return x;
    } else {
        node *a = x;
        node *b = x->next_sibling;
        node *c = x->next_sibling->next_sibling;
        a->next_sibling = nullptr;
        b->next_sibling = nullptr;
        return merge(merge(a, b), two_pass_merge(c));
    }
}

node *delete_max(node *x) {
    return two_pass_merge(x->left_child);
}

int n, q;
node *sets[101];
InParser in("mergeheap.in");
ofstream out("mergeheap.out");

void read() {
    in >> n >> q;
    for (int i = 1; i <= n; i++) {
        sets[i] = nullptr;
    }
}


void query_insert(int set, int value) {
    node *new_node = new node(value);
    sets[set] = merge(sets[set], new_node);
}

void query_max(int set) {
    out << sets[set]->value << '\n';
    sets[set] = delete_max(sets[set]);
}

void query_merge(int set_a, int set_b) {
    sets[set_a] = merge(sets[set_a], sets[set_b]);
    sets[set_b] = nullptr;
}

void solve() {
    int a, b, c;
    while (q--) {
        in >> a;
        if (a == 1) {
            in >> b >> c;
            query_insert(b, c);
        } else if (a == 2) {
            in >> b;
            query_max(b);
        } else {
            in >> b >> c;
            query_merge(b, c);
        }
    }
}


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