zoukankan      html  css  js  c++  java
  • Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson

    因为抑或,一眼字典树
    但是处理起来比较难

    #include<iostream>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 6e6+5;
    #define MS(x,y) memset(x,y,sizeof(x))
    #define MP(x, y) make_pair(x, y)
    const int INF = 0x3f3f3f3f;
    
    int nx[N][2];
    int cnt[N];
    int has[N];
    int tot;
    void insert(int x) {
        int rt = 0;
        for(int i = 18; i >= 0; --i) {
            int tt = (x>>i) & 1;
        //  printf("%d
    ", rt);
            if(!nx[rt][tt]) {
                nx[rt][tt] = ++tot;
            }
            rt = nx[rt][tt];
            cnt[rt] ++;
        }
    //  has[rt] = 1;
    }
    void search(int x) {
        int rt = 0;
        int ans = 0;
        for(int i = 18; i >= 0; --i) {
            int tt = (x >> i) & 1;
            if( (1<<i) - cnt[nx[rt][0 ^ tt]] ) {
                if(cnt[nx[rt][0 ^ tt]] == 0) break;
                rt = nx[rt][0 ^ tt];
            }else {
                if(cnt[nx[rt][1 ^ tt]] == 0) {
                //  printf("hh
    ");
                    ans += 1<<i;
                    break;
                }
    
                rt = nx[rt][1 ^ tt]; ans += 1<<i;
            }
        //  printf("%d %d
    ", rt, 0^tt);
        }
        printf("%d
    ", ans);
    }
    int main() {
        int n, m;
        while(~scanf("%d %d", &n, &m)) {
            MS(nx, 0);
            MS(cnt, 0);
            tot = 0;
            map<int, int> mp;
            for(int i = 1; i <= n; ++i) {
                int a; scanf("%d", &a); 
                if(mp.find(a) == mp.end()) insert(a);
                mp[a] ++;
            }
            int tmp = 0;
            for(int i = 0; i < m; ++i) {
                int a; scanf("%d", &a);
                tmp ^= a;
                search(tmp);
            }
        }
        return 0;
    }
  • 相关阅读:
    java中字符串截取
    Linux主机添加Windows字体
    oracle命令
    Oracle索引
    快速搜索多个word、excel等文件中内容
    oracle数据库修改字符集
    Linux服务器安装svn
    Linux定时任务增删改查等说明
    Linux按行读取文件内容
    linux远程通过shell脚本执行另一台linux机器上的shell文件
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433687.html
Copyright © 2011-2022 走看看