Chocolate Game
Difficulty: Hard

Laurel and Hardy have $n$ piles of chocolates with each pile containing some number of chocolates. The piles are arranged from left to right in a non decreasing order based on the number of chocolates in each pile. They play the following game. 

For every continuous subsequence of chocolate piles (at least 2 piles form a subsequence), Laurel and Hardy will play game on this subsequence of chocolate piles, Laurel plays first, and they play in turn. In one move, the player can choose one of the piles and remove at least one chocolate from it, but the non-decreasing order of the chocolate piles must be maintained. The last person to make a valid move wins. 

How many continuous subsequences of chocolate piles will Laurel win if both of them play optimally? The number of chocolates of each pile will be recovered after the game ends for each subsequences. 


Input Format:
The first line contains an integer $n$ denoting the number of piles.   
The second line contains the number of chocolates in each pile, arranged from left to right and separated by a single space between them. 


Output Format:
A single integer denoting the number of continuous subsequences of chocolate piles in which Laurel will win.


Sample Input:
    5
    1 1 2 2 3


Sample Output:
    5


Python 3 starter code:
#
# Complete the 'chocolateGame' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def chocolateGame(arr):
    # Write your code here



Editorial: 2020 September Editorial: Chocolate Game
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p>This is a challenging problem. In case of any queries beyond the scope of this editorial, contact our problem setter, WanBo: wanbo_hackerrank@gmail.com  </p>

<p>Let us start by simplifying this problem:
Given d[], can you win if you play first?  </p>

<pre><code>d[0] &lt;= d[1] &lt;= ... &lt;= d[n-1]  
</code></pre>

<p>Transform d[] to another array a[] using the following transformation: <br>
     a[i] = d[i+1] - d[i]    </p>

<p>So from the array d[] we get a new array a[]: <br>
    d[0], d[1], ... d[n-1] <br>
    d[1] - d[0], d[2] - d[1], ..., d[n-1] - d[n-2]  </p>

<pre><code>a[i]     = d[i + 1] - d[i]  
a[i - 1] = d[i] - d[i - 1]  
</code></pre>

<p>If we remove x from d[i]: <br>
    d[i] -= x ==&gt; a[i] += x and a[i - 1] -= x
==&gt; we remove x from the a[i - 1] and add it to a[i]  </p>

<p>This is a classic staircase NIM game.
a[] with N elements is a win state
&lt;==&gt; 0 != xorsum{a[i] | 0 &lt;= i &lt; N and (N - i) % 2 == 1} </p>

<p>Naive solution to the original problem:</p>


for(i = 0; i + 1 

<p>We transform this to: <br>
==&gt;</p>

<p>for(i = 0; i &lt; n; i++) 
    for(j = i + 1; j &lt; n; j++) {
        xor = 0
        for(k = j; k &gt;= i; k -= 2) xor ^= d[k]
        if(xor != 0) res++ 
    }</p>

<p>==&gt; The inner for(k = j; k &gt;= i; k -= 2) can be easily optimized by maintain the xor prefix sum of odd position and even position separately.  O(n^3) ==&gt; O(n^2)  </p>

<p>==&gt; 
We can find the number of losing states.
How many y exist such that x xor y == 0 for a given x?  </p>

<p>&lt;==&gt; Find the number of elements equals to x. 
We can define a data structure to optimize this to O(n logn). 
This need enough analysis and careful coding, but it is not the hardest part of this challenge. <br>
<br></p>

<h3>C++ Solution</h3>  


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define LL long long
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)    cerr "#b": "void PV(T a, T b) {while(a != b)cout inline bool chmin(T &amp;a, T b) {return a &gt; b ? a = b, 1 : 0;}
templateinline bool chmax(T &amp;a, T b) {return a  v(d + i, d + j + 1);
            for(int k = (int) v.size() - 1; k &gt; 0; k--) v[k] -= v[k - 1];
            reverse(ALL(v));
            int xr = 0;
            for(int k = 0; k  0) res++;
        }
        P(i);
    }
    return res;
}

