Sona - NBUT 1457 莫队算法

来源:互联网 发布:简述单片机的开发过程 编辑:程序博客网 时间:2024/05/23 19:16

  • [1457] Sona

  • 时间限制: 5000 ms 内存限制: 65535 K
  • 问题描述
  • SonaMaven of the Strings. Of cause, she can play the zither.

    Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.

    There're an ancient score(乐谱). But because it's too long, Sona can't play it in a short moment. So Sona decide to just play a part of it and revise it.

    A score is composed of notes. There are 109 kinds of notes and a score has105 notes at most.

    To diversify Sona's own score, she have to select several parts of it. The energy of each part is calculated like that:

    Count the number of times that each notes appear. Sum each of the number of times' cube together. And the sum is the energy.

    You should help Sona to calculate out the energy of each part.

  • 输入
  • This problem contains several cases. And this problem provides 2 seconds to run.
    The first line of each case is an integer N (1 ≤ N ≤ 10^5), indicates the number of notes.
    Then N numbers followed. Each number is a kind of note. (1 ≤ NOTE ≤ 10^9)
    Next line is an integer Q (1 ≤ Q ≤ 10^5), indicates the number of parts.
    Next Q parts followed. Each part contains 2 integers Li and Ri, indicates the left side of the part and the right side of the part.
  • 输出
  • For each part, you should output the energy of that part.
  • 样例输入
  • 81 1 3 1 3 1 3 341 83 85 65 5
  • 样例输出
  • 1287221
题意:求出给定区间内各个数字的出现次数的立方和。

思路:运用莫队算法分块的思想解决。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<cmath>using namespace std;typedef long long ll;struct node{    int l,r,id;    ll ans;}arr[100010];int n,m,pos[100010],c[100010];ll num[100010];ll ans;map<int,int> match;bool cmp(node a,node b){    return pos[a.l]<pos[b.l] || (pos[a.l]==pos[b.l] && a.r<b.r);}bool cmp_id(node a,node b){    return a.id<b.id;}void update(int p,int add){    ans-=num[c[p]]*num[c[p]]*num[c[p]];    num[c[p]]+=add;    ans+=num[c[p]]*num[c[p]]*num[c[p]];}void solve(){    int i,j,k,l,r;    ans=0;    l=1;r=0;    memset(num,0,sizeof(num));    for(i=1;i<=m;i++)    {        //printf("l=%d r=%d\n",arr[i].l,arr[i].r);        for(;r<arr[i].r;r++)           update(r+1,1);        for(;r>arr[i].r;r--)           update(r,-1);        for(;l<arr[i].l;l++)           update(l,-1);        for(;l>arr[i].l;l--)           update(l-1,1);        arr[i].ans=ans;    }}int main(){    int i,j,k,tot;    while(~scanf("%d",&n))    {        for(i=1;i<=n;i++)           scanf("%d",&c[i]);        k=(int)sqrt((double)n);        for(i=1;i<=n;i++)           pos[i]=(i-1)/k+1;        match.clear();        tot=0;        for(i=1;i<=n;i++)        {            if(match[c[i]]>0)              c[i]=match[c[i]];            else            {                match[c[i]]=++tot;                c[i]=tot;            }        }        scanf("%d",&m);        for(i=1;i<=m;i++)        {            scanf("%d%d",&arr[i].l,&arr[i].r);            arr[i].id=i;        }        sort(arr+1,arr+1+m,cmp);        solve();        sort(arr+1,arr+1+m,cmp_id);        for(i=1;i<=m;i++)           printf("%I64d\n",arr[i].ans);    }}



0 0
原创粉丝点击