题目链接:点击传送
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 products Ks·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 21 2 11 21 3

output
36

input
8 31 1 2 2 1 3 1 12 71 62 7

output
202020

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.

莫队板子题;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=2e5+10,M=1e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
///   数组大小
int pos[N],k,a[N],ji[M];
struct is
{int l,r,p;bool operator <(const is &b)const{if(pos[l]==pos[b.l])return r<b.r;return pos[l]<pos[b.l];}
}s[N];
ll ans;
void add(int x)
{ans-=1LL*ji[a[x]]*ji[a[x]]*a[x];ji[a[x]]++;ans+=1LL*ji[a[x]]*ji[a[x]]*a[x];
}
void del(int x)
{ans-=1LL*ji[a[x]]*ji[a[x]]*a[x];ji[a[x]]--;ans+=1LL*ji[a[x]]*ji[a[x]]*a[x];
}
ll out[N];
int main()
{int n,q;scanf("%d%d",&n,&q);k=sqrt(n);for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[i]=(i-1)/k+1;for(int i=1;i<=q;i++)scanf("%d%d",&s[i].l,&s[i].r),s[i].p=i;sort(s+1,s+1+q);int L=1,R=0;for(int i=1;i<=q;i++){while(L<s[i].l){del(L);L++;}while(L>s[i].l){L--;add(L);}while(R>s[i].r){del(R);R--;}while(R<s[i].r){R++;add(R);}out[s[i].p]=ans;}for(int i=1;i<=q;i++)printf("%lld\n",out[i]);return 0;
}

转载于:https://www.cnblogs.com/jhz033/p/6662190.html

Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队相关推荐

  1. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队算法

    链接: http://codeforces.com/problemset/problem/86/D 题意: 给你一个数组,每次询问一个区间,求对于每个数,算出这个数在这个区间出现的个数的平方再*这个数 ...

  2. CodeForces 86 D Powerful array 莫队

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

  3. cf D. Powerful array 莫队算法

    D. Powerful array 题意:给定一个序列>>每次查询一个区间>>查询该区间内 出现过的数字*出现的次数的平方 的和 思路:学习莫队的第一题或者说小z的袜子是第一题 ...

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

    题目链接:点击查看 题目大意:给出一个由n个数字组成的数列,再给出m次查询,每次查询要求输出[l,r]中的答案,这个题目的答案为: 假设x为区间[l,r]内的数,出现的次数记为cnt[x],则数x的贡 ...

  5. 【打CF,学算法——四星级】CodeForces 86D Powerful array (莫队算法)

    [CF简介] 题目链接:CF 86D 题面: D. Powerful array time limit per test 5 seconds memory limit per test 256 meg ...

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

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

  7. Powerful array(CF-86D)

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

  8. Codeforces D. Powerful array(莫队)

    题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...

  9. #6164. 「美团 CodeM 初赛 Round A」数列互质-莫队

    #6164. 「美团 CodeM 初赛 Round A」数列互质 思路 : 对这个题来言,莫队可以 n*根号n 离线处理出各个数出现个的次数 ,同时可以得到每个次数出现的次数 , 但是还要处理有多少 ...

最新文章

  1. 实验吧 速度爆破
  2. ACMNO.25 C语言-间隔输出 写一函数,输入一个四位数字,要求输出这四个数字字符,但每两个数字间空格。如输入1990,应输出1 9 9 0。 输入 一个四位数 输出 增加空格输出
  3. jmeter mysql plugin_安装一个jmeter的插件--实现监控事务响应时间、tps等功能
  4. OVS openflow(二十四)
  5. django 内置 admin
  6. c++的引用是什么意思?怎么回事?
  7. sql server 2008学习9 视图
  8. jquery $.trim()方法使用介绍
  9. django-关于a标签路径的测试
  10. 软件公司怎么定价它们的项目_如何为副项目定价
  11. linux 系统 网卡 ethX没有显示IP的处理方式
  12. 一支口红用了5年_用了7年微信才知道!原来微信隐藏5大实用功能,比app更好用...
  13. 20135226黄坤信息安全系统设计基础期末总结
  14. 使用域名访问服务器网站,使用域名访问网站是啥意思
  15. 谷歌访问英文网站翻译为中文
  16. 序列化对象互转--Jackson中的ObjectMapper,解决超类派生问题,序列化问题
  17. Android sim卡 pin码解锁流程.
  18. 单链表指定结点的前插与后插(C/C++)
  19. 痞子衡嵌入式:语音处理工具pzh-speech诞生记(6)- 文语合成实现(pyttsx3, eSpeak1.48.04)...
  20. 猿辅导python助教面试两次都有什么内容_来还愿!终于收到猿辅导offer了,但是23k*14,手…程序员分享...

热门文章

  1. python如何保持数据类型不变_python 可变和不可变数据类型、格式化输出和基础运算符...
  2. workbench拓扑优化教程_workbenchds拓扑优化分析.ppt
  3. python源码笔记_python源码学习笔记(二)
  4. android 音频合成_【Android工具】用手机测量噪声的工具软件,噪声仪分贝计,量化噪声声音工具...
  5. linux编写一个简单的端口扫描程序,小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)...
  6. 未发现oracle(tm)客户端和网络组件_SpringColud Eureka的服务注册与发现
  7. python逻辑运算符的使用_总结Python中逻辑运算符的使用
  8. python输入多个坐标点_判断多个坐标是否在同一条直线上|Python练习系列[13]
  9. c语言字符指针初始化赋值,C语言_指针变量的赋值与运算,很详细详解
  10. 【渝粤教育】国家开放大学2018年秋季 0267-21T摄影技术 参考试题