由于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. 一种高斯模糊渐变动画的实现-b

    关于高斯模糊的方式有很多种,但是如果需要模糊渐变,那么对这种高斯模糊算法的性能要求是比较高的,今天这里重点不讨论算法,只是提供一个动画实现的思路.动画效果如下: 高斯模糊渐变动画 //高斯模糊 -(U ...

  2. 【线段树/数学/扩展欧几里得】 Bzoj 3913:奇数国

    Description 在一片美丽的大陆上有100000个国家,记为1到100000.这里经济发达,有数不尽的账房,并且每个国家有一个银行.某大公司的领袖在这100000个银行开户时都存了3大洋,他惜 ...

  3. leetcode5 Implement strstr() 实现strstr函数功能

    Implement strstr() 实现strstr函数功能 whowhoha@outlook.com Question: Implement strstr(). Returns the index ...

  4. uva 125

    floyd 算法   如果存在无数条路  则存在a->a的路  a->b的路径数等于 a->i 和 i->b(0=<i<=_max) 路径数的乘积和 #includ ...

  5. ural 1250

    有点坑的dfs  看懂题应该就会做了 神圣海必然围成一个圈  dfs将神圣还外围的全部去掉   简单题 #include <cstdio> #include <cstring> ...

  6. POJ 1269 Intersecting Lines(几何)

    题目链接 题意 : 给你两条线段的起点和终点,一共四个点,让你求交点坐标,如果这四个点是共线的,输出“LINE”,如果是平行的就输出“NONE”. 思路 : 照着ZN留下的模板果然好用,直接套上模板了 ...

  7. What is the innovator’s solution——什么才是创新的解决方案2

    前略:http://www.cnblogs.com/Kassadin/p/4233497.html 案例1 从书上的一个案例开始: 让我们来看看AT&T公司(美国电话电报公司)的案例吧.198 ...

  8. linux bash_profile和.bashrc区别

    经常在一些技术类的文章中提到修改bash_profile和.bashrc这两个文件,也算是使用频率比较高的两个文件吧,但实现同样一个功能,有的教程里说修改bash_profile这个文件,有的教程里却 ...

  9. MysqlHelper类

    连接方式:server=localhost;port=3306;userid=root;password=123456789;database=mysql;persist security info= ...

  10. maven新建Spring MVC + MyBatis + Oracle的Web项目中pom.xml文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...