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 莫队算法或者说块状数组 其实都是有点优化的暴力的更多相关文章
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- CodeForces - 86D D. Powerful array —— 莫队算法
题目链接:http://codeforces.com/problemset/problem/86/D D. Powerful array time limit per test 5 seconds m ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
随机推荐
- 转】使用Maven编译项目遇到——“maven编码gbk的不可映射字符”解决办法
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4239006.html 感谢! 一.问题描述 今天在MyEclipse中使用Maven编译项目源代码时,结果如下了 ...
- 安装Win7和Office2010并激活
1. 下载Win7 建议安装原版的win7 SP1 64位中文旗舰版,不建议安装Ghost版本,之前用U盘安装Ghost版本一直失败.原版的下载地址为,选其中一个地址下载就行了. ed2k://|fi ...
- 【原】Hadoop伪分布模式的安装
Hadoop伪分布模式的安装 [环境参数] (1)Host OS:Win7 64bit (2)IDE:Eclipse Version: Luna Service Release 2 (4.4.2) ( ...
- Represent nil with NSNull
[Represent nil with NSNull] It’s not possible to add nil to the collection classes described in this ...
- error日志
2016/06/15 微信调核心时通用意外险 2016-06-15 11:44:23,771>>INFO >> com.isoftstone.core.service.comm ...
- Windows下Mysql5.6启用监控执行脚本的日志
修改my.ini (我的MySQL安装位置是:E:\MySQL\MySQL Server 5.6) log-output=FILE general-log=1 general_log_file=&qu ...
- 修改Android 程序的icon快捷方式图标和名称
在res/drawable-hdpi或res/drawable-ldpi或res/drawable-mdpi目录下,加下你要显示的图片,最好后缀是为.png的,然后修改AndroidManifest. ...
- setTimeout setInterval 区别 javascript线程解释
原文:http://www.iamued.com/qianduan/1645.html 今天看到这篇文章,学到了不少东西 特此发出来 和大家分享 JavaScript的setTimeout与setIn ...
- 使用Office-Word的博客发布功能(测试博文)
本人打算在博客园开博,但平时收集和整理资料都在OneNote中,又不想在写博客时还要进行复制粘贴操作,于是就想到了Microsoft Office自带的博客发布功能.在此做了一下测试,发布了此博文. ...
- 转载:页面加载swf插件:swfobject
转自:http://www.cnblogs.com/analyzer/articles/1299592.html 我一直都在用SWFObject 插入flash,好处多多,代码简洁,不会出现微软的“单 ...