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. 【Mac】解压文件夹中文乱码

    Mac 用的英文系统,发现下载含中文的文件后,解压为乱码,如图所示: 解决方法: 下载一个解压软件:"The Unarchiver" 安装后设置下,如图: 之后设置压缩文件的默认打 ...

  2. AI中台——智能聊天机器人平台的架构与应用(分享实录)

    内容来源:宜信技术学院第3期技术沙龙-线上直播|AI中台——智能聊天机器人平台 主讲人:宜信科技中心AI中台团队负责人王东 导读:随着“中台”战略的提出,目前宜信中台建设在思想理念及架构设计上都已经取 ...

  3. codeforces 213div(2) 365 A.Good Number 365 B.The Fibonacci Segment

    #include <stdio.h> #include <string.h> bool vis[11]; int n, k; bool judge(int x) { memse ...

  4. ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core

    前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...

  5. Unity经典游戏教程之:弓之骑士

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  6. 洛谷P2125 题解

    吐槽: 只能说这道题很数学,本数学蒟蒻推了半天没推出来,只知道要用绝对值,幸亏教练提醒,才勉强想出正解(似乎不是这样的),真的是很无语. 以上皆为吐槽本题,可直接 跳过 分析: 既然题目是要使书架上的 ...

  7. python多线程与多进程及其区别

    个人一直觉得对学习任何知识而言,概念是相当重要的.掌握了概念和原理,细节可以留给实践去推敲.掌握的关键在于理解,通过具体的实例和实际操作来感性的体会概念和原理可以起到很好的效果.本文通过一些具体的例子 ...

  8. 从SpringBoot构建十万博文聊聊缓存穿透

    前言 在博客系统中,为了提升响应速度,加入了 Redis 缓存,把文章主键 ID 作为 key 值去缓存查询,如果不存在对应的 value,就去数据库中查找 .这个时候,如果请求的并发量很大,就会对后 ...

  9. SAP Special Fields in FAGLL03 transaction

    https://wiki.scn.sap.com/wiki/display/ERPFI/Special+Fields+in+FAGLL03+transaction https://wiki.scn.s ...

  10. Java开发包Jedis

    Jedis: http://www.oschina.net/p/jedis (Redis的官方首选Java开发包) <!--Redis --> <dependency> < ...