map M;

LL solve() {
    LL res = 0;
    {
        M.clear();
        M[0] = 1;
        int xr = 0;
        for(int i = n - 1; i &gt;= 0; i -= 2) {
            int t = d[i];
            if(i &gt; 0) t = d[i] - d[i - 1];
            res += M[xr ^ d[i]];
            if(i &gt; 0) res += M[xr ^ t];
            xr ^= t;
            M[xr]++;
        }
    }
    {
        M.clear();
        M[0] = 1;
        int xr = 0;
        for(int i = n - 2; i &gt;= 0; i -= 2) {
            int t = d[i];
            if(i &gt; 0) t = d[i] - d[i - 1];
            res += M[xr ^ d[i]];
            if(i &gt; 0) res += M[xr ^ t];
            xr ^= t;
            M[xr]++;
        }
    }
    res = (LL) n * (n - 1) / 2 - res;
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin &gt;&gt; N;
    assert(1 &gt; d[i];
        assert(1 = d[i - 1]);
    n = N;
    //LL res1 = brute();
    LL res2 = solve();
    cout 

Reference solution (C++):
###C++  
```cpp  
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define MM(a,x) memset(a,x,sizeof(a));
#define ALL(x)  (x).begin(), (x).end()
#define P(x)    cerr<<"["#x<<" = "<<x<<"]\n";
#define PP(x,i)    cerr<<"["#x<<i<<" = "<<x[i]<<"]\n";
#define P2(x,y)    cerr<<"["#x" = "<<x<<", "#y" = "<<y<<"]\n";
#define TM(a,b)    cerr<<"["#a" -> "#b": "<<1e3*(b-a)/CLOCKS_PER_SEC<<"ms]\n";
#define mp make_pair
#define pb push_back
template<class T>void PV(T a, T b) {while(a != b)cout << *a++, cout << (a != b ? " " : "\n");}
template<class T>inline bool chmin(T &a, T b) {return a > b ? a = b, 1 : 0;}
template<class T>inline bool chmax(T &a, T b) {return a < b ? a = b, 1 : 0;}
const int inf = 0x3f3f3f3f;
const int mod = int(1e9) + 7;

int N, n;
int d[100010];
int s[100010];
int s1[100010];
int s2[100010];

LL brute() {
    LL res = 0;
    for(int i = 0; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            vector<int> v(d + i, d + j + 1);
            for(int k = (int) v.size() - 1; k > 0; k--) v[k] -= v[k - 1];
            reverse(ALL(v));
            int xr = 0;
            for(int k = 0; k < v.size(); k += 2) xr ^= v[k];
            if(xr > 0) res++;
        }
        P(i);
    }
    return res;
}

map<int, int> M;

LL solve() {
    LL res = 0;
    {
        M.clear();
        M[0] = 1;
        int xr = 0;
        for(int i = n - 1; i >= 0; i -= 2) {
            int t = d[i];
            if(i > 0) t = d[i] - d[i - 1];
            res += M[xr ^ d[i]];
            if(i > 0) res += M[xr ^ t];
            xr ^= t;
            M[xr]++;
        }
    }
    {
        M.clear();
        M[0] = 1;
        int xr = 0;
        for(int i = n - 2; i >= 0; i -= 2) {
            int t = d[i];
            if(i > 0) t = d[i] - d[i - 1];
            res += M[xr ^ d[i]];
            if(i > 0) res += M[xr ^ t];
            xr ^= t;
            M[xr]++;
        }
    }
    res = (LL) n * (n - 1) / 2 - res;
    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin >> N;
    assert(1 <= N && N <= 100000);
    for(int i = 0; i < N; i++) {
        cin >> d[i];
        assert(1 <= d[i] && d[i] <= 1000000000);
    }
    for(int i = 1; i < N; i++) assert(d[i] >= d[i - 1]);
    n = N;
    //LL res1 = brute();
    LL res2 = solve();
    cout << res2 << endl;
    //P2(res1, res2);
    //assert(res1 == res2);
    return 0;
}
```