CF86D Powerful array

怎么2700的题这么简单啊QAQ

长得非常像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
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
#define int long long
const int MaxN = 1000010;
struct query
{
int l, r, id, pos;
};
query q[MaxN];
int n, t, size;
int ans[MaxN], sum;
int a[MaxN], cnt[MaxN];
inline int cmp(query a, query b)
{
if (a.pos != b.pos)
return a.pos < b.pos;
return a.r < b.r;
}
inline int read()
{
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0')
ch = getchar();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
inline void add(int x)
{
sum += a[x] * (2 * cnt[a[x]] + 1);
cnt[a[x]]++;
}
inline void del(int x)
{
sum -= a[x] * (2 * cnt[a[x]] - 1);
cnt[a[x]]--;
}
inline void solve()
{
int l = 1, r = 0;
for (int i = 1; i <= t; i++)
{
while (l > q[i].l)
--l, add(l);
while (r < q[i].r)
++r, add(r);
while (l < q[i].l)
del(l), ++l;
while (r > q[i].r)
del(r), --r;
ans[q[i].id] = sum;
}
}
signed main()
{
n = read(), t = read();
size = pow(n, 0.55);
for (int i = 1; i <= n; i++)
a[i] = read();
for (int i = 1; i <= t; i++)
{
q[i].l = read(), q[i].r = read();
q[i].id = i, q[i].pos = (q[i].l - 1) / size + 1;
}
std::sort(q + 1, q + t + 1, cmp);
solve();
for (int i = 1; i <= t; i++)
printf("%lld\n", ans[i]);
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×