86D(莫队算法)

这篇博客介绍了如何运用莫队算法解决数组子段的权力计算问题。给出的数组长度n和查询次数t不超过200000,元素范围在1到10^6之间。莫队算法在处理这类问题时表现出优越性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Powerful array
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.

You should calculate the power of t given subarrays.

Input

First line contains two integers n and t (1 ≤ n, t ≤ 200000) — the array length and the number of queries correspondingly.

Second line contains n positive integers ai (1 ≤ ai ≤ 106) — the elements of the array.

Next t lines contain two positive integers lr (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.

Output

Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).

Examples
input
3 2
1 2 1
1 2
1 3
output
3
6
input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
output
20
20
20
Note

Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

Then K1 = 3K2 = 2K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.


解题思路:不多说,直接上莫队,对于这种问题,我觉得莫队完爆一切数据结构。


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 200000 + 10;
int n, q;
LL a[maxn];
LL ans[maxn];
int pos[maxn];
int block;
int num[1000005];
int L, R;
LL s0;
struct query{
    int l, r;
    int loc;
    int id;
    bool operator <(const query &res) const{
        if(loc == res.loc) return r < res.r;
        else return loc < res.loc;
    }
}Query[maxn<<1];
void init()
{
    block = (int)sqrt(n);
    memset(num, 0, sizeof(num));
    memset(pos, 0, sizeof(pos));
    s0 = 0;
    L = R = 1;
}
void add(LL x)
{
    s0 = s0 + 2 * (LL)num[x] * x + x;
    num[x]++;
}
void sub(LL x)
{
    s0 = s0 - 2 * (LL)num[x] * x + x;
    num[x]--;
}
int main()
{
    scanf("%d%d", &n, &q);
    init();
    for(int i = 1; i <= n; i++)
    {
        scanf("%I64d", &a[i]);
        pos[i] = i / block;
    }
    for(int i = 1; i <= q; i++)
    {
        scanf("%d%d", &Query[i].l, &Query[i].r);
        Query[i].loc = pos[Query[i].l];
        Query[i].id = i;
    }
    sort(Query + 1, Query + q + 1);
    add(a[1]);
    for(int i = 1; i <= q; i++)
    {
        int l = Query[i].l;
        int r = Query[i].r;
        while(L < l)
        {
            sub(a[L]);
            L++;
        }
        while(L > l)
        {
            L--;
            add(a[L]);
        }
        while(R < r)
        {
            R++;
            add(a[R]);
        }
        while(R > r)
        {
            sub(a[R]);
            R--;
        }
        ans[Query[i].id] = s0;
    }
    for(int i = 1; i <= q; i++)
    {
        printf("%I64d\n", ans[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值