using
System;
using
System.Collections.Generic;
public
class
Node
{
public
bool
Dec =
false
;
public
bool
Inc =
false
;
public
int
Lm = -1;
public
int
Rm = -1;
}
public
class
Solution
{
private
List<Node> seg;
private
Node Merge(Node n1, Node n2)
{
if
(n1.Lm == -1 || n2.Lm == -1)
return
n1.Lm == -1 ? n2 : n1;
Node ans =
new
Node();
ans.Lm = n1.Lm;
ans.Rm = n2.Rm;
if
(n1.Inc && n2.Inc && n1.Rm <= n2.Lm)
ans.Inc =
true
;
if
(n1.Dec && n2.Dec && n1.Rm >= n2.Lm)
ans.Dec =
true
;
return
ans;
}
private
void
Update(
int
idx,
int
l,
int
r,
int
id,
int
val)
{
if
(l == r)
{
seg[idx] =
new
Node { Inc =
true
, Dec =
true
, Lm = val, Rm = val };
return
;
}
int
mid = (l + r) / 2;
if
(id <= mid)
Update(2 * idx + 1, l, mid, id, val);
else
Update(2 * idx + 2, mid + 1, r, id, val);
seg[idx] = Merge(seg[2 * idx + 1], seg[2 * idx + 2]);
}
private
Node Query(
int
idx,
int
l,
int
r,
int
ql,
int
qr)
{
if
(ql > r || qr < l)
return
new
Node();
if
(ql <= l && qr >= r)
return
seg[idx];
int
mid = (l + r) / 2;
Node lq = Query(2 * idx + 1, l, mid, ql, qr);
Node rq = Query(2 * idx + 2, mid + 1, r, ql, qr);
return
Merge(lq, rq);
}
private
List<
int
> SolveQueries(List<
int
> nums, List<List<
int
>> queries)
{
List<
int
> ans =
new
List<
int
>();
int
n = nums.Count;
seg =
new
List<Node>();
for
(
int
i = 0; i < 4 * n + 1; i++)
{
seg.Add(
new
Node());
}
for
(
int
i = 0; i < n; i++)
{
Update(0, 0, n - 1, i, nums[i]);
}
foreach
(List<
int
> it
in
queries)
{
int
q = it[0];
int
a = it[1] - 1;
int
b = it[2] - 1;
if
(q == 1)
{
Update(0, 0, n - 1, a, ++b);
}
else
{
Node res = Query(0, 0, n - 1, a, b);
if
(res.Inc == res.Dec)
ans.Add(-1);
else
if
(res.Dec)
ans.Add(1);
else
ans.Add(0);
}
}
return
ans;
}
public
static
void
Main(
string
[] args)
{
Solution solution =
new
Solution();
List<
int
> nums =
new
List<
int
> { 1, 5, 7, 4, 3, 5, 9 };
List<List<
int
>> queries =
new
List<List<
int
>>
{
new
List<
int
> { 2, 1, 3 },
new
List<
int
> { 1, 7, 4 },
new
List<
int
> { 2, 6, 7 }
};
List<
int
> result = solution.SolveQueries(nums, queries);
Console.Write(
"Result: "
);
foreach
(
int
res
in
result)
{
Console.Write(res +
" "
);
}
Console.WriteLine();
}
}