Beautiful Subarrays (01字典树 瞎搞)

题意:
在这里插入图片描述题解:
一看问的是子序列,并且还是异或。
首先想到01字典树,再一看让你求子序列的个数,大致是想让你把这个序列进行前缀异或处理后然后再01字典树上进行操作吧。

假设01字典树往左边是0右边是1
对于如何寻找k这个值,肯定是顺着k这个值对应的链下去,那么对于这条链右边的链来说,值都是大于k的。那么我们在递归k的时候,对于右边的链直接把他们的个数给加上即可。这题比较难处理的是对于异或前缀的一个转换。

特别注意叶子节点需要特判一下。

大佬的代码:

#include<bits/stdc++.h>
//#define int long long
using namespace std;
#define ll long long
const int maxn = 1e6+10+1;

int trie[maxn*32][2];
ll cnt[maxn*32];
int tot=1;  //从一开始是因为node起点为1,node从1开始是防止node=0的干扰
void ins(int x){
    int node=1;
    for(int i=31;i>=0;i--){
        int t=(x>>i)&1;
        if(!trie[node][t]){
            trie[node][t]=++tot;
        }
        node=trie[node][t];
        cnt[node]++;
    }
}
ll query(int x,int k){
    int node=1;
    ll res=0;
    for(int i=31;i>=0;i--){
        int now=(x>>i)&1;
        int t=(k>>i)&1;
        if(t==1) node=trie[node][!now];
        else{
            res+=cnt[trie[node][!now]];
            node=trie[node][now];
        }
    }
    res+=cnt[node];
    return res;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,k;
    cin>>n>>k;
    int pre=0;
    ll ans=0;
    ins(0);
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        pre^=x;
        ans+=query(pre,k);
        ins(pre);
    }
    cout<<ans<<endl;
}

我的沙口代码:

#include<bits/stdc++.h>
//#define int long long
using namespace std;
#define ll long long
const int maxn = 1e6+10+1;

int trie[maxn*32][2];
ll cnt[maxn*32];
int tot;
void ins(int x){
    int node=0;
    for(int i=31;i>=0;i--){
        int t=(x>>i)&1;
        if(!trie[node][t]){
            trie[node][t]=++tot;
        }
        node=trie[node][t];
        cnt[node]++;
    }
}
ll query(int x,int k){
    int node=0;
    ll res=0;
    for(int i=31;i>=0;i--){
        int now=(x>>i)&1;
        int t=(k>>i)&1;

        //cout<<"pre  now "<<i<<" "<<now<<" "<<t<<endl;  //末尾结点的处理

        if(i==0){
            if(now==1) res+=cnt[trie[node][t^1]];
            else res+=cnt[trie[node][t]];
            //break;
        }

        if(now==1&&t==0){
            if(trie[node][t]) res+=cnt[trie[node][t]];
            if(trie[node][1]) node=trie[node][1];
            else break;
        }
        if(now==1&&t==1){
            if(trie[node][0]) node=trie[node][0];
            else break;
        }
        if(now==0&&t==0){
            if(trie[node][1]) res+=cnt[trie[node][1]];
            if(trie[node][t]) node=trie[node][t];
            else break;
        }
        if(now==0&&t==1){
            if(trie[node][t]) node=trie[node][t];
            else break;
        }

    }
    return res;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,k;
    cin>>n>>k;
    int pre=0;
    ll ans=0;
    ins(0);
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        //if(x>=k) ans++;
        pre^=x;
        //cout<<"debug  "<<pre<<endl;
        ans+=query(pre,k);
        ins(pre);
        //cout<<"debug  "<<ans<<endl;
    }
    cout<<ans<<endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值