D. Powerful array
time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of productsKs·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.

You should calculate the power of t given subarrays.

Input

First line contains two integers n and t (1 ≤ n, t ≤ 200000) — the array length and the number of queries correspondingly.

Second line contains n positive integers ai (1 ≤ ai ≤ 106) — the elements of the array.

Next t lines contain two positive integers lr (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.

Output

Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use%I64d).

Examples
input
3 2
1 2 1
1 2
1 3
output
3
6
input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
output
20
20
20
Note

Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):

Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.

题目链接:CF 86D

用自定义函数来排序会超时的一道题超时N次看了大牛的博客发现把块写到结构体里面速度比较快;把add和del函数写到while里面也可以加快速度,再加上位运算会快一点(注意一下优先级)

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=200010;
const int M=1000010;
LL cnt[M];
int arr[N];
LL ans[N];
int unit;
struct info
{
int l,r;
int d;
int b;
bool operator<(const info &t)const
{
if(b==t.b)
return r<t.r;
return b<t.b;
}
};
info node[N];
int main(void)
{
int n,m,i,j;
scanf("%d%d",&n,&m);
unit=(int)sqrt(n+0.5);
for (i=1; i<=n; ++i)
scanf("%d",&arr[i]);
for (i=0; i<m; ++i)
{
scanf("%d%d",&node[i].l,&node[i].r);
node[i].d=i;
node[i].b=node[i].l/unit;
}
sort(node,node+m);
int L=node[0].l;
int R=L-1;
LL power=0;
for (i=0; i<m; ++i)
{
while (L>node[i].l)
{
--L;
power=power+arr[L]*((cnt[arr[L]]<<1)+1);
++cnt[arr[L]];
}
while (L<node[i].l)
{
--cnt[arr[L]];
power=power-arr[L]*((cnt[arr[L]]<<1)+1);
++L;
}
while (R>node[i].r)
{
--cnt[arr[R]];
power=power-arr[R]*((cnt[arr[R]]<<1)+1);
--R;
}
while (R<node[i].r)
{
++R;
power=power+arr[R]*((cnt[arr[R]]<<1)+1);
++cnt[arr[R]];
}
ans[node[i].d]=power;
}
for (i=0; i<m; ++i)
printf("%I64d\n",ans[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. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  4. Codeforces 86D Powerful array (莫队)

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

  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,Powerful array 莫队

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

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

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

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

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

  9. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

随机推荐

  1. poj 2136 Vertical Histogram 解题报告

    题目链接:http://poj.org/problem?id=2136 题意不难理解,就是输入四行字符串(每行字符总数不超过72个),统计26个英文字母的数目,并按柱状图的形式输出.我的思路就是,先用 ...

  2. 纯css3 加载loading动画特效

    最近项目中要实现当页面还没有加载完给用户提示正在加载的loading,本来是想做个图片提示的,但是图片如果放大电脑的分辨率就会感觉到很虚,体验效果很不好.于是就采用css3+js实现这个loading ...

  3. jQuery Mobile 基础(第三章)

    1.表单: 表单控件: 文本输入框 搜索输入框 单选按钮 复选框 选择菜单 滑动条 翻转拨动开关 fileld容器:如需让标签和表单元素看起来更适应宽屏,请用带有 data-role="fi ...

  4. try-catch 示例

    package unit5; import java.util.Scanner; import javax.print.CancelablePrintJob; import javax.sound.m ...

  5. CodeForces 404C Ivan and Powers of Two

    Ivan and Powers of Two Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  6. javascript优化--02高质量编码

    方法调用: 通常某个对象调用方法查找该方法并将该对象作为该方法的接受者(this): 使用call自定义接受者 可以调用在给定对象中不存在的方法: 定义高阶函数,允许使用者给回调函数指定接受者: 使用 ...

  7. js:数据结构笔记9--二叉树

    树:以分层的方式存储数据:节点:根节点,子节点,父节点,叶子节点(没有任何子节点的节点):层:根节点开始0层: 二叉树:每个节点子节点不超过两个:查找快(比链表),添加,删除快(比数组): BST:二 ...

  8. Hadoop_10_shuffle02_详解Shuffle过程【来源网络】推荐更为详细

    网址:http://www.cnblogs.com/felixzh/p/4680808.html Shuffle过程,也称Copy阶段.reduce task从各个map task上远程拷贝一片数据, ...

  9. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  10. C# 方法调用的切换器 Update 2015.02.02

    在编写应用程序时,我们经常要处理这样的一组对象,它们的类型都派生自同一个基类,但又需要为每个不同的子类型应用不同的处理方法. 通常的做法,最简单的就是用很多 if-else 去判断各自的类型,如下面的 ...