由于10^9很大,所以先离散化一下,把给你的这一段数哈希 时间复杂度O(nlogn)

然后就是分块莫队 已知[L,R],由于事先的离散化,可以在O(1)的的时间更新[l+1,r],[l,r+1],[l-1,r],[l,r-1]时间复杂度O(n*sqrt(n));

代码如下,速度并不是很快(我比较喜欢手动的去重,unique一直没怎么用过)

/*96655 's source code for B
Memory: 3744 KB Time: 2968 MS
Language: G++ Result: Accepted
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<cmath>
using namespace std;
typedef long long LL;
const int maxn=;
map<int,int>mp;
int a[maxn],pos[maxn],b[maxn];
LL sum;
struct node
{
int l,r,id;
LL ans;
} res[maxn];
bool cmp1(node a,node b)
{
if(pos[a.l]==pos[b.l])return a.r<b.r;
return a.l<b.l;
}
bool cmp2(node a,node b)
{
return a.id<b.id;
}
void change(int pos,int op)
{ LL temp=b[a[pos]];
sum-=temp*temp*temp;
b[a[pos]]+=op;
temp=b[a[pos]];
sum+=temp*temp*temp;
}
int main()
{
int n,q;
while(~scanf("%d",&n))
{
double cn=n;
int blk=(int)(sqrt(cn));
sum=,mp.clear();
for(int i=; i<=n; ++i)
scanf("%d",&a[i]),pos[i]=(i-)/blk+,b[i]=a[i];
sort(b+,b+n+);
int cnt=;
for(int i=; i<=n; ++i)
if(b[i]!=b[i-])b[cnt++]=b[i];
--cnt;
for(int i=; i<=n; ++i)
a[i]=lower_bound(b+,b++cnt,a[i])-(b+);
memset(b,,sizeof(b));
scanf("%d",&q);
for(int i=; i<=q; ++i)
scanf("%d%d",&res[i].l,&res[i].r),res[i].id=i;
sort(res+,res++q,cmp1);
for(int i=,l=,r=; i<=q; ++i)
{
for(; r<res[i].r; ++r)
change(r+,);
for(; r>res[i].r; --r)
change(r,-);
for(; l<res[i].l; ++l)
change(l,-);
for(; l>res[i].l; --l)
change(l-,);
res[i].ans=sum;
}
sort(res+,res+q+,cmp2);
for(int i=; i<=q; ++i)
printf("%I64d\n",res[i].ans);
}
return ;
}

NBUT1457 Sona 莫队算法的更多相关文章

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

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

  2. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  3. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  4. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  5. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  6. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  7. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  8. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  9. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

随机推荐

  1. 【BZOJ 1491】 [NOI2007]社交网络

    Description Input Output 输出文件包括n 行,每行一个实数,精确到小数点后3 位.第i 行的实数表 示结点i 在社交网络中的重要程度. Sample Input 4 4 1 2 ...

  2. ubuntu下安装GTK过程

    GTK的安装过程比较繁琐,以前也安装过,但没有记录下来,Google一下记录下来备用. 利用此方法成功在ubuntu12.04下安装GTK 2.24.10 1.安装gcc/g++/gdb/make 等 ...

  3. hdu 4710 Balls Rearrangement()

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...

  4. python 开发webService

    最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用. 首先系统上要有python.其次要用python进行webservice开发,还需要一些库: 1 ...

  5. NOIP2014 行记

    不知道OI是啥或者信息学竞赛是啥的可以按`Ctrl+W`. <del>很早开始写的..准备出分之后再发布.</del> 谨以此文纪念我信息学竞赛的第一次正式考试. 背景音乐底部 ...

  6. [转载]iframe跨域

    最近做的一个项目中需要ajax跨域取得数据,如果是在本域中确实没有问题,但是放到二级域和其他域下浏览器直接就弹出提示框:“该页正在访问其控制范围之外的数据,这有些危险,是否继续" 1.什么引 ...

  7. spoj 390

    简单题  记得uva上有个一样的  画个图就好了 #include <cstdio> #include <cmath> const double pi = acos(-1); ...

  8. Visual C++ 6.0静态、动态链接库

    1.什么是静态连接库,什么是动态链接库          静态链接库与动态链接库都是共享代码的方式,如果采用静态链接库,则无论你愿不愿意,lib 中的指令都全部被直接包含在最终生成的 EXE 文件中了 ...

  9. hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...

  10. 李洪强iOS开发之图片拉伸技巧

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...