-
Notifications
You must be signed in to change notification settings - Fork 0
/
mos2.cpp
81 lines (76 loc) · 1.28 KB
/
mos2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007ll
const int MAX = 2e5+5;
const int LIMIT = 10e6+2;
const int block = 500;
int l[MAX], r[MAX], arr[MAX],cnt[LIMIT];
ll current = 0;
ll ans[MAX];
vector<int> v[505][505];
int n, q;
inline void add(int i) {
ll x, y;
y = arr[i];
x = cnt[y];
current -= (x*x*y);
x++;
cnt[y]++;
current += (x*x*y);
}
inline void remove(int i) {
ll x, y;
y = arr[i];
x = cnt[y];
current -= (x*x*y);
x--;
cnt[y]--;
current += (x*x*y);
}
int main (void) {
scanf ("%d%d", &n,&q);
int i;
for (i = 0; i < n; i++) {
scanf ("%d", &arr[i]);
}
for (i = 0; i < q; i++) {
scanf ("%d%d", &l[i], &r[i]);
v[l[i]/block][r[i]/block].push_back(i);
}
int lll = 0, rr = -1;
int sz = n/block+1;
int a, b;
int j, k;
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
for (k = 0; k < v[i][j].size(); k++) {
int inx = v[i][j][k];
a = l[inx]-1;
b = r[inx]-1;
// cout<<a<<" "<<b<<endl;
while (rr < b) {
rr++;
add(rr);
}
while (rr > b) {
remove(rr);
rr--;
}
while (lll < a) {
remove(lll);
lll++;
}
while (lll > a) {
lll--;
add(lll);
}
ans[inx] = current;
}
}
}
for (i = 0; i < q; i++) {
printf ("%lld\n", ans[i]);
}
return 0;
}