1 条题解

  • 1
    @ 2026-10-21 21:47:21
    #include <stdio.h>
    #include <stdlib.h>
    
    #define BT_BITS 30
    
    typedef struct {
        int (*ch)[2];       /* ch[node][0/1] */
        int *cnt;           /* 经过该节点的数的个数 */
        int tot;            /* 已使用的节点数,根固定为 1 */
        int size;           /* 当前集合元素个数 */
        int bits;           /* 位宽 */
        int maxNodes;
    } BinaryTrie;
    
    static int bt_newNode(BinaryTrie *t) {
        int id = t->tot++;
        t->ch[id][0] = t->ch[id][1] = 0;
        t->cnt[id] = 0;
        return id;
    }
    
    static void bt_init(BinaryTrie *t, int maxNodes, int bits) {
        t->maxNodes = maxNodes;
        t->bits = bits;
        t->tot = 2;              /* 根固定为 1,新节点从 2 开始 */
        t->size = 0;
        t->ch = (int (*)[2])calloc((size_t)maxNodes, sizeof(int[2]));
        t->cnt = (int *)calloc((size_t)maxNodes, sizeof(int));
    }
    
    static void bt_free(BinaryTrie *t) {
        free(t->ch);
        free(t->cnt);
        t->ch = NULL;
        t->cnt = NULL;
        t->tot = 2;
        t->size = 0;
    }
    
    static void bt_insert(BinaryTrie *t, int x) {
        int p = 1, b;
        for (b = t->bits - 1; b >= 0; --b) {
            int c = (x >> b) & 1;
            if (!t->ch[p][c]) t->ch[p][c] = bt_newNode(t);
            p = t->ch[p][c];
            ++t->cnt[p];
        }
        ++t->size;
    }
    
    static int bt_erase(BinaryTrie *t, int x) {
        int p = 1, b;
        for (b = t->bits - 1; b >= 0; --b) {
            int c = (x >> b) & 1;
            if (!t->ch[p][c] || t->cnt[t->ch[p][c]] == 0) return 0;
            p = t->ch[p][c];
        }
        p = 1;
        for (b = t->bits - 1; b >= 0; --b) {
            int c = (x >> b) & 1;
            p = t->ch[p][c];
            --t->cnt[p];
        }
        --t->size;
        return 1;
    }
    
    static int bt_maxXor(BinaryTrie *t, int x) {
        int p = 1, b, res = 0;
        if (t->size == 0) return 0;
        for (b = t->bits - 1; b >= 0; --b) {
            int c = (x >> b) & 1;
            int want = c ^ 1;
            if (t->ch[p][want] && t->cnt[t->ch[p][want]] > 0) {
                res |= (1 << b);
                p = t->ch[p][want];
            } else {
                p = t->ch[p][c];
            }
        }
        return res;
    }
    
    int main(void) {
        int n, m, i, x;
        BinaryTrie t;
        if (scanf("%d%d", &n, &m) != 2) return 0;
        bt_init(&t, (n + m + 5) * BT_BITS + 5, BT_BITS);
        for (i = 0; i < n; ++i) {
            scanf("%d", &x);
            bt_insert(&t, x);
        }
        for (i = 0; i < m; ++i) {
            int op;
            scanf("%d%d", &op, &x);
            if (op == 1) bt_insert(&t, x);
            else if (op == 2) bt_erase(&t, x);
            else printf("%d\n", bt_maxXor(&t, x));
        }
        bt_free(&t);
        return 0;
    }
    
    
    • 1

    信息

    ID
    795
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    677
    已通过
    131
    上传者