Powerful array CodeForces - 86D (莫队算法)
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';
}
}
}
Powerful array CodeForces - 86D (莫队算法)的更多相关文章
- Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)
vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...
- Powerful array CodeForces - 86D(莫队)
给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和.1<=n,m<=200000.1<=s<=10^6 #include <iostr ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;
D. Powerful array time limit per test seconds memory limit per test megabytes input standard input o ...
- [Codeforces86D]Powerful array(莫队算法)
题意:定义K[x]为元素x在区间[l,r]内出现的次数,那么它的贡献为K[x]*K[x]*x 给定一个序列,以及一些区间询问,求每个区间的贡献 算是莫队算法膜版题,不带修改的 Code #includ ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法
E. XOR and Favorite Number 题目连接: http://www.codeforces.com/contest/617/problem/E Descriptionww.co Bo ...
- Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】
任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...
- 莫队算法初识~~CodeForces - 617E
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
随机推荐
- MySQL 给已存在的数据表 增加字段和注释
MySQL 给已存在的数据表 增加字段和注释 问题描述 在开发一个系统的过程中,经常会遇到随着系统服务功能的扩展,或者服务之间的关联,需要适当的修改原有的表结构,比如,增加一些必要的字段. 示例:在已 ...
- java数据结构之ArrayList
一.ArrayList源码注释 /** * ArrayList源码分析,jdk版本为1.8.0_121 */ public class ArrayList<E> extends Abstr ...
- RDD实例
实例一: teacher.log http://bigdata.baidu.cn/zhangsan http://bigdata.baidu.cn/zhangsan http://bigdata.ba ...
- Postman接口测试:自动获取登录后的cookie并设置环境变量
在对网站进行接口测试的时候,很多请求往往是需要带登录的cookie才能请求成功的,一般来说,可以用抓包软件(fiddler,浏览器的F12)来查看登录后的cookie,并把它设置到postman的环境 ...
- VS快捷键操作
1.窗口快捷键记忆诀窍: 凡跟窗口挂上钩的快捷键必有一个W(Windows):Ctrl+W,W: 浏览器窗口 (浏览橱窗用有道的翻译是window shopping) Ctrl+W,S: 解决方案管理 ...
- pandas将非数值型特征转化为数值型(one-hot编码)
import pandas as pd import numpy as np import matplotlib.pyplot as plt name = np.array([['jack', 'ro ...
- VirtualBox下Centos6.8网络配置
win10环境下,VirtualBox和Centos6.8已经按照完毕,下面配置Centos6.8网络. 1.设置VirtualBox为桥接模式,具体的有三种联网方法,我们参考http://www.c ...
- python基础之字典dict
不可变数据类型:tuple.bool.int.str --可哈希类型可变数据类型:list.dict.set --不可哈希类型dict-key 必须是不可变数据类型,可哈希dict-value 任意数 ...
- 【Python开发】Python之re模块 —— 正则表达式操作
Python之re模块 -- 正则表达式操作 这个模块提供了与 Perl 相似l的正则表达式匹配操作.Unicode字符串也同样适用. 正则表达式使用反斜杠" \ "来代表特殊形式 ...
- 常见网络摄像机的端口及RTSP地址
海康威视默认IP地址:192.168.1.64/DHCP 用户名admin 密码自己设端口:“HTTP 端口”(默认为 80).“RTSP 端口”(默认为 554).“HTTPS 端 口”(默认 44 ...