1054D - Changing Array - CodeForces Solution


greedy implementation *1900

Please click on ads to support us..

Python Code:

n,k,*a=map(int,open(0).read().split());m={0:1};p=A=0
for a in a:p^=a;x,p=min((m.get(k,0),k)for k in[2**k-1^p,p]);A+=x;m[p]=x+1
print(n*-~n//2-A)

C++ Code:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <unordered_map>
#include <climits>
#include <unordered_set>

using namespace std;

typedef long long LL;

class CF1054D {
public:
    LL solve(const vector<int> &a, int k) {
        LL n = a.size();
        unordered_map<int, int> xor2cnt;
        int mask = (1 << k) - 1;

        xor2cnt[0]++;
        for (int i = 0, xorSum = 0; i < n; i++) {
            xorSum ^= a[i];
            int newXorSum = min(xorSum, xorSum ^ mask);
            xor2cnt[newXorSum]++;
        }
        LL ans = (1 + n) * n / 2;
        for (auto &ite : xor2cnt) {
            ans -= calcZero(ite.second);
        }
        return ans;
    }
    LL calcZero(LL cnt) {
        LL x = cnt / 2;
        LL y = cnt - x;
        return x * (x - 1) / 2 + y * (y - 1) / 2;
    }
};

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    printf("%lld\n", CF1054D().solve(a, k));
    return 0;
}


Comments

Submit
0 Comments
More Questions

762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping