Codeforces#86D Powerful array(分块暴力)

本文介绍了一个算法问题,即计算给定数组中特定子数组的能量值,能量定义为子数组中各元素出现频率的平方乘以其值的总和。文章提供了一种通过分块暴力的方法来高效解决该问题,并附带了完整的C++实现代码。

Description

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).

Sample Input

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

题意:就问问你统计[L,R]中x1,x2,,xi出现次数a1,a2,,ai的ai*ai*xi和。

题解:分块暴力。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef __int64 LL;
const int INF = 0x3f3f3f3f;
const int maxn=200000+100;
LL up[maxn];
int col[maxn],sum[maxn*10],n,m,k;
LL ans;
struct node{
    int l,r;
    int id;
}q[maxn];
int cmp(node l1,node l2)
{
    if(l1.l/k==l2.l/k)
        return l1.r<l2.r;
    return l1.l<l2.l;
}
void update(int x,int val)
{
    ans-=(LL)col[x]*sum[col[x]]*sum[col[x]];
    sum[col[x]]+=val;
    ans+=(LL)col[x]*sum[col[x]]*sum[col[x]];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        CLEAR(sum,0);
        k=sqrt(n*1.0+0.5);
        REPF(i,1,n)
            scanf("%d",&col[i]);
        REP(i,m)
        {
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q,q+m,cmp);
        int L=0,R=0;
        ans=0;
        for(int i=0;i<m;i++)
        {
            int id=q[i].id;
            int l=q[i].l;
            int r=q[i].r;
            while(L < l)
            {
                update(L,-1);
                L++;
            }
            while(R > r)
            {
                update(R,-1);
                R--;
            }
            while(L > l)
            {
                L--;
                update(L,1);
            }
            while(R < r)
            {
                R++;
                update(R,1);
            }
            up[id]=ans;
        }
        for(int i=0;i<m;i++)
            printf("%I64d\n",up[i]);
    }
    return 0;
}


转载于:https://www.cnblogs.com/clnchanpin/p/6732957.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值