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 莫队的更多相关文章

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

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

  2. codeforces 86D,Powerful array 莫队

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

  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 - Powerful array(莫队算法)

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

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

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

  6. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

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

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

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

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

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

  9. codeforces 220B . Little Elephant and Array 莫队+离散化

    传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...

随机推荐

  1. 关于FFT分析音频的学习

    本文部分知识从以下文章学习: https://zhuanlan.zhihu.com/p/19763358 傅里叶变换的知识 https://www.cnblogs.com/RabbitHu/p/FFT ...

  2. 基于 HTML5 WebGL 的加油站 3D 可视化监控

    前言 随着数字化,工业互联网,物联网的发展,我国加油站正向有人值守,无人操作,远程控制的方向发展,传统的人工巡查方式逐渐转变为以自动化控制为主的在线监控方式,即采用数据采集与监控系统 SCADA.SC ...

  3. React 如何搭建脚手架

    React 如何搭建脚手架   npm install -g create-react-app    //安装 create-react-app react-demo    // react-demo ...

  4. 1、Java小白之路前言

    大二一年准备好好学习Java,养成一个良好的习惯写博客,但是由于各种各样的原因,并没有坚持下来.而正好又赶上大三结束,去实习,发现自己的基础还是有些薄弱,所以决定,重新走上这条Java小白之路. 时隔 ...

  5. html学习笔记整理

    网页 1.网页的组成部分 网页是由文字,图片,视频,音频,输入框,按钮这些元素(也就是html标签)组成. 2.浏览网页常用的五大主流浏览器 谷歌,IE,火狐,欧朋,safari.浏览器的内核(渲染引 ...

  6. 鲜为人知的maven标签解说

    目录 localRepository interactiveMode offline pluginGroups proxies servers mirrors profiles 使用场景 出现位置 激 ...

  7. React的新特性 ---- Hooks ---- 的基本使用

    一.react-hooks概念 React中一切皆为组件,React中组件分为类组件和函数组件,在React中如果需要记录一个组件的状态的时候,那么这个组件必须是类组件.那么能否让函数组件拥有类组件的 ...

  8. ABAP:如何等待小数秒数

    WAIT UP TO x SECONDS. 和CALL FUNCTION 'ENQUE_SLEEP'都只能支持整数的秒数(如果是非整数,则四舍五入),如果要WAIT非整数的描述,可以如下写法:

  9. Laya 中缩放的实现

    Laya 缩放功能的实现 在 laya 中实现滚轮对选中对象的缩放,涉及到以下两个模块: 事件 容器坐标 1. 事件 在 Laya 中, Event 是事件类型的集合.包含了常见的鼠标事件.键盘事件. ...

  10. 网络安全攻击与防护--HTML学习

    第一节. HTML基本语法(文末有对该文视频讲解) HTML的官方介绍什么的我就不说了,打字也挺累的,只简单介绍一下吧,其他的懂不懂都没关系. HTML全称为Hypertext Markup Lang ...