import
java.util.*;
public
class
SegmentTree {
static
final
int
MAX =
100005
;
static
HashMap<Integer, Integer> map =
new
HashMap<>();
static
Node[] tree =
new
Node[
5
* MAX];
public
static
void
main(String[] args)
{
int
[] a = {
1
,
1
,
2
,
4
,
3
,
3
};
int
n = a.length;
buildtree(
0
, n -
1
,
0
, a);
int
l =
1
, r =
4
;
System.out.println(answerQuery(a, n, l, r));
l =
2
;
r =
6
;
System.out.println(answerQuery(a, n, l, r));
}
static
void
buildtree(
int
low,
int
high,
int
pos,
int
[] a)
{
if
(low == high) {
tree[pos] =
new
Node(a[low],
1
);
return
;
}
int
mid = (low + high) /
2
;
buildtree(low, mid,
2
* pos +
1
, a);
buildtree(mid +
1
, high,
2
* pos +
2
, a);
if
(tree[
2
* pos +
1
].min < tree[
2
* pos +
2
].min) {
tree[pos] =
new
Node(tree[
2
* pos +
1
].min,
tree[
2
* pos +
1
].cnt);
}
else
if
(tree[
2
* pos +
1
].min
> tree[
2
* pos +
2
].min) {
tree[pos] =
new
Node(tree[
2
* pos +
2
].min,
tree[
2
* pos +
2
].cnt);
}
else
{
tree[pos]
=
new
Node(tree[
2
* pos +
1
].min,
tree[
2
* pos +
1
].cnt
+ tree[
2
* pos +
2
].cnt);
}
}
static
Node query(
int
s,
int
e,
int
low,
int
high,
int
pos)
{
Node dummy =
new
Node(Integer.MAX_VALUE,
Integer.MAX_VALUE);
if
(e < low || s > high) {
return
dummy;
}
if
(s >= low && e <= high) {
return
tree[pos];
}
int
mid = (s + e) /
2
;
Node ans1 = query(s, mid, low, high,
2
* pos +
1
);
Node ans2
= query(mid +
1
, e, low, high,
2
* pos +
2
);
Node ans =
new
Node(
0
,
0
);
ans.min = Math.min(ans1.min, ans2.min);
if
(ans1.min == ans2.min) {
ans.cnt = ans2.cnt + ans1.cnt;
}
else
if
(ans1.min < ans2.min) {
ans.cnt = ans1.cnt;
}
else
{
ans.cnt = ans2.cnt;
}
return
ans;
}
static
int
answerQuery(
int
[] a,
int
n,
int
l,
int
r)
{
for
(
int
i =
0
; i < n; i++) {
map.put(a[i], i);
}
Node ans = query(
0
, n -
1
, l -
1
, r -
1
,
0
);
return
ans.cnt;
}
static
class
Node {
int
min;
int
cnt;
Node(
int
min,
int
cnt)
{
this
.min = min;
this
.cnt = cnt;
}
}
}