Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2709 小B的询问

P2709 小B的询问

简单的普通莫队问题。

查看代码
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
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5e4 + 10, M = 5e4 + 10;
LL res, ans[M];
int n, m, len, w[N], cnt[N];
struct Query
{
int l, r, i;
bool operator<(const Query &_) const
{
if (l / len != _.l / len)
return l < _.l;
return (l / len) & 1 ? r < _.r : r > _.r;
}
} q[M];
void add(int x)
{
res -= cnt[x] * cnt[x];
cnt[x]++;
res += cnt[x] * cnt[x];
}
void del(int x)
{
res -= cnt[x] * cnt[x];
cnt[x]--;
res += cnt[x] * cnt[x];
}
int main()
{
int a;
scanf("%d%d%d", &n, &m, &a);
len = sqrt(n);
for (int i = 1; i <= n; i++)
scanf("%d", &w[i]);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &q[i].l, &q[i].r);
q[i].i = i;
}
sort(q, q + m);
for (int i = 1, j = 0, k = 0; k < m; k++)
{
while (i > q[k].l)
add(w[--i]);
while (i < q[k].l)
del(w[i++]);
while (j < q[k].r)
add(w[++j]);
while (j > q[k].r)
del(w[j--]);
ans[q[k].i] = res;
}
for (int i = 0; i < m; i++)
printf("%lld\n", ans[i]);
return 0;
}