#include <iostream>
#include <unordered_set>
#include <algorithm>

void find_common_values(int** A, int rowsA, int colsA, int** B, int rowsB, int colsB, int** C, int rowsC, int colsC, int*& common_values, int& common_size) {
    std::unordered_set<int> setA, setB, setC;
    for (int i = 0; i < rowsA; ++i) {
        for (int j = 0; j < colsA; ++j) {
            setA.emplace(A[i][j]);
        }
    }
    for (int i = 0; i < rowsB; ++i) {
        for (int j = 0; j < colsB; ++j) {
            setB.emplace(B[i][j]);
        }
    }
    for (int i = 0; i < rowsC; ++i) {
        for (int j = 0; j < colsC; ++j) {
            setC.emplace(C[i][j]);
        }
    }

    int* temp = new int[setA.size()];
    int index = 0;
    for (int val : setA) {
        if (setB.count(val) && setC.count(val)) {
            temp[index++] = val;
        }
    }

    common_size = index;
    common_values = new int[common_size];
    std::copy(temp, temp + common_size, common_values);
    delete[] temp;
}

void find_unique_values(int** A, int rowsA, int colsA, int** B, int rowsB, int colsB, int** C, int rowsC, int colsC, int*& unique_values, int& unique_size) {
    std::unordered_set<int> setA, setB, setC;
    for (int i = 0; i < rowsA; ++i) {
        for (int j = 0; j < colsA; ++j) {
            setA.emplace(A[i][j]);
        }
    }
    for (int i = 0; i < rowsB; ++i) {
        for (int j = 0; j < colsB; ++j) {
            setB.emplace(B[i][j]);
        }
    }
    for (int i = 0; i < rowsC; ++i) {
        for (int j = 0; j < colsC; ++j) {
            setC.emplace(C[i][j]);
        }
    }

    int* temp = new int[setA.size() + setB.size() + setC.size()];
    int index = 0;
    for (int val : setA) {
        if (!setB.count(val) && !setC.count(val)) {
            temp[index++] = val;
        }
    }
    for (int val : setB) {
        if (!setA.count(val) && !setC.count(val)) {
            temp[index++] = val;
        }
    }
    for (int val : setC) {
        if (!setA.count(val) && !setB.count(val)) {
            temp[index++] = val;
        }
    }

    unique_size = index;
    unique_values = new int[unique_size];
    std::copy(temp, temp + unique_size, unique_values);
    delete[] temp;
}

void find_common_values_AC(int** A, int rowsA, int colsA, int** C, int rowsC, int colsC, int*& common_values, int& common_size) {
    std::unordered_set<int> setA, setC;
    for (int i = 0; i < rowsA; ++i) {
        for (int j = 0; j < colsA; ++j) {
            setA.emplace(A[i][j]);
        }
    }
    for (int i = 0; i < rowsC; ++i) {
        for (int j = 0; j < colsC; ++j) {
            setC.emplace(C[i][j]);
        }
    }

    int* temp = new int[setA.size()];
    int index = 0;
    for (int val : setA) {
        if (setC.count(val)) {
            temp[index++] = val;
        }
    }

    common_size = index;
    common_values = new int[common_size];
    std::copy(temp, temp + common_size, common_values);
    delete[] temp;
}

void find_negative_values(int** A, int rowsA, int colsA, int** B, int rowsB, int colsB, int** C, int rowsC, int colsC, int*& negative_values, int& negative_size) {
    std::unordered_set<int> set;
    for (int i = 0; i < rowsA; ++i) {
        for (int j = 0; j < colsA; ++j) {
            if (A[i][j] < 0) {
                set.emplace(A[i][j]);
            }
        }
    }
    for (int i = 0; i < rowsB; ++i) {
        for (int j = 0; j < colsB; ++j) {
            if (B[i][j] < 0) {
                set.emplace(B[i][j]);
            }
        }
    }
    for (int i = 0; i < rowsC; ++i) {
        for (int j = 0; j < colsC; ++j) {
            if (C[i][j] < 0) {
                set.emplace(C[i][j]);
            }
        }
    }

    negative_size = set.size();
    negative_values = new int[negative_size];
    int index = 0;
    for (int val : set) {
        negative_values[index++] = val;
    }
}

int main() {
    int rowsA = 3, colsA = 3;
    int rowsB = 3, colsB = 3;
    int rowsC = 3, colsC = 3;

    int** A = new int*[rowsA];
    int** B = new int*[rowsB];
    int** C = new int*[rowsC];

    for (int i = 0; i < rowsA; ++i) {
        A[i] = new int[colsA];
        B[i] = new int[colsB];
        C[i] = new int[colsC];
    }

    A[0][0] = 1; A[0][1] = 2; A[0][2] = 3;
    A[1][0] = 4; A[1][1] = 5; A[1][2] = 6;
    A[2][0] = 7; A[2][1] = 8; A[2][2] = 9;

    B[0][0] = 1; B[0][1] = 2; B[0][2] = 3;
    B[1][0] = 4; B[1][1] = 5; B[1][2] = 6;
    B[2][0] = 7; B[2][1] = 8; B[2][2] = 9;

    C[0][0] = 1; C[0][1] = 2; C[0][2] = 3;
    C[1][0] = 4; C[1][1] = 5; C[1][2] = 6;
    C[2][0] = 7; C[2][1] = 8; C[2][2] = 9;

    int* common_values;
    int common_size;
    find_common_values(A, rowsA, colsA, B, rowsB, colsB, C, rowsC, colsC, common_values, common_size);

    int* unique_values;
    int unique_size;
    find_unique_values(A, rowsA, colsA, B, rowsB, colsB, C, rowsC, colsC, unique_values, unique_size);

    int* common_values_AC;
    int common_size_AC;
    find_common_values_AC(A, rowsA, colsA, C, rowsC, colsC, common_values_AC, common_size_AC);

    int* negative_values;
    int negative_size;
    find_negative_values(A, rowsA, colsA, B, rowsB, colsB, C, rowsC, colsC, negative_values, negative_size);

    std::cout << "Общие значения для A, B, C: ";
    for (int i = 0; i < common_size; ++i) {
        std::cout << common_values[i] << " ";
    }
    std::cout << std::endl;

    std::cout << "Уникальные значения для A, B, C: ";
    for (int i = 0; i < unique_size; ++i) {
        std::cout << unique_values[i] << " ";
    }
    std::cout << std::endl;

    std::cout << "Общие значения для A и C: ";
    for (int i = 0; i < common_size_AC; ++i) {
        std::cout << common_values_AC[i] << " ";
    }
    std::cout << std::endl;

    std::cout << "Отрицательные значения для A, B, C: ";
    for (int i = 0; i < negative_size; ++i) {
        std::cout << negative_values[i] << " ";
    }
    std::cout << std::endl;

    delete[] common_values;
    delete[] unique_values;
    delete[] common_values_AC;
    delete[] negative_values;

    for (int i = 0; i < rowsA; ++i) {
        delete[] A[i];
        delete[] B[i];
        delete[] C[i];
    }
    delete[] A;
    delete[] B;
    delete[] C;

    return 0;
}
