博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces#86D Powerful array(分块暴力)
阅读量:7239 次
发布时间:2019-06-29

本文共 2099 字,大约阅读时间需要 6 分钟。

Description

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).

Sample Input

Input
3 21 2 11 21 3
Output
36
Input
8 31 1 2 2 1 3 1 12 71 62 7
Output
202020

题意:就问问你统计[L,R]中x1,x2,,xi出现次数a1,a2,,ai的ai*ai*xi和。

题解:分块暴力。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef __int64 LL;const int INF = 0x3f3f3f3f;const int maxn=200000+100;LL up[maxn];int col[maxn],sum[maxn*10],n,m,k;LL ans;struct node{ int l,r; int id;}q[maxn];int cmp(node l1,node l2){ if(l1.l/k==l2.l/k) return l1.r
r) { update(R,-1); R--; } while(L > l) { L--; update(L,1); } while(R < r) { R++; update(R,1); } up[id]=ans; } for(int i=0;i

转载于:https://www.cnblogs.com/clnchanpin/p/6732957.html

你可能感兴趣的文章
数位DP
查看>>
PLSQL Convert Object to String
查看>>
Linux 串口驱动设计二
查看>>
jQuery方法判断checkbox是否选中以及改变checkbox的选中状态
查看>>
CSS样式设置小技巧
查看>>
PDF 补丁丁 0.4.2.1063 测试版发布:新增检查新版本功能
查看>>
Module的加载实现
查看>>
统计学 学习第一季第一集《统计学习方法》第一章 统计学方法概论 简要概述...
查看>>
Ubuntu 安装jdk与tomcat
查看>>
css学习_css补充知识
查看>>
python 08day--软件包的管理及ssh、samba、apache服务
查看>>
ThinkPPHP学习(一)生成图片验证码
查看>>
*Algs4-1.5.19动画-(感觉不正确)
查看>>
git 合并分支
查看>>
hdu 1098 Ignatius's puzz
查看>>
KFC ajax
查看>>
学长哈哈的店公告
查看>>
python 读取 xlsx
查看>>
ethereum/EIPs-1271 smart contract
查看>>
Football
查看>>