码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1053 B - Vasya and Good Sequences

时间:2018-10-02 20:12:07      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:and   c++   force   print   codeforce   define   can   main   eof   

B - Vasya and Good Sequences

思路:

满足异或值为0的区间,必须满足一下条件:

1.区间中二进制1的个数和为偶数个;

2.区间二进制1的个数最大值的两倍不超过区间和.

如果区间长度大于128,第二个条件肯定满足,所以我们只要暴力区间长度小于128的就可以了

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 3e5 + 100;
LL a[N];
int cnt[N], sum[N][2];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 63; j++) {
           if(a[i] & (1LL << j)) cnt[i]++;
        }
    }
    sum[0][0] = 1;
    sum[0][1] = 0;
    int tot = 0;
    for (int i = 1; i <= n; i++) {
        sum[i][0] = sum[i-1][0];
        sum[i][1] = sum[i-1][1];
        tot += cnt[i];
        if(tot&1) sum[i][1]++;
        else sum[i][0]++;
    }
    LL ans = 0;
    for (int l = 1; l <= n; l++) {
        int up = min(l+127, n);
        int mx = -0x3f3f3f3f, tot = 0;
        for (int i = l; i <= up; i++) {
            mx = max(mx, cnt[i]);
            tot += cnt[i];
            if(tot%2 == 0 && tot >= mx*2) ans++;
        }
    }
    tot = 0;
    for (int i = 1; i <= 128; i++) tot += cnt[i];
    for (int i = 129; i <= n; i++) {
        tot += cnt[i];
        if(tot&1) ans += sum[i-129][1];
        else ans += sum[i-129][0];
    }
    printf("%lld\n", ans);
    return 0;
}

 

Codeforces 1053 B - Vasya and Good Sequences

标签:and   c++   force   print   codeforce   define   can   main   eof   

原文地址:https://www.cnblogs.com/widsom/p/9737489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!