Codeforces 86D:Powerful array

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 productsKs·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 test(s)
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 = 3K2 = 2K3 = 1, so the power is equal to  32·1 + 22·2 + 12·3 = 20.

给出了一个固定的数组,t个区间,询问这个区间 每一个值其个数^2*该值 的和。

莫队再练习一道。然后一开始一直TLE。update函数这样写。

void update(int x, int d)
{
	ans -= num[val[x]] * num[val[x]] * val[x];
	num[val[x]] += d;
	ans += num[val[x]] * num[val[x]] * val[x];
}

后来发现可以化简,原来x^2变(x+1)^2直接 加上2*x+1就好了。同理减法也是。

void update(int x, int d)
{
	ll s = val[x];
	if (d == 1)
	{
		ans += ((num[s] << 1) + 1) * s;
		num[s] ++;
	}
	else
	{
		num[s]--;
		ans -= ((num[s] << 1) + 1) * s;
	}
}
3秒多水过。。。

代码:

#pragma warning(disable:4996)  
#include <iostream>  
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>  
#include <string>  
#include <cmath>
#include <queue>
#include <map>
using namespace std;

typedef long long ll;
#define INF 0x3fffffff
const int mod = 1e9 + 7;
const int maxn = 3000005;

int n, t, bk;
ll val[maxn], pos[maxn];
ll num[maxn], res[maxn], ans;

struct no
{
	int le;
	int ri;
	int id;
}qu[maxn];

bool cmp(no a, no b)
{
	if (pos[a.le] == pos[b.le])
	{
		return a.ri < b.ri;
	}
	else
	{
		return pos[a.le] < pos[b.le];
	}
}

void input()
{
	int i, j;
	scanf("%d%d", &n, &t);
	
	memset(num, 0, sizeof(num));
	bk = ceil(sqrt(1.0*n));

	for (i = 1; i <= n; i++)
	{
		scanf("%I64d", &val[i]);
		pos[i] = (i - 1) / bk;
	}

	for (i = 0; i < t; i++)
	{
		scanf("%d%d", &qu[i].le, &qu[i].ri);
		qu[i].id = i;
	}
}

void update(int x, int d)
{
	ll s = val[x];
	if (d == 1)
	{
		ans += ((num[s] << 1) + 1) * s;
		num[s] ++;
	}
	else
	{
		num[s]--;
		ans -= ((num[s] << 1) + 1) * s;
	}
}

void solve()
{
	int i, j, pl, pr, id;
	sort(qu, qu + t, cmp);

	pl = 1; pr = 0;
	ans = 0;
	
	for (i = 0; i < t; i++)
	{
		id = qu[i].id;
		if (qu[i].le == qu[i].ri)
		{
			res[id] = val[qu[i].le];
			continue;
		}
		if (pr < qu[i].ri)
		{
			for (j = pr + 1; j <= qu[i].ri; j++)
			{
				update(j, 1);
			}
		}
		else
		{
			for (j = pr ; j > qu[i].ri; j--)
			{
				update(j, -1);
			}
		}
		pr = qu[i].ri;

		if (pl < qu[i].le)
		{
			for (j = pl; j < qu[i].le; j++)
			{
				update(j, -1);
			}
		}
		else
		{
			for (j = pl - 1; j >= qu[i].le; j--)
			{
				update(j, 1);
			}
		}
		pl = qu[i].le;
		res[id] = ans;
	}
	for (i = 0; i < t; i++)
	{
		printf("%I64d\n", res[i]);
	}
}


int main()
{
	//freopen("i.txt", "r", stdin);
	//freopen("o.txt", "w", stdout);

	input();
	solve();

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值