Sona

Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format:

Appoint description: 

Description

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.

Sample Input

8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5

Sample Output

128
72
2
1
 
题意:
n个数,q次查询,区间内每个数字出现的次数的三次方的和。
思路:
莫队算法,这题卡时间很厉害,map会TLE的。由于n最多只有10^5,又我们的统计是数出现的次数,所以我们不必需要真正的数,所以可以离散化,统计的时候,只要累加这个数对应的位置即可。

/*
* Author: sweat123
* Created Time: 2016/7/15 8:25:26
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int l,r,id;
}q[MAXN];
int a[MAXN],n,m,pos[MAXN],b[MAXN],k,p[MAXN];
ll ret,ans[MAXN];
int mp[MAXN];
bool cmp(node a,node b){
if(pos[a.l] == pos[b.l])return a.r < b.r;
return pos[a.l] < pos[b.l];
}
ll power(int x){
return 1LL * x * x * x;
}
int getkey(int x){
int l,r,m,ans;
l = ,r = k - ;
while(l <= r){
m = (l + r) >> ;
if(b[m] == x)return m;
else if(b[m] > x) r = m - ;
else l = m + ;
}
}
void init(){
sort(b+,b+n+);
k = ;
for(int i = ; i <= n; i++){
if(b[i] != b[i-]){
b[k++] = b[i];
}
}
for(int i = ; i <= n; i++){
int tp = getkey(a[i]);
p[i] = tp;
}
}
void updata(int x,int val){
ret -= power(mp[p[x]]);
mp[p[x]] += val;
ret += power(mp[p[x]]);
}
int main(){
while(~scanf("%d",&n)){
int tp = (int)ceil(sqrt(n * 1.0));
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
b[i] = a[i];
pos[i] = (i - ) / tp;
}
init();
memset(mp,,sizeof(mp));
scanf("%d",&m);
for(int i = ; i <= m; i++){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id = i;
}
sort(q+,q+m+,cmp);
int pl,pr;
pl = ;
pr = ;
ret = ;
for(int i = ; i <= m; i++){
int id = q[i].id;
if(q[i].l == q[i].r){
ans[id] = ;
continue;
} else {
if(pr <= q[i].r){
for(int j = pr + ; j <= q[i].r; j++){
updata(j,);
}
} else{
for(int j = pr; j > q[i].r; j--){
updata(j,-);
}
}
pr = q[i].r;
if(pl < q[i].l){
for(int j = pl; j < q[i].l; j++){
updata(j,-);
}
} else{
for(int j = pl - ; j >= q[i].l; j--){
updata(j,);
}
}
pl = q[i].l;
ans[id] = ret;
}
}
for(int i = ; i <= m; i++){
printf("%I64d\n",ans[i]);
}
}
return ;
}

NBUT 1457 莫队算法 离散化的更多相关文章

  1. NBUT 1457 Sona(莫队算法+离散化)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  2. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  3. 【bzoj3289】Mato的文件管理 离散化+莫队算法+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6805224.html 题目描述 Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份 ...

  4. CodeForces - 220B 离散化+莫队算法

    莫队算法链接:传送门 题意: 有n个数,m个区间.问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3). 题解: 因为a[i]太大,所以要离散化一下,但是不能用map容 ...

  5. HDU-6534-Chika and Friendly Pairs (莫队算法,树状数组,离散化)

    链接: https://vjudge.net/contest/308446#problem/C 题意: Chika gives you an integer sequence a1,a2,-,an a ...

  6. BZOJ3289 Mato的文件管理(莫队算法+树状数组)

    题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...

  7. CSU 1515 Sequence (莫队算法)

    题意:给n个数,m个询问.每个询问是一个区间,求区间内差的绝对值为1的数对数. 题解:先离散化,然后莫队算法.莫队是离线算法,先按按询问左端点排序,在按右端点排序. ps:第一次写莫队,表示挺简单的, ...

  8. 【莫队算法】【权值分块】bzoj3920 Yuuna的礼物

    [算法一] 暴力. 可以通过第0.1号测试点. 预计得分:20分. [算法二] 经典问题:区间众数,数据范围也不是很大,因此我们可以: ①分块,离散化,预处理出: <1>前i块中x出现的次 ...

  9. 【bzoj4542】[Hnoi2016]大数 莫队算法

    题目描述 给出一个数字串,多次询问一段区间有多少个子区间对应的数为P的倍数.其中P为质数. 输入 第一行一个整数:P.第二行一个串:S.第三行一个整数:M.接下来M行,每行两个整数 fr,to,表示对 ...

随机推荐

  1. Wireshark

    0. install Wireshark on Ubuntu 14 sudo apt-get install -y wireshark sudo addgroup -quiet -system wir ...

  2. Mac 热键大全

    屏幕捕捉快捷键动作............................保存到............-快捷键 全屏捕捉........................桌面(.PDF文件)..... ...

  3. QT数据库连接的几个重要函数的使用及注意事项(原创)

    注:在这里数据库对象等同于数据库连接对象,也就是QSqlDatabase类的对象 QSqlDatabase QSqlDatabase::addDatabase((const QString & ...

  4. 【原】Go语言及Web框架Beego环境无脑搭建

    本文涉及软件均以截至到2013年10月12日的最新版本为准 1. 相关软件准备: 1) go1.2rc1.windows-386.msi,对应32位windows系统安装使用 下载地址: https: ...

  5. Android中的自定义控件(二)

    案例四: 自定义开关       功能介绍:本案例实现的功能是创建一个自定义的开关,可以自行决定开关的背景.当滑动开关时,开关的滑块可跟随手指移动.当手指松开后,滑块根据开关的状态,滑到最右边或者滑到 ...

  6. undefined reference to `__android_log_print'

    使用android studio 编写NDK代码时出现错误:undefined reference to `__android_log_print' 解决办法: eclipse       andro ...

  7. [Erlang 0120] Know a little Core Erlang

      Erlang开发者或多或少都用过或者听说过Core erlang,它是什么样的呢?新建一个测试模块a.erl,如下操作会生成core erlang代码而非a.beam:   Eshell V6.0 ...

  8. Long类型的数据转换时间格式方法

    function getDate(date) { //得到日期对象 var d=new Date(date); //得到年月日 var year =d.getFullYear(); ); var da ...

  9. 在WINDOWS下初步试用OMNET++ 4

    闲扯: 最近实习公司要做ZIGBEE,我是对这个兴趣不大,但工作还是要做的,目前帮着找找合适的仿真软件,什么NS-2啊,OPNET啊. 正文: 这个软件软件直接去官网下载就好了,免费开源. 安装也比较 ...

  10. Surface在C++层的创建源码解析

    Surface在C++层的创建源码解析 源码为:android4.4.4 1.创建SurfaceComposerClient绘图客户端 // create a client to surfacefli ...