题目链接 Powerful array

给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和。

$1<=n,m<=200000,   1<=s<=10^{6}$

考虑莫队算法

把区间排序,然后让l和r分别询问即可。

根据排序的方式,l指针和r指针移动次数和大概是$n\sqrt{n}$ 级别的。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 2e5 + 10;
const int M = 1e6 + 10; int belong[N]; struct query{
int l, r;
int id;
void scan(){ scanf("%d%d", &l, &r); }
friend bool operator < (const query &a, const query &b){
return belong[a.l] == belong[b.l] ? a.r < b.r :
belong[a.l] < belong[b.l];
}
} q[N]; int n, m;
int a[N], c[M];
int BS, l, r;
LL ret[N], ans; int main(){ scanf("%d%d", &n, &m);
rep(i, 1, n) scanf("%d", a + i); BS = sqrt(n);
rep(i, 1, n) belong[i] = (i - 1) / BS + 1;
rep(i, 1, m){
q[i].scan();
q[i].id = i;
} sort(q + 1, q + m + 1);
l = 0; r = 0; ans = 0; rep(i, 1, m){
while (l > q[i].l){
--l;
ans += (LL)(2 * c[a[l]] + 1) * a[l];
++c[a[l]];
} while (r < q[i].r){
++r;
ans += (LL)(2 * c[a[r]] + 1) * a[r];
++c[a[r]];
} while (l < q[i].l){
--c[a[l]];
ans -= (LL)(2 * c[a[l]] + 1) * a[l];
++l;
} while (r > q[i].r){
--c[a[r]];
ans -= (LL)(2 * c[a[r]] + 1) * a[r];
--r;
} ret[q[i].id] = ans;
} rep(i, 1, m) printf("%lld\n", ret[i]);
return 0;
}

Codeforces 86D Powerful array (莫队算法)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  8. codeforces 86D,Powerful array 莫队

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

  9. codeforces 86D : Powerful array

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

随机推荐

  1. 行内元素的padding和margin是否无效

    html中元素分为三种:块级元素.行内元素(也叫内联元素),内联块级元素. 常用块级元素:<div>.<p>.<h1>...<h6>.<ol> ...

  2. 【技巧:字符串同构】Avendesora

    判断字符串“同构”的技巧 题目大意 给定A,B两个序列,要求B在A中出现的次数以及位置.定义字符变换:把所有相同的字符变为另一种字符:两个字符串相等:当且仅当一个字符串可以在若干次字符变换之后变为另一 ...

  3. 一次线上mysql死锁分析

    一.现象 发运车次调用发车接口时发生异常,后台抛出数据库死锁日志. 二.原因分析 通过日志可以看出事务T1等待 heap no 8的行锁 (X locks 排他锁) 事务T2持有heap no 8的行 ...

  4. 【Java_基础】并发、并行、同步、异步、多线程的区别

    1. 并发:位于同一个处理器上的多个已开启未完成的线程,在任意一时刻系统调度只能让一个线程获得CPU资源运行,虽然这种调度机制有多种形式(大多数是以时间片轮巡为主).但无论如何,都是通过不断切换需要运 ...

  5. Git学习——版本切换

    版本回退 回退到前面几个版本的命令如下: git reset --hard HEAD^ //回退到前一个版本 git reset --hard HEAD^^ //回退到前前一个版本 git reset ...

  6. C语言:哲学家吃饭问题

    //五个哲学家围坐在一起,两人之间都放有一个叉子,意大利面需要2个叉子吃,哲学家吃饭时候叉子只能拿左右手,哲学家除了吃饭时间其他时间都在思考 #include <stdio.h> #inc ...

  7. CSS3-文本-text-shadow

    一.text-shadow 语法: text-shadow : none | <length> none | [<shadow>, ] * <shadow> 或no ...

  8. cache支持三种pre-fetch方式:normal/pre-fetch1/pre-fetch2-way1/pre-fetch-way2

    1.normal fetch  ----fetch 1 cache line once 2. pre-fetch mode one ---- fetch 3 cache line once 3.pre ...

  9. php官方微信接口大全

    微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中.内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI <?php class ...

  10. WordPress的编译器功能扩展

    //php代码如下://向文章编辑器的Visual区添加自定义按钮,js文件存放在wp-content/plugins/文件夹下 add_action('admin_head', 'my_custom ...