CodeForces 86 D Powerful array 莫队
题意:求区间[l, r] 内的数的出现次数的平方 * 该数字。
题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了。
这个题目中发现了一个很神奇的事情,本来数组开1e6大小就直接过了4100+ms, 想测试一下inline,顺手把空间砍成了刚好够用,然后跑的更慢了 4700+ms,删了inline之后T了,到现在也不知道发生了啥。
如果有大牛路过希望帮我看下, CF run id 38822420(4100+) 38822607(4700+)38822636(TLE)。然后就是我空间稍微开大一点就跑的更快了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define _S(X) cout << x << ' ';
#define __S(x) cout << x << endl;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 8e6+ ;
int n, m, blo;
int cnt[N];
int a[N];
LL ans[N];
LL tmp;
struct Node{
int l, r;
int id;
}q[N];
void Add(int p){
tmp -= 1ll * a[p] * cnt[a[p]] * cnt[a[p]];
cnt[a[p]]++;
tmp += 1ll * a[p] * cnt[a[p]] * cnt[a[p]];
}
void Remove(int p){
tmp -= 1ll * a[p] * cnt[a[p]] * cnt[a[p]];
cnt[a[p]]--;
tmp += 1ll * a[p] * cnt[a[p]] * cnt[a[p]];
}
bool cmp(Node x1, Node x2){
if(x1.l/blo != x2.l/blo)
return x1.l/blo < x2.l / blo;
return x1.r < x2.r;
}
int main(){
scanf("%d%d", &n, &m);
blo = sqrt(n);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q+, q++m, cmp);
int nL = , nR = , tL, tR;
for(int i = ; i <= m; i++){
tL = q[i].l, tR = q[i].r;
while(nL < tL) Remove(nL++);
while(nL > tL) Add(--nL);
while(nR <= tR) Add(nR++);
while(nR > tR+) Remove(--nR);
ans[q[i].id] = tmp;
}
for(int i = ; i <= m; i++){
printf("%I64d\n", ans[i]);
}
return ;
}
86D
CodeForces 86 D Powerful array 莫队的更多相关文章
- 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内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- codeforces 86D D. Powerful array(莫队算法)
题目链接: D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
- CodeForces - 86D Powerful array (莫队)
题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[p ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
随机推荐
- 【Android Studio】常用快捷键
1. 删除一行:Ctrl + X 更新中……
- 使用f12定位bug
为什么找到网站中的bug后还要去分析它到底是属于前端bug还是后端bug 三个原因: 1.在一些公司,一个系统可能是由前端团队和后端团队共同开发出来的,因此在分配bug的时候,不同模块的bug一般都会 ...
- WPF ContextMenu+VisualTreeHelper实现删除控件操作
<UserControl MouseRightButtonDown="UserControl_MouseRightButtonDown" > <UserC ...
- Angualr6表单提交验证并跳转
在Angular6中,使用NG-ZRROR作为前端开发框架,在进行表单开发时遇到了一些问题,最后解决了,在此记录. 1.表单构造: 引入forms: import { FormGroup, FormB ...
- 全球十大OTA 谁能有一席之地?
全球十大OTA 谁能有一席之地? http://www.traveldaily.cn/article/78381/1 2014-03-05 来源:i黑马 随着旅游行业日新月异的发展,在线旅游网站的出现 ...
- Opengl_入门学习分享和记录_01_Graphics Pipeline(图形渲染管线)
写在前面的废话: 这个系列拖更了很久,很大一定程度上是由于自己最近忙着高数学习和线代学习..感觉时间特别紧缺(其实就是我太懒了)好了不废话了,总结开始. 正文开始: 首先Graphics Pipeli ...
- 谈谈你对java平台的理解?
问题:谈谈你对java平台的理解?java是解释执行,这句话对吗? 典型回答:java本身是一种面向对象的语言,具有很好的跨平台的能力,能够做到“write once ,run anywhere”.另 ...
- Android8.1 MTK平台 截屏功能分析
前言 涉及到的源码有 frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java vend ...
- OCP培训 Oracle 12c/18c/19c OCP认证实战培训【送OCP优惠名额】
一.OCP培训 Oracle 12c/18c/19c OCP认证全套实战培训[送OCP优惠名额],本课程内容 课程目标: 为满足想参加Oracle OCP考证的学员,风哥设计的一套比较全面OCP实战培 ...
- 云上RDS架构
概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...