E. Beautiful Subarrays(01字典树)


题意:

  • 给一个长度为n的数组a,问有多少个连续的子数组的异或和大于等于k

思路:

  • 01字典树
  • 建立前缀异或和数组,题目变为有多少个$a_i ˆ a_j(1 \leq i < j \leq n)$
  • 类似(几乎一模一样)的题目
  • 一次插入一个左端点,然后枚举右端点的情况

  • R ^ L $\leq$ k,从高位往低位

  • $k_i = 1$,必须$R_i$ ^ $L_i = 1$,走 $R_i $ ^ $ 1$;$R_i$ ^ $L_i = 0$会造成比 k 小,忽略

  • $k_i = 0$,若$R_i$ ^ $L_i = 1$,加上$R_i $ ^ $ 1 $这个方向,因为这个方向必然会比 k 小;若$R_i$ ^ $L_i = 0$,走$R_i $这个方向

 

#include<bits/stdc++.h>
#define debug1(a) cout<<#a<<'='<< a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<endl;
#define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<"  "<<#e<<" = "<<e<<endl;
#define endl "\n"
#define fi first
#define se second

//#define int long long
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
const int N = 1e6+10;
int son[32*N][2],idx;
int cnt[32*N];
int a[N];
int n,k;

void insert(int x)                
{
    int p = 0;
    for (int i = 30; i >= 0; i -- )
    {
        int &s = son[p][x >> i & 1];
        if (!s) s = ++ idx;
        p = s;
        cnt[p] ++;
    }
}

int get(int x,int R)
{
    int p = 0;
    int res = 0;
    for(int i = 30;i >= 0;i -- )
    {
        if(x >> i & 1)
        {
            p = son[p][(R>>i&1)^1];
        }else{
            res += cnt[son[p][(R>>i&1)^1]];
            p = son[p][R>>i&1];
        }
        if(p == 0)return res;
    }
    return res + cnt[p];
}

void solve() 
{
    cin >> n >> k;
    for(int i = 1;i <= n;i ++)
    {
        cin >> a[i];
        a[i] ^= a[i-1];
    }

    long long ans = 0;
    insert(0);
    for(int i = 1;i <= n;i ++)
    {
        ans += get(k,a[i]);
        insert(a[i]);
    }
    cout << ans << endl;
}

signed main()
{
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    */
    int T = 1;//cin >> T;

    while(T--){
        //puts(solve()?"YES":"NO");
        solve();
    }
    return 0;

}
/*

*/

 

posted @ 2023-01-11 19:13  俄罗斯刺沙蓬  阅读(24)  评论(0编辑  收藏  举报
返回顶端