using
System;
class
GFG {
static
int
getMid(
int
s,
int
e)
{
return
s + (e - s) / 2;
}
static
int
MaxUtil(
int
[] st,
int
ss,
int
se,
int
l,
int
r,
int
node)
{
if
(l <= ss && r >= se)
return
st[node];
if
(se < l || ss > r)
return
-1;
int
mid = getMid(ss, se);
return
Math.Max(
MaxUtil(st, ss, mid, l, r, 2 * node + 1),
MaxUtil(st, mid + 1, se, l, r, 2 * node + 2));
}
static
int
getMax(
int
[] st,
int
n,
int
l,
int
r)
{
if
(l < 0 || r > n - 1 || l > r)
{
Console.Write(
"Invalid Input"
);
return
-1;
}
return
MaxUtil(st, 0, n - 1, l, r, 0);
}
static
int
constructSTUtil(
int
[] arr,
int
ss,
int
se,
int
[] st,
int
si)
{
if
(ss == se)
{
st[si] = arr[ss];
return
arr[ss];
}
int
mid = getMid(ss, se);
st[si] = Math.Max(
constructSTUtil(arr, ss, mid, st,
si * 2 + 1),
constructSTUtil(arr, mid + 1, se, st,
si * 2 + 2));
return
st[si];
}
static
int
[] constructST(
int
[] arr,
int
n)
{
int
x = (
int
)(Math.Ceiling(Math.Log(n)));
int
max_size = 2 * (
int
)Math.Pow(2, x) - 1;
int
[] st =
new
int
[max_size];
constructSTUtil(arr, 0, n - 1, st, 0);
return
st;
}
public
static
void
Main(String[] args)
{
int
[] arr = {5, 2, 3, 0};
int
n = arr.Length;
int
[] st = constructST(arr, n);
int
[, ] Q = {{1, 3}, {0, 2}};
for
(
int
i = 0; i < Q.GetLength(0); i++) {
int
max = getMax(st, n, Q[i, 0], Q[i, 1]);
int
ok = 0;
for
(
int
j = 30; j >= 0; j--) {
if
((max & (1 << j)) != 0)
ok = 1;
if
(ok <= 0)
continue
;
max |= (1 << j);
}
Console.Write(max +
" "
);
}
}
}