Codeforces 86D Powerful array 莫队算法 分块

时间:2021-07-03 20:02:35

D. Powerful arraytime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 coutstream (also you may use %I64d).

Examplesinput
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):

Codeforces 86D Powerful array 莫队算法 分块
Then K1 = 3K2 = 2K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.


第一次接触莫队算法,传说中的玄学算法,可以解决几乎一切区间离线查询问题。

基本思想就是先分块,再把所有询问区间以区间左端的块数为第一关键字,右端点为第二关键字排序。

之后不断调整 l 和 r,计算两端增加和减少的值。

神奇~


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=200005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
int num;
ll t[1000005],a[maxn],ans[maxn];

struct area{
int l,r,id,kuai;
};
area q[maxn];

bool cmp(area a,area b) {
return a.kuai<b.kuai || (a.kuai==b.kuai&&a.r<b.r);
}

int main() {
int n,m,i,j;
scanf("%d%d",&n,&m);
num=sqrt(n);
if (n%num!=0) num++;
for (i=1;i<=n;i++) scanf("%I64d",&a[i]);
for (i=1;i<=m;i++) {
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
q[i].kuai=q[i].l/num;
}
sort(q+1,q+m+1,cmp);
mem0(t);
ll k=0;
for (i=q[1].l;i<=q[1].r;i++) {
k+=(t[a[i]]*2+1)*a[i];
t[a[i]]++;
}
int l=q[1].l,r=q[1].r;
ans[q[1].id]=k;
//cout << l << ' ' << r << ' ' << k << endl;
for (i=2;i<=m;i++) {
while (r<q[i].r) {
r++;
k+=(t[a[r]]*2+1)*a[r];
t[a[r]]++;
}
while (r>q[i].r) {
k-=(t[a[r]]*2-1)*a[r];
t[a[r]]--;
r--;
}
while (l<q[i].l) {
k-=(t[a[l]]*2-1)*a[l];
t[a[l]]--;
l++;
}
while (l>q[i].l) {
l--;
k+=(t[a[l]]*2+1)*a[l];
t[a[l]]++;
}
ans[q[i].id]=k;
//cout << l << ' ' << r << ' ' << k << endl;
}
for (i=1;i<=m;i++)
printf("%I64d\n",ans[i]);
return 0;
}