• 2016.1.9


    可持久化trie处理抑或和最大值

    用trie树从上往下贪心的思想查询区间抑或最大值

    后缀抑或上整体转成维护前缀,方便处理修改

    题目

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define maxn 600020

    struct node{
    int next[2],val;
    }trie[maxn * 30];
    int root[maxn],a[maxn],data[maxn];
    int n,m,tot,xorsum;

    int insert(int now,int x,int bit){
    int cur = ++tot,c = (x >> (bit - 1)) & 1; //不是 x >> (bit - 1)
    trie[cur].val = trie[now].val + 1;
    if ( !bit ) return cur;
    trie[cur].next[c] = insert(trie[now].next[c],x,bit - 1);
    trie[cur].next[c ^ 1] = trie[now].next[c ^ 1];
    return cur;
    }
    int query(int a,int b,int x,int bit){
    int c = (x >> (bit - 1)) & 1,ans = 0;
    if ( !bit ) return 0;
    if ( (trie[trie[a].next[c ^ 1]].val - trie[trie[b].next[c ^ 1]].val) > 0 ){
    ans += query(trie[a].next[c ^ 1],trie[b].next[c ^ 1],x,bit - 1);
    ans += (1 << (bit - 1));
    }
    else{
    ans += query(trie[a].next[c],trie[b].next[c],x,bit - 1);
    }
    /*
    else{
    if ( trie[trie[a].next[c ^ 1]].val - trie[trie[b].next[c ^ 1]].val ){
    ans += query(trie[a].next[c ^ 1],trie[b].next[c ^ 1],x,bit - 1);
    ans += (1 << (bit - 2));
    }
    else{
    ans += query(trie[a].next[c],trie[b].next[c],x,bit - 1);
    }
    }*/
    return ans;
    }
    int main(){
    freopen("xor.in","r",stdin);
    freopen("xor.out","w",stdout);
    scanf("%d %d",&n,&m);
    for (int i = 1 ; i <= n ; i++) scanf("%d",&data[i]);
    a[1] = data[1];
    for (int i = 2 ; i <= n ; i++) a[i] = data[i] ^ a[i - 1];
    xorsum = a[n];
    for (int i = 1 ; i <= n ; i++){
    root[i] = insert(root[i - 1],a[i],25);
    /*cout<<i<<endl;
    for (int j = root[i] ; j <= tot ; j++){
    cout<<j<<" "<<trie[j].val<<" next: "<<trie[j].next[0]<<" "<<trie[j].next[1]<<endl;
    }
    cout<<endl;*/
    }
    while ( m-- ){
    char ch;
    scanf("%s",&ch);
    if ( ch == 'A' ){
    scanf("%d",&data[++n]);
    a[n] = a[n - 1] ^ data[n];
    xorsum ^= data[n];
    root[n] = insert(root[n - 1],a[n],25);
    /* cout<<n<<endl;
    for (int i = root[n] ; i <= tot ; i++){
    cout<<i<<" "<<trie[i].val<<" next: "<<trie[i].next[0]<<" "<<trie[i].next[1]<<endl;
    }
    cout<<endl;*/
    }
    else{
    int l,r,x;
    scanf("%d %d %d",&l,&r,&x);
    x ^= xorsum;
    if ( r == 1 ) printf("%d ",x);
    else
    printf("%d ",query(root[r - 1],root[max(l - 2,0)],x,25)); //因为将后缀转化为前缀来处理,所以区间整体向左平移1,保证r,l两个端点可以被处理
    }
    }
    return 0;
    }

  • 相关阅读:
    Python randrange() 函数
    200行Python代码实现2048
    select默认下拉箭头改变、option样式清除
    图片垂直居中
    去除select边框和三角-----appearance:none
    原生 js 实现点击按钮复制文本
    This dependency was not found: * !!vue-style-loader!css-loader?……解决方案
    vue项目启动时将localhost替换成指定ip地址
    安装cnpm
    vue 项目要使用的库
  • 原文地址:https://www.cnblogs.com/zqq123/p/5119714.html
Copyright © 2020-2023  润新知