// 包含C++标准库中的头文件 #include <bits/stdc++.h> // 使用std命名空间 using namespace std; // 使用LL作为long long的别名

// 定义常量inf为2^30+114514 const int inf = (1 << 30) + 114514; // 定义常量SZ为2,表示二进制位数 const int SZ = 2;

// 定义Trie类,模板参数为T和K template<typename T, typename K> struct Trie { // 定义嵌套结构体node,用于表示Trie中的节点 struct node { K value; // 节点的值 int ans; // 节点的答案 int vis_count; // 节点的访问计数 array<int, SZ> children; // 子节点的索引数组

    // 构造函数,初始化节点的值和子节点数组
    node(K val) : value(val) {
        vis_count = 0;
        fill(children.begin(), children.end(), 0);
        ans = inf;
    }
};

// 定义Trie树的数组
vector<node> tree;

// 构造函数,初始化Trie树的根节点
Trie(K val) {
    tree.push_back(node(val));
}

// 将K类型的值转换为int类型的索引值
int cast(K val) {
    int ret = val;
    assert(ret < SZ and ret >= 0);
    return ret;
}

// 插入函数,向Trie树中插入一个序列
void insert(int v, int pos, int cur, const T& sequence, bool remove = false) {

    // 根据remove参数更新节点的访问计数
    if (remove) {
        tree[cur].vis_count -= 1;
    }
    else {
        tree[cur].vis_count += 1;
    }

    // 如果pos小于序列的长度,则继续插入
    if (pos < sequence.size()) {

        // 获取当前位置的值
        K value = sequence[pos];

        // 如果子节点不存在,则创建子节点
        if (tree[cur].children[cast(value)] == 0) {
            tree[cur].children[cast(value)] = (int)tree.size();
            tree.emplace_back(v);
        }

        // 递归调用插入函数,插入下一个位置的值
        insert(v, pos + 1, tree[cur].children[cast(value)], sequence, remove);

        // 更新当前节点的值
        if (tree[cur].vis_count == 1) {
            int lson = tree[cur].children[cast(value)];
            int rson = tree[cur].children[cast(value) ^ 1];
            if (lson != 0 && tree[lson].vis_count != 0)
                tree[cur].value = tree[lson].value;
            else
                tree[cur].value = tree[rson].value;
        }

        // 更新当前节点的答案
        tree[cur].ans = inf;
        int lson = tree[cur].children[0], rson = tree[cur].children[1];
        if (lson != 0 && tree[lson].vis_count > 1)
            tree[cur].ans = min(tree[cur].ans, tree[lson].ans);
        if (rson != 0 && tree[rson].vis_count > 1)
            tree[cur].ans = min(tree[cur].ans, tree[rson].ans);
        if (lson != 0 && rson != 0 && tree[lson].vis_count == 1 && tree[rson].vis_count == 1)
            tree[cur].ans = tree[lson].value ^ tree[rson].value;
    }
    else {
        // 如果pos等于序列的长度,则更新当前节点的答案
        tree[cur].ans = inf;
        if (tree[cur].vis_count > 1)
            tree[cur].ans = 0;
    }
}

// 删除函数,从Trie树中删除一个序列
void remove(int v, const T& sequence) {
    insert(v, 0, 0, sequence, true);
}

// 获取最小答案的函数
int get_min() {
    return tree.front().ans;
}

};

// 主函数 int main(void) { // 关闭输入输出流的同步 ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // 创建Trie树对象tree,初始值为0 Trie<array<int, 30>, int> tree(0); // 定义一个lambda表达式tr2bin,用于将整数转换为二进制表示的数组 auto tr2bin = [](int x) { array<int, 30> bin{}; for (int i = 0; i < 30; ++i) { bin[i] = ((x >> (29 - i)) & 1); } return bin; };

// 输入查询次数
int q;
cin >> q;
// 循环处理每个查询
while (q--) {
    // 输入操作类型
    int op;
    cin >> op;
    // 根据操作类型执行相应操作
    if (op == 1) {
        // 如果操作类型为1,输入一个整数x,并将x插入到Trie树中
        int x;
        cin >> x;
        tree.insert(x, 0, 0, tr2bin(x));
    }
    else if (op == 2) {
        // 如果操作类型为2,输入一个整数x,并将x从Trie树中删除
        int x;
        cin >> x;
        tree.remove(x, tr2bin(x));
    }
    else {
        // 如果操作类型不为1或2,输出Trie树中的最小答案
        cout << tree.get_min() << '\n';
    }
}

return 0;
#include bitsstdc++husing namespace std;using LL = long long;const int inf = 1 30 + 114514;const int SZ = 2;templatetypename T typename Kstruct Trie struct node K value; int ans;

原文地址: https://www.cveoy.top/t/topic/hTSc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录