import
java.util.*;
public
class
GFG {
static
class
Pair{
int
key;
int
value;
public
Pair(
int
k,
int
v){
key = k;
value = v;
}
}
static
int
N = (
int
)1e5 +
5
;
static
int
freq[] =
new
int
[N];
static
int
sq =
0
;
static
int
ans =
0
;
static
void
add(
int
x,
int
arr[])
{
ans -= arr[x] * freq[arr[x]];
freq[arr[x]]++;
ans += arr[x] * freq[arr[x]];
}
static
void
remove(
int
x,
int
arr[])
{
ans -= arr[x] * freq[arr[x]];
freq[arr[x]]--;
ans += arr[x] * freq[arr[x]];
}
static
void
answerQueries(
int
arr[],
int
n, ArrayList<Pair> queries)
{
sq = (
int
)Math.sqrt(n) +
1
;
int
[] answer =
new
int
[queries.size()];
Map<Pair, Integer> idx =
new
HashMap<>();
for
(
int
i =
0
; i < queries.size(); i++)
idx.put(queries.get(i),i);
Collections.sort(queries,(a,b)->{
if
(a.key/sq != b.key/sq)
return
a.key-b.key;
return
a.value - b.value;
});
ans =
0
;
int
x =
0
, y = -
1
;
for
(Pair i : queries) {
int
l = i.key -
1
, r = i.value -
1
;
int
id = idx.get(i);
while
(x > l) {
x--;
add(x, arr);
}
while
(y < r) {
y++;
add(y, arr);
}
while
(x < l) {
remove(x, arr);
x++;
}
while
(y > r) {
remove(y, arr);
y--;
}
answer[id] = ans;
}
for
(
int
i =
0
; i < queries.size(); i++)
System.out.println(answer[i]);
}
public
static
void
main (String[] args) {
int
arr[] =
new
int
[]{
1
,
1
,
2
,
2
,
1
,
3
,
1
,
1
};
int
n = arr.length;
ArrayList<Pair> queries =
new
ArrayList<>();
queries.add(
new
Pair(
2
,
7
));
queries.add(
new
Pair(
1
,
6
));
answerQueries(arr, n, queries);
}
}