由于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. 【POJ2104】kth num

    You are working for Macrohard company in data structures department. After failing your previous tas ...

  2. Experience all that SharePoint 15 has to offer. Start now or Remind me later.

    $spSite = Get-SpSite($waUrl); $spSite.AllowSelfServiceUpgrade = $false

  3. shell 后台执行命令

    shell 后台执行命令方法: 1. nohup cmd &          后台会生成 nohup.out 文件 2.cmd >/路径/xx.log &   后台生成 xx. ...

  4. lua通过bunlde读注意事项

    把lua脚本做成bundle,加载字符串,变成lua对象: lua loadstring("name")() 注意:loadstring的问题: 无法访问全局local变量,需要改 ...

  5. dictionary 和 hashtable 区别

    区别:1,Dictionary支持泛型,而Hashtable不支持. 2,Dictionary没有装填因子(Load Facto)概念,当容量不够时才扩容(扩容跟Hashtable一样,也是两倍于当前 ...

  6. POJ1905Expanding Rods(二分)

    http://poj.org/problem?id=1905 题意 :在两堵实心墙中间有一根杆,长度为L,然后给它加热,温度是n,则两墙之间的杆会弯曲,长度会变为L'=(1+n*C)*L,求前后两个状 ...

  7. CF 136B Ternary Logic

    http://codeforces.com/problemset/problem/136/B 题意 :就是说tor是一个三进制的运算,代表的是两个三进制数的运算,两个三进制数按位逐一相加后对三取余,没 ...

  8. 【转】notepad++ 应用学习 -- 列模式,十六进制模式

      Notepad++ 顾名思义,是一个比notepad(Windows下叫记事本)的功能更强的编辑器. 总以为notepad++小巧轻盈,而且开源,要比UE(UltraEdit)好用.因为她支持的视 ...

  9. Servlet课程0426(十二)Servlet MV模式下用户登录及查看用户表中所有用户

    Welcome.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; import java ...

  10. nginx配置静态文件服务器

    搭建文件服务器 要点就是root目录,会自动指向索引文件 如: index, index.html等 server { client_max_body_size 4G; listen 80; ## l ...