ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeForces 665E Beautiful Subarrays 字典树

2019-05-20 16:53:09  阅读:333  来源: 互联网

标签:Beautiful rt const int Subarrays LL 30 665E define


Beautiful Subarrays   

题解:

把数字转化成2进制之后,用字典树去维护。

 

想到字典树之后就应该是一道很容易写的题目了。

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 1e6 + 100;
int n, k;
int a[N];
int s[30], ss[30];
int tree[1<<24][2];
int cnt[1<<24];
int tot = 2;
void add(){
    int rt = 1;
    for(int i = 0; i < 30; ++i){
        int id = s[i];
        if(!tree[rt][id]) tree[rt][id] = ++tot;
        ++cnt[tree[rt][id]];
        rt = tree[rt][id];
    }
}
void add(int x){
    for(int i = 29; i >= 0; --i){
        s[i] =  + (x & 1);
        x >>= 1;
    }
    add();
}
LL ans = 0;
void Find(){
    int rt = 1;
    for(int i = 0; i < 30; ++i){
        if(!rt) return ;
        if(s[i] == 0 && ss[i] == 0){
//            cout << " *** " << endl;
            ans += cnt[tree[rt][1]];
            rt = tree[rt][0];
        }
        else if(s[i] == 0 && ss[i] == 1){
            rt = tree[rt][1];
        }
        else if(s[i] == 1 && ss[i] == 0){
//            cout << "??" << endl;
            ans += cnt[tree[rt][0]];
            rt = tree[rt][1];
        }
        else if(s[i] == 1 && ss[i] == 1){
            rt = tree[rt][0];
        }
//        cout << i << " " << ss[i] << " " <<ans << endl;
    }
    ans += cnt[rt];
}

int main(){
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for(int i = 29; i >= 0; --i){
        ss[i] =  + (k & 1);
        k >>= 1;
    }
    add(0);
    for(int i = 1; i <= n; ++i){
        a[i] ^= a[i-1];
        add(a[i]);
        Find();
    }
    cout << ans << endl;
    return 0;
}
View Code

 

标签:Beautiful,rt,const,int,Subarrays,LL,30,665E,define
来源: https://www.cnblogs.com/MingSD/p/10894923.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有