import
java.util.*;
class
FenwickTree {
int
N;
int
[] BIT;
public
FenwickTree(
int
N)
{
this
.N = N;
BIT =
new
int
[N];
}
public
void
update(
int
index,
int
increment)
{
while
(index < N) {
BIT[index] += increment;
index += index & -index;
}
}
public
int
getSum(
int
index)
{
int
sum =
0
;
while
(index >
0
) {
sum += BIT[index];
index -= index & -index;
}
return
sum;
}
}
public
class
Main {
public
static
void
setElement(
int
[] arr,
int
n,
int
index,
int
x,
FenwickTree fenTree)
{
int
removedElement = arr[index];
fenTree.update(removedElement, -
1
);
arr[index] = x;
fenTree.update(x,
1
);
}
public
static
int
getCount(
int
[] arr,
int
n,
int
l,
int
r, FenwickTree fenTree)
{
int
count
= fenTree.getSum(r) - fenTree.getSum(l -
1
);
return
count;
}
public
static
void
SolveQuery(
int
[] arr,
int
n,
ArrayList<
int
[]> Q)
{
int
N =
100001
;
FenwickTree fenTree =
new
FenwickTree(N);
for
(
int
i =
0
; i < n; i++) {
fenTree.update(arr[i],
1
);
}
for
(
int
i =
0
; i < Q.size(); i++) {
if
(Q.get(i)[
0
] ==
1
) {
int
x = getCount(arr, n, Q.get(i)[
1
],
Q.get(i)[
2
], fenTree);
System.out.print(x +
" "
);
}
else
{
setElement(arr, n, Q.get(i)[
1
], Q.get(i)[
2
],
fenTree);
}
}
}
public
static
void
main(String[] args)
{
int
[] arr = {
1
,
2
,
2
,
3
,
4
,
4
,
5
,
6
};
int
n = arr.length;
ArrayList<
int
[]> Q =
new
ArrayList<>();
Q.add(
new
int
[] {
1
,
3
,
5
});
Q.add(
new
int
[] {
1
,
2
,
4
});
Q.add(
new
int
[] {
1
,
1
,
2
});
Q.add(
new
int
[] {
2
,
1
,
7
});
Q.add(
new
int
[] {
1
,
1
,
2
});
SolveQuery(arr, n, Q);
}
}