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 l, r (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.

题意:
给定一个长度N的数组a

要求:询问区间1<=L<=R<=N中,每个数字出现次数的平方与当前数字的乘积和

思路:

用一个数组flag[i] 表示 数字i在当前区间出现的次数。

然后正常的莫队add,del转移即可。。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
pll ans[maxn];
ll Ans = 0ll;
int l = 0;
int r = 0;
struct node {int l, r, id;
} a[maxn];
int pos[maxn];
int n, m;
int len;
bool cmp(node aa, node bb)
{if (pos[aa.l] == pos[bb.l]) {return aa.r < bb.r;} else {return pos[aa.l] < pos[bb.l];}
}
int col[maxn];
int flag[maxn];
void add(int x)
{Ans-=1ll*col[x]*flag[col[x]]*flag[col[x]];flag[col[x]]++;Ans+=1ll*col[x]*flag[col[x]]*flag[col[x]];
}
void del(int x)
{Ans-=1ll*col[x]*flag[col[x]]*flag[col[x]];flag[col[x]]--;Ans+=1ll*col[x]*flag[col[x]]*flag[col[x]];
}
int main()
{//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);gg(n); gg(m);len = (int)(sqrt(n));repd(i, 1, n) {gg(col[i]);}repd(i, 1, m) {gg(a[i].l);gg(a[i].r);a[i].id = i;pos[i] = i / len;}sort(a + 1, a + 1 + m, cmp);repd(i, 1, m) {while (l > a[i].l) {l--;add(l);}while (r < a[i].r) {r++;add(r);}while (l < a[i].l) {del(l);l++;}while (r > a[i].r) {del(r);r--;}ans[a[i].id].fi = Ans;}repd(i, 1, m) {printf("%lld\n", ans[i].fi);}return 0;
}inline void getInt(int *p)
{char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11365533.html

Powerful array CodeForces - 86D (莫队算法)相关推荐

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

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

  2. cf D. Powerful array 莫队算法

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

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

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

  4. 分治 —— 莫队算法

    [概述] 莫队算法(mo's algorithm)是用来解决离线区间不修改询问问题,可以将复杂度优化到 O(n^1.5),除去普通的莫队算法外,还有带修改的莫队.树上莫队等等. 莫队常用于维护区间答案 ...

  5. 离线区间的神奇——莫队算法

    离线区间的神奇--莫队算法 前言 一.什么是莫队算法? 二.例题分析: 1.相关例题 题目描述 输入格式 输出格式 简单分析: 三.莫队算法思想: 那么要怎么做(预处理):莫队算法优化的核心是**分块 ...

  6. NBUT 1457 Sona(莫队算法+离散化)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  7. XOR and Favorite Number CF340E 莫队算法

    题目链接 题意:求给定询问区间[L,R]问有多少连续区间异或值等k,多次询问可以离线. a[i]^a[i+1]^a[i+2]^a[n]=(a[1]^a[2]^a[3]^...^a[i-1])^(a[1 ...

  8. Hdu 6534 Chika and Friendly Pairs 莫队算法+树状数组

    题目链接 题意求给区间[L,R]中有少对(i,j)满足i<j且abs(a[i]-a[j])<=k. 首先来说暴力的方法就是离散化,然后用树状数组来维护,但是m次询问,m很大,所以说一定会t ...

  9. BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MB Submit: 9894  Solved: 4561 [Su ...

最新文章

  1. Ubuntu 18.04时间同步
  2. linux centos7 升级gcc版本 使用 yum centos-release-scl devtoolset-8-gcc* 非源码编译
  3. 【机器视觉】 dev_map_par算子
  4. XML 读写(命名空间)
  5. SAP Spartacus 读取payment detail数据的API
  6. python导出csv有引号_python – csv中的双引号元素不能用pandas读取
  7. matlab画一个电动机系统图,基于MATLABGUI的电机学仿真实验系统设计
  8. C算法编程题(七)购物
  9. Unix系统编程()发送信号kill
  10. 如何选择适合自己的相机?
  11. 螺旋桨的制作图文教程
  12. 5.1.2全景声音箱摆位_5.1.2全景声系统私人家庭影院设计方案
  13. 树莓派使用pigpio 控制舵机
  14. 20.04Ubuntu安装桌面系统详细教程
  15. 软件工程作业之甘特图
  16. 手机系统更新(提示已是最新版本),怎么升级更新
  17. 一文详解 .obj 和 .mtl文件格式
  18. 均匀传输线插入损耗分析
  19. nimi SearchEngin 项目思路及算法
  20. Hyperledger/cello实际测试——SDK

热门文章

  1. Jerry的SAP Kyma和Kubernetes讲座的幻灯片分享
  2. 树状数组基础原理与模板
  3. 什么是java构造函数_什么是java构造函数
  4. jvm内存参数配置_“步步精心”-常用JVM配置参数
  5. mybatis的增删改操作及需要注意的问题
  6. 计算机视觉算法工程师 笔试,深度学习算法工程师笔试题目
  7. python升级pip_新手求助, python 升级 pip 失败
  8. at24c16如何划分出多个读写区_读写AT24C02,并用1602显示
  9. python在金融工程领域包括_金融工程专业对计算机的能力要求到底是什么?
  10. android java获取string.xml_android获取string.xml的值