其他分享
首页 > 其他分享> > CF86D Powerful array 题解

CF86D Powerful array 题解

作者:互联网

看到长这样的题目,显然是莫队板子题。

但是不知道为什么很多人写的都是 \(2 \times cnt_x + 1\) 之类的?好像直接先减再加不就好了?公式都不用推。

注意指针顺序以及 long long

目前 CF 的机子上已经不需要用 %l64d 输出 long long,直接 %lld 输出即可。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int MAXA = 1e6 + 10, MAXN = 2e5 + 10;
int n, m, cnt[MAXA], a[MAXN], ys[MAXN], block;
LL total, ans[MAXN];
struct node
{
	int l, r, id;
}q[MAXN];

int read()
{
	int sum = 0, fh = 1; char ch = getchar();
	while (ch < '0' || ch > '9') {if (ch == '-') fh = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {sum = (sum << 3) + (sum << 1) + (ch ^ 48); ch = getchar();}
	return sum * fh;
}

bool cmp(const node &fir, const node &sec)
{
	if (ys[fir.l] ^ ys[sec.l]) return ys[fir.l] < ys[sec.l];
	if (ys[fir.l] & 1) return fir.r < sec.r;
	return fir.r > sec.r;
}

void add(int x)
{
	total -= (LL)cnt[a[x]] * cnt[a[x]] * a[x];
	cnt[a[x]]++;
	total += (LL)cnt[a[x]] * cnt[a[x]] * a[x];
}

void del(int x)
{
	total -= (LL)cnt[a[x]] * cnt[a[x]] * a[x];
	cnt[a[x]]--;
	total += (LL)cnt[a[x]] * cnt[a[x]] * a[x];
}

int main()
{
	n = read(), m = read(); block = sqrt(n);
	for (int i = 1; i <= n; ++i) a[i] = read();
	for (int i = 1; i <= n; ++i) ys[i] = (i - 1) / block + 1;
	for (int i = 1; i <= m; ++i) {q[i].l = read(), q[i].r = read(), q[i].id = i;}
	sort(q + 1, q + m + 1, cmp);
	int l = 1, r = 0;
	for (int i = 1; i <= m; ++i)//l--,r++,r--,l++
	{
		while (l > q[i].l) add(--l);
		while (r < q[i].r) add(++r);
		while (r > q[i].r) del(r--);
		while (l < q[i].l) del(l++);
		ans[q[i].id] = total;
	}
	for (int i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
	return 0;
}

标签:cnt,ch,int,题解,LL,Powerful,long,CF86D,total
来源: https://www.cnblogs.com/Plozia/p/16146473.html