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.
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; }
相关文章推荐
- android中的进程和线程
- Andorid 10 个最常见的 React Native Android 问题
- Java随机密码生成并和邮箱、手机号匹配
- 2016太原网络营销师郭文军分享所带学员在达内的成长?
- Yarn资源分配性能调优
- ZOJ1024-Calendar Game
- Leetcode[110]-Balanced Binary Tree
- C# 类的序列化和反序列化
- C语言中的指针学习(小黑板)
- 两个数组找相同
- iOS下载大文件原理解析一
- win7用控制台本地组策略自定义程序(任意文件)开机自动启动
- 智能硬件公司估值集体缩水 资本疯狂涌入VR
- SQL 查询 日期
- 复杂对象的组装与创建——建造者模式
- LeetCode Text Justification
- 基于SMB协议的共享文件读写 博客分类: Java
- 加锁测试
- [转]Hibernate时间总结
- 吉他如何扫弦(下)