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. maven之一:maven安装和eclipse集成

    maven作为一个项目构建工具,在开发的过程中很受欢迎,可以帮助管理项目中的bao依赖问题,另外它的很多功能都极大的减少了开发的难度,下面来介绍maven的安装及与eclipse的集成. maven的 ...

  2. Android的历史与花边

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 历史 现在的Android如日中天.每天150万部的Android设备被激活,全球 ...

  3. Console.In.ReadToEnd() 控制台 输入完毕

    输入完数据后 按回车(另起一行) ctrl+z enter .......百度了半天 没百度到..最后还是google 强大..解决了问题 ..

  4. python语言中的编码问题

    在编程的过程当中,常常会遇到莫名其妙的乱码问题.很多人选择出了问题直接在网上找答案,把别人的例子照搬过来,这是快速解决问题的一个好办法.然而,作为一个严谨求实的开发者,如果不从源头上彻底理解乱码产生的 ...

  5. 移动端web自适应解决方案: adaptive.js

    代码有更新,最好直接查看github github:https://github.com/finance-sh/adaptive adaptivejs利用rem解决移动端页面开发的自适应问题 页面模板 ...

  6. css小技巧(1)

    1.-webkit-overflow-scrolling: touch; 解决ios滑动时无缓冲问题 2.::-webkit-scrollbar 设置ios滑动时是否显示滚动条 3.::selecti ...

  7. Node.js 教程 03 - 创建HTTP服务器

    前言: 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请 ...

  8. DependencyResolver.Current

    描述: 获取依赖关系解析程序的实现. 命名空间: System.Web.Mvc 程序集: System.Web.Mvc(在 System.Web.Mvc.dll 中) 用例: IRecLockServ ...

  9. Linux下查看系统版本号信息的方法

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version 2.uname -a 二.查看Linux系统版本的命令(3种方法): 1.lsb_release -a,即可列出所 ...

  10. XML简介与CDATA解释

    简介XML 是一种受到广泛支持的 Internet 标准,用于以一种特殊的方式编码结构化数据.实际上,以 XML 编码的数据可以通过任何编程语言解码,人们甚至可以使用标准的文本编辑器来阅读或编写 XM ...