using
System;
using
System.Collections.Generic;
public
class
GFG
{
public
static
List<
int
> countXorSubarr(
int
[] arr,
int
[,] queries,
int
n,
int
m)
{
int
[] temp_arr =
new
int
[arr.Length + 1];
temp_arr[0] = 0;
for
(
int
i = 1; i < temp_arr.Length; i++) {
temp_arr[i] = arr[i - 1];
}
arr = temp_arr;
int
[] count =
new
int
[n + 1];
int
[] sum =
new
int
[n + 1];
count[0] = sum[0] = 0;
for
(
int
i = 1; i <= n; i++) {
sum[i] = (sum[i - 1] + arr[i]) % 2;
count[i] = count[i - 1];
if
(sum[i] % 2 == 0)
count[i]++;
}
List<
int
> ans =
new
List<
int
>();
for
(
int
i = 0; i < queries.GetLength(0); i++)
{
int
L = queries[i,0], R = queries[i,1];
int
even = count[R] - count[L - 1];
int
odd = (R - L + 1) - even;
if
(sum[L - 1] == 1) {
int
temp = even;
even = odd;
odd = temp;
}
even++;
int
subCount = (even * (even - 1)) / 2 + (odd * (odd - 1)) / 2;
ans.Add(subCount);
}
return
ans;
}
public
static
int
[] GetRow(
int
[,] matrix,
int
row)
{
var
rowLength = matrix.GetLength(1);
var
rowVector =
new
int
[rowLength];
for
(
var
i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return
rowVector;
}
public
static
void
Main(String []args)
{
int
n = 5;
int
[] arr = { 1, 2, 9, 8, 7 };
int
m = 2;
int
[,] queries = { { 1, 5 }, { 3, 4 } };
List<
int
> ans = countXorSubarr(arr, queries, n, m);
foreach
(
int
x
in
ans)
Console.Write(x +
" "
);
Console.WriteLine(
""
);
}
}