using
System;
using
System.Linq;
using
System.Collections.Generic;
public
class
Query
{
public
int
L {
get
;
set
; }
public
int
R {
get
;
set
; }
public
int
Index {
get
;
set
; }
public
int
Even {
get
;
set
; }
public
Query(
int
l,
int
r,
int
index,
int
even)
{
L = l;
R = r;
Index = index;
Even = even;
}
}
public
class
GFG
{
static
int
MAX = 100000;
static
int
[] arr =
new
int
[] { 5, 2, 3, 1, 4, 8, 10, 12 };
static
int
n = arr.Length;
static
int
block = (
int
)Math.Sqrt(n);
static
int
DigitSum(
int
num)
{
int
sum = 0;
while
(num > 0)
{
sum += (num % 10);
num /= 10;
}
return
sum;
}
static
int
Add(
int
currL,
int
[] a,
int
count_even)
{
if
(DigitSum(a[currL]) % 2 == 0)
{
count_even++;
}
return
count_even;
}
static
int
Remove(
int
currR,
int
[] a,
int
count_even)
{
if
(DigitSum(a[currR]) % 2 == 0)
{
count_even--;
}
return
count_even;
}
static
int
Compare(Query x, Query y)
{
if
(x.L / block != y.L / block)
{
return
x.L / block - y.L / block;
}
else
{
return
x.R - y.R;
}
}
static
void
QueryResults(
int
[] a,
int
n, Query[] q,
int
m)
{
int
count_even = 0;
q = q.OrderBy(x => x, Comparer<Query>.Create(Compare)).ToArray();
int
currL = 0, currR = 0;
for
(
int
i = 0; i < m; i++)
{
int
L = q[i].L, R = q[i].R;
while
(currR <= R)
{
count_even = Add(currR, a, count_even);
currR++;
}
while
(currL > L)
{
count_even = Add(currL - 1, a, count_even);
currL--;
}
while
(currR > R + 1)
{
count_even = Remove(currR - 1, a, count_even);
currR--;
}
while
(currL < L)
{
count_even = Remove(currL, a, count_even);
currL++;
}
q[i].Even = count_even;
}
}
static
void
PrintResults(Query[] q,
int
m)
{
q = q.OrderBy(x => x.Index).ToArray();
for
(
int
i = 0; i < m; i++)
{
Console.WriteLine(q[i].Even);
}
}
public
static
void
Main()
{
Query[] q =
new
Query[] {
new
Query(1, 3, 0, 0),
new
Query(0, 4, 1, 0),
new
Query(4, 7, 2, 0)
};
int
m = q.Length;
QueryResults(arr, n, q, m);
PrintResults(q, m);
}
}