using
System;
class
GFG{
static
int
getXOR(
int
[]BITree,
int
index)
{
int
ans = 0;
index += 1;
while
(index > 0)
{
ans ^= BITree[index];
index -= index & (-index);
}
return
ans;
}
static
void
updateBIT(
int
[]BITree,
int
n,
int
index,
int
val)
{
index = index + 1;
while
(index <= n)
{
BITree[index] ^= val;
index += index & (-index);
}
}
static
int
[] constructBITree(
int
[]arr,
int
n)
{
int
[]BITree =
new
int
[n + 1];
for
(
int
i = 1; i <= n; i++)
BITree[i] = 0;
for
(
int
i = 0; i < n; i++)
updateBIT(BITree, n, i, arr[i]);
return
BITree;
}
static
int
rangeXor(
int
[]BITree,
int
l,
int
r)
{
return
getXOR(BITree, r) ^
getXOR(BITree, l - 1);
}
public
static
void
Main(String[] args)
{
int
[]A = { 2, 1, 1, 3, 2, 3,
4, 5, 6, 7, 8, 9 };
int
n = A.Length;
int
[,]q = { { 1, 0, 9 },
{ 2, 3, 6 },
{ 2, 5, 5 },
{ 2, 8, 1 },
{ 1, 0, 9 } };
int
[]BITree = constructBITree(A, n);
for
(
int
i = 0; i < q.GetLength(0); i++)
{
int
id = q[i, 0];
if
(id == 1)
{
int
L = q[i, 1];
int
R = q[i, 2];
Console.Write(
"XOR of elements "
+
"in given range is "
+
rangeXor(BITree, L, R) +
"\n"
);
}
else
{
int
idx = q[i, 1];
int
val = q[i, 2];
A[idx] ^= val;
updateBIT(BITree, n, idx, val);
}
}
}
}