Sona

Sona , Maven 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 are109 kinds of notes and a score has 105 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.

Input

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.

Output

For each part, you should output the energy of that part. 


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
#include <set>
#include <iomanip>
using namespace std;
#define eqs 1e-9
#define INF 0x3f3f3f3f
#define MOD 100000000
#define MAXN 100010
#define ll long long

int n,m,nn;
ll ans;
int a[MAXN];
int b[MAXN];
ll num[MAXN];
ll cnt[MAXN];

struct node
{
    int l,r,id;
    bool operator < (const node pp)const
    {
        return l/nn!=pp.l/nn?l/nn<pp.l/nn:r<pp.r;
    }
}p[MAXN];

void updata(int x,int val)
{
    ll k = num[b[x]];
    ans += (3*k*k*val+3*k+val);
    num[b[x]] += val;
}

int main()
{
    while( ~scanf("%d",&n) )
    {
        nn = ceil(sqrt(n*1.0));
        for( int i = 1; i <= n; i++ )
        {
            scanf("%d",&a[i]);
            b[i] = a[i];
        }
        sort(a+1,a+1+n);
        int size = unique(a+1,a+1+n)-(a+1);
        for( int i = 1; i <= n; i++ )
            b[i] = lower_bound(a+1,a+1+size,b[i])-a;
        scanf("%d",&m);
        for( int i = 0; i < m; i++ )
        {
            scanf("%d %d",&p[i].l,&p[i].r);
            p[i].id = i;
        }
        sort(p,p+m);
        memset(num,0,sizeof(num));
        int l = 1,r = 0;
        ans = 0;
        for( int i = 0; i < m; i++ )
        {
            while( p[i].r > r )
            {
                updata(++r,1);
            }
            while( p[i].r < r )
            {
                updata(r--,-1);
            }
            while( p[i].l > l )
            {
                updata(l++,-1);
            }
            while( p[i].l < l )
            {
                updata(--l,1);
            }
            cnt[p[i].id] = ans;
        }
        for( int i = 0; i < m; i++ )
            printf("%I64d\n",cnt[i]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值