using
System;
class
Node
{
public
int
l0, r0;
public
int
min0;
}
class
GFG
{
static
Node[] seg =
new
Node[100001];
static
Node task(Node l, Node r)
{
Node x =
new
Node();
x.l0 = (l.l0 != -1) ? l.l0 : r.l0;
x.r0 = (r.r0 != -1) ? r.r0 : l.r0;
x.min0 = Math.Min(l.min0, r.min0);
if
(l.r0 != -1 && r.l0 != -1)
x.min0 = Math.Min(x.min0, r.l0 - l.r0);
return
x;
}
static
void
build(
int
qs,
int
qe,
int
ind,
int
[] arr)
{
if
(qs == qe)
{
if
(arr[qs] == 0)
{
seg[ind].l0 = seg[ind].r0 = qs;
seg[ind].min0 =
int
.MaxValue;
}
else
{
seg[ind].l0 = seg[ind].r0 = -1;
seg[ind].min0 =
int
.MaxValue;
}
return
;
}
int
mid = (qs + qe) >> 1;
build(qs, mid, ind << 1, arr);
build(mid + 1, qe, ind << 1 | 1, arr);
seg[ind] = task(seg[ind << 1], seg[ind << 1 | 1]);
}
static
Node query(
int
qs,
int
qe,
int
ns,
int
ne,
int
ind)
{
Node x =
new
Node();
x.l0 = x.r0 = -1;
x.min0 =
int
.MaxValue;
if
(qs <= ns && qe >= ne)
return
seg[ind];
if
(ne < qs || ns > qe || ns > ne)
return
x;
int
mid = (ns + ne) >> 1;
Node l = query(qs, qe, ns, mid, ind << 1);
Node r = query(qs, qe, mid + 1, ne, ind << 1 | 1);
x = task(l, r);
return
x;
}
public
static
void
Main(
string
[] args)
{
for
(
int
i = 0; i < 100001; i++)
{
seg[i] =
new
Node();
}
int
[] arr =
new
int
[] { 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0 };
int
n = arr.Length;
build(0, n - 1, 1, arr);
int
[][] Q =
new
int
[][] {
new
int
[] { 3, 9 },
new
int
[]{ 10, 13 } };
for
(
int
i = 0; i < 2; i++)
{
Node ans = query(Q[i][0], Q[i][1], 0, n - 1, 1);
Console.WriteLine(ans.min0);
}
}
}