Language: C++

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

typedef map<char, set<int> > M;

vector<int> conv_vec(set<int> st) {
    vector<int> vec;
    for(set<int>::iterator it = st.begin(); it != st.end(); ++it) {
        vec.push_back(*it);
    }
    return vec;
}

set<int> conv_set(vector<int> vec) {
    set<int> st;
    for(vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
        st.insert(*it);
    }
    return st;
}

void show(vector<int> A, vector<int> B, char order, vector<int> result)
{
    cout << "{ ";
    for (int i = 0; i < A.size(); i += 1) {
        cout << A[i] << " ";
    }
    cout << "} " << order << " { ";
    for (int i = 0; i < B.size(); i += 1) {
        cout << B[i] << " ";
    }
    cout << "} = { ";
    for (int i = 0; i < result.size(); i += 1) {
        cout << result[i] << " ";
    } 
    cout << "}" << endl;
    return;
}

set<int> calc(set<int> st_A, set<int> st_B, char order) {
    vector<int> result;
    vector<int> A = conv_vec(st_A), B = conv_vec(st_B);

    if (order == 'u') {
        set_union(begin(A), end(A), begin(B), end(B), inserter(result, end(result)));
    } else if (order == 'i') {
        set_intersection(begin(A), end(A), begin(B), end(B), inserter(result, end(result)));
    } else if (order == 'd') {
        set_difference(begin(A), end(A), begin(B), end(B), inserter(result, end(result)));
    } else if (order == 's') {
        set_symmetric_difference(begin(A), end(A), begin(B), end(B), inserter(result, end(result)));
    } else if (order == 'c') {
        set_difference(begin(A), end(A), begin(B), end(B), inserter(result, end(result)));
    }
    //show(A, B, order, result);
    return conv_set(result);
}

int p_skip(string str, int p) {
    int cnt = 1;
    while(p < str.size()) {
        if (str[p] == '(') {
            ++cnt;
        } else if (str[p] == ')') {
            --cnt;
        }
        ++p;
        if (cnt == 0) {
            break;
        }
    }
    return p;
}

set<int> parse(string str, M dataset, int start) {
    int p = start;
    set<int> A, B;
    if (str[p] == '(') {
        p += 1;
        A = parse(str, dataset, p);
        p = p_skip(str, p);
    } else if (str[p] == 'c') {
        p += 1;
        if(str[p] == '(') {
            p += 1;
            A = parse(str, dataset, p);
            p = p_skip(str, p);
        } else {
            A = dataset[str[p]];
            p += 1;
        }
        A = calc(dataset['U'], A, 'c');
    } else {
        A = dataset[str[p]];
        p += 1;
    }

    while(p < str.size()) {
        if (((string)("uids")).find(str[p]) == string::npos) {
            break;
        }

        char order = str[p];
        ++p;

        if (str[p] == '(') {
            p += 1;
            B = parse(str, dataset, p);
            p = p_skip(str, p);
        } else if (str[p] == 'c') {
            p += 1;
            if(str[p] == '(') {
                p += 1;
                B = parse(str, dataset, p);
                p = p_skip(str, p);
            } else {
                B = dataset[str[p]];
                p += 1;
            }
            B = calc(dataset['U'], B, 'c');
        } else {
            B = dataset[str[p]];
            p += 1;
        }

        A = calc(A, B, order);
    }

    return A;
}

void solve(string str, M dataset) {
    set<int> result = parse(str, dataset, 0);
    if (result.empty()) {
        cout << "NULL" << endl;
        return;
    }

    int cnt = 0;
    sort(result.begin(), result.end());
    for (set<int>::iterator it = result.begin(); it != result.end(); ++it) {
        if (cnt == (int)(result.size())-1) {
            cout << *it << endl;
        } else {
            cout << *it << " ";
            cnt += 1;
        }
    }
    return;
}

int main()
{
    char s;
    int t;
    set<int> U;
    string str;
    M dataset;

    while(!cin.eof()) {
        cin >> s >> t;
        if(s == 'R'){
            cin >> str;
            //cout << "str: " << str << endl;
            dataset['U'] = U;
            solve(str, dataset);
            dataset.clear();
            U.clear();
            continue;
        }
        set<int> st;
        int u;
        for (int i = 0; i < t; i += 1) {
            cin >> u;
            st.insert(u);
            U.insert(u);
        }
        dataset[s] = st;
    }

    return 0;
}

Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = _Rb_tree_const_iterator<int>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]':
/usr/include/c++/14/bits/stl_algo.h:4772:18:   required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = _Rb_tree_const_iterator<int>]'
 4772 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter());
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:148:9:   required from here
  148 |     sort(result.begin(), result.end());
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1906:50: error: no match for 'operator-' (operand types are 'std::_Rb_tree_const_iterator<int>' and 'std::_Rb_tree_const_iterator<int>')
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                           ~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/string:48,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:618:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  618 |     operator-(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:618:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:1906:50: note:   'std::_Rb_tree_const_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                           ~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1790 |     operator-(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:1906:50: note:   'std::_Rb_tree_const_iterator<int>' is not derived from 'const std::move_iterator<_IteratorL>'
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                           ~~~~~~~^~~~~~~~~