-
[1457] Sona
- 时间限制: 5000 ms 内存限制: 65535 K
- 问题描述
- 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 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
题目链接:
莫队算法就是能用n*sqrt(n)的时间来解决无修改区间查询的一种办法……,主要处理就是把询问暂时不按照读入顺序而按照所在块和右端点值为关键字进行排序,然后对这样的一个暂时非正常的询问顺序进行计算回答,然后可以再次按照输入顺序排序或者用数组记录答案后输出。
可以参考这篇博客:
这题写出立方公式就很容易发现a^3和(a+1)^3以及(a-1)^3的区别,然后就可以很方便地进行O(1)转化,网上一些题解代码都是暴力减掉当前的再加回更新的,即减掉a*a*a再加上(a+1)*(a+1)*(a+1),感觉这样写不太好……,反正如果能快速得到[L-1,R]或[L,R+1]这样的区间信息的话莫队算法值得一试。这题用了输入输出外挂搞到2100+MS,一些人直接用立方加减的3300+MS
代码:
#include#include #include #include #include #include #include #include #include #include #include #include #include #include