Winning Segments
Time limit4sMemory limit256 MB
Given a permutation of 0..2^M-1, count the nonempty subarrays whose XOR can be made equal to 2^M-1 by one mandatory swap of two elements.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Prefix sum, Combinatorics, Array
- Solved
- No attempts yet
Problem
You are given an integer and an array of length that holds each of exactly once.
The computer picks one nonempty contiguous segment of . You then pick two different positions and swap the two numbers sitting there. The swap is mandatory, and the two positions may both lie inside the segment, both outside it, or one on each side. After the swap you win if the bitwise XOR of every number inside the segment the computer picked is exactly .
The computer has segments to pick from. Count how many of them let you win.
Input
The first line contains the integer ().
The second line contains the numbers of , separated by spaces. They form a permutation of .
Output
Print the number of segments that let you win, on one line.
Hint
In the first example, if the computer picks the segment 1 2 3, you win by swapping 0 and 3. In that example every segment except the whole array lets you win.
In the second example, if the computer picks the whole array 3 7 0 4 6 1 5 2, the XOR of the segment is 0 and swapping any two numbers leaves it at 0.