莫队算法就是优化的暴力算法。莫队算法是要把询问先按左端点属于的块排序,再按右端点排序。只是预先知道了所有的询问。可以合理的组织计算每个询问的顺序以此来降低复杂度。

                          D. Powerful array

典型的莫队算法题

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int a[MS];
int cnt[*MS];
LL ans[MS]; struct node
{
int l,r;
int no,qid;
bool operator <(const node &a)const
{
return no<a.no||(no==a.no&&r<a.r);
}
}nodes[MS]; int n,SIZE;
LL res;
int L,R;
LL query(int x,int y,int flag)
{
if(flag)
{
for(int i=x;i<L;i++)
{
res+=(cnt[a[i]]<<|)*a[i];
cnt[a[i]]++;
}
for(int i=L;i<x;i++)
{
cnt[a[i]]--;
res-=(cnt[a[i]]<<|)*a[i];
}
for(int i=R+;i<=y;i++)
{
res+=(cnt[a[i]]<<|)*a[i];
cnt[a[i]]++;
}
for(int i=y+;i<=R;i++)
{
cnt[a[i]]--;
res-=(cnt[a[i]]<<|)*a[i];
}
}
else
{
for(int i=x;i<=y;i++)
{
res+=(cnt[a[i]]<<|)*a[i];
cnt[a[i]]++;
}
}
L=x;R=y;
return res;
} int main()
{
int Q;
scanf("%d%d",&n,&Q);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]); // 注意%d的速度远大于%I64d的速度
// 在大量数据输入时,能用%d就不要用%I64d,但千万要注意数据溢出
}
SIZE=sqrt(n+0.5);
for(int i=;i<Q;i++)
{
scanf("%d%d",&nodes[i].l,&nodes[i].r);
nodes[i].no=nodes[i].l/SIZE;
nodes[i].qid=i;
}
sort(nodes,nodes+Q);
memset(cnt,,sizeof(cnt));
res=;
for(int i=;i<Q;i++)
ans[nodes[i].qid]=query(nodes[i].l,nodes[i].r,i);
for(int i=;i<Q;i++)
printf("%I64d\n",ans[i]);
return ;
}

D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力的更多相关文章

  1. [Codeforces86D]Powerful array(莫队算法)

    题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...

  2. Codeforces 86D - Powerful array(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...

  3. codeforces 86D D. Powerful array(莫队算法)

    题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...

  4. CodeForces - 86D D. Powerful array —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...

  5. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

  6. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...

  7. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

  8. CodeForces - 86D Powerful array (莫队)

    题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...

  9. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

随机推荐

  1. JXTA+JavaFX实现的网络黑白棋测试地址

    http://www.qhdbaobao.com/dist/secondegg-reversi.html ——————————————————————————————————————————————— ...

  2. Ubuntu安装Burg

    友情提示:本文只介绍了如何安装Burg,没有关于卸载Burg的相关说明.事实上,我后来直接新装了12.04,我没有卸载Burg的经验.考虑到Burg事关系统引导的大事,安装的话按本文来做应该没有问题, ...

  3. PostQueuedCompletionStatus

    PostQueuedCompletionStatus函数,向每个工作者线程都发送—个特殊的完成数据包.该函数会指示每个线程都“立即结束并退出”.下面是PostQueuedCompletionStatu ...

  4. 检查.net代码中占用高内存函数(翻译)

    哈哈,昨天没事做,在CodeProject瞎逛,偶然看到这篇文章,居然读得懂,于是就翻译了一下,当练习英语,同时增强对文章的理解,发现再次翻译对于文章的一些细节问题又有更好的理解.下面是翻译内容,虽然 ...

  5. perl学习笔记(4)——动态加载

    在写perl的时候,如果要应用到各种平台的话,比如linux 和windows,会遇到各种问题,有时就是要根据系统类型来加载各种库,之前写的就是这样的, if($^O eq 'linux'){ use ...

  6. hdu 2821 Pusher (dfs)

    把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀 ...

  7. X431 元征诊断枪

    X-431 Diagun是专门为汽车维修技师设计的诊断设备. 小巧的主机.强大的诊断功能.方便快捷的网上升级.一体化多功能接头,都是维修技师的首选.X-431 Diagun 是汽车维修技师的标准装备. ...

  8. ThinkSNS插件的使用方法

    1.插件包放到/addons/plugin/ 目录下. 2.自定义一个插件钩子:修改/apps/public/Tpl/default/Index/index.html,约在65行左右,添加下面钩子.{ ...

  9. jquery.validate使用攻略

    主要分几部分 jquery.validate 基本用法 jquery.validate API说明 jquery.validate 自定义 jquery.validate 常见类型的验证代码 下载地址 ...

  10. Codeforces Gym 100463B Music Mess Hash 逻辑题

    Music Mess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments ...