Chip Factory 01字典树

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<bitset>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ull unsigned long long
#define endl '\n'
#define clr_0(a) memset(a, 0, sizeof(a))
#define clr_INF(a) memset(a, INF, sizeof(a))
#define lowbit(x) x & -x
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define PB push_back
#define POP pop_back
const double pi = acos(-1);
const int maxn = 1e5 + 10;
const int maxm = (maxn<<5) + 5;
const ll mod = 998244353;
const int hash_mod = 19260817;
int T, n, m;
ll a[maxn];
int vis[maxn];
struct node{
    int ch[maxm][2];
    ll val[maxm];
    int cnt = 0;
    void insert(ll x){
        int rt = 0;
        for(int i = 32 ; i >= 0 ; -- i){
            int id = (x >> i) & 1;
            if(!ch[rt][id]){
                ch[cnt+1][0] = ch[cnt+1][1] = 0;
                val[cnt+1] = vis[cnt+1] = 0;
                ch[rt][id] = ++ cnt;
            }
            rt = ch[rt][id];
            vis[rt] ++;
        }
        val[rt] = x;
    }
    ll query(ll x){
        int rt = 0;
        for(int i = 32 ; i >= 0 ; -- i){
            int id = (x >> i) & 1;
            if(ch[rt][id^1] && vis[ch[rt][id^1]]) rt = ch[rt][id^1];
            else rt = ch[rt][id];
        }
        return val[rt] ^ x;
    }
    void init(){
        cnt = 0;
        ch[0][0] = ch[0][1] = 0;
    }
    void upd(ll x, int y){
        int rt = 0;
        for(int i = 32 ; i >= 0 ; -- i){
            int id = (x >> i) & 1;
            rt = ch[rt][id];
            vis[rt] += y;
        }
    }
}Trie;

int main()
{
    scanf("%d", &T);
    for(int k = 1 ; k <= T ; ++ k){
        scanf("%d", &n);
        Trie.init();
        for(int i = 1 ; i <= n ; ++ i){
            scanf("%lld", &a[i]);
            Trie.insert(a[i]);
        }
        ll maxi = 0;
        for(int i = 1 ; i <= n ; ++ i){
            for(int j = i + 1 ; j <= n ; ++ j){
                Trie.upd(a[i], -1);
                Trie.upd(a[j], -1);
                maxi = max(maxi, Trie.query(a[i]+a[j]));
                Trie.upd(a[i], 1);
                Trie.upd(a[j], 1);
            }
        }
        cout << maxi << endl;
    }
    return 0;
}
上一篇:【原创】中断子系统-ARM GPIO中断处理流程


下一篇:字符设备驱动(1)代码分析---之gpio_get_value()