码迷,mamicode.com
首页 > 其他好文 > 详细

Powerful array CodeForces - 86D

时间:2020-02-28 22:27:07      阅读:42      评论:0      收藏:0      [点我收藏+]

标签:str   pow   set   sort   amp   clu   scan   printf   i++   

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10; 
int n,m,block;
long long a[maxn],cnt[maxn];
long long ans,s[maxn];
struct node
{
    int l,r,id;
}q[maxn];
bool cmp(node x,node y)
{
    if(x.l/block!=y.l/block)
        return x.l<y.l;
    return x.r<y.r;
}
void add(long long x)
{
    ans-=cnt[x]*cnt[x]*x;
    cnt[x]++;
    ans+=cnt[x]*cnt[x]*x;
}
void del(long long x)
{
    ans-=cnt[x]*cnt[x]*x;
    cnt[x]--;
    ans+=cnt[x]*cnt[x]*x;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        block=sqrt(n);
        memset(cnt,0,sizeof cnt);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q,q+m,cmp);
        int L=1,R=0;
        ans=0;
        for(int i=0;i<m;i++)
        {
            while(L<q[i].l) 
                del(a[L++]);
            while(L>q[i].l) 
                add(a[--L]);
            while(R<q[i].r) 
                add(a[++R]);
            while(R>q[i].r) 
                del(a[R--]);
            s[q[i].id]=ans;
        }
        for(int i=0;i<m;i++)
            printf("%lld\n",s[i]);
    }
    return 0;
}

Powerful array CodeForces - 86D

标签:str   pow   set   sort   amp   clu   scan   printf   i++   

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12380455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!