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;
    }
  • 相关阅读:
    C#学习三之几个关键字Static,ref&out,get&set,readonly
    C#学习二之浅析var类型和enum枚举类型
    C#学习一之HelloWorld
    泛型(二)
    泛型(一)
    JavaScript 使用
    JavaScript
    C# 自定义控件制作和使用实例(winform)
    ThinkPHP 参数绑定原理
    处理jQuery append加入的元素 绑定事件无效的方法
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433687.html
Copyright © 2011-2022 走看看