您的位置:首页 > 其它

Sona

2016-01-28 10:21 302 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: