86D - Powerful array

莫队。

统计ai[i]的出现次数,每一次先还原贡献,再加上或减去当前的贡献即可。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
	char ch = getchar(); ll x = 0, f = 1;
	while (ch<‘0‘ || ch>‘9‘) { if (ch == ‘-‘)f = -1; ch = getchar(); }
	while (ch >= ‘0‘ && ch <= ‘9‘) { x = x * 10 + ch - ‘0‘; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 2e5 + 10;
int ai[N], belong[N];
int n, t, block;
ll ans = 0;
int cnt[N * 10];
int L = 1, R;
struct node {
	int l, r, id;
	bool operator<(const node temp)const {
		if (belong[l] == belong[temp.l])
		{
			if (belong[l] & 1)
			{
				return r < temp.r;
			}
			else return r > temp.r;
		}
		else return l < temp.l;
	}
}qr[N];
void add(int x)
{
	ans -= 1ll * cnt[x] * x* cnt[x];
	cnt[x]++;
	ans += 1ll * cnt[x] * x* cnt[x];
}
void del(int x)
{
	ans -= 1ll * cnt[x] * x* cnt[x];
	cnt[x]--;
	ans += 1ll * cnt[x] * x* cnt[x];
}
ll ans_mo[N];
void mo()
{
	upd(i, 1, t)
	{
		while (L < qr[i].l)del(ai[L++]);
		while (L > qr[i].l)add(ai[--L]);
		while (R < qr[i].r)add(ai[++R]);
		while (R > qr[i].r)del(ai[R--]);
		ans_mo[qr[i].id] = ans;
	}
	upd(i, 1, t)
	{
		cout << ans_mo[i] << endl;
	}
}
int main()
{
	cin >> n >> t;
	block = sqrt(n);
	upd(i, 1, n) {
		cin >> ai[i];
		belong[i] = (i - 1) / block + 1;
	}
	upd(i, 1, t)
	{
		qr[i].id = i;
		qr[i].l = read();
		qr[i].r = read();
	}
	sort(qr + 1, qr + 1 + t);
	mo();
	return 0;
}

原文地址:https://www.cnblogs.com/LORDXX/p/12590958.html

时间: 2024-07-30 23:10:11

86D - Powerful array的相关文章

Codeforces#86D Powerful array(分块暴力)

Description 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

Codeforces 86D Powerful array(莫队)

题目链接:http://codeforces.com/problemset/problem/86/D 题目: 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 occu

CodeForces 86D Powerful array(莫队算法)

和BZOJ2038差不多..复习一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 int block; 6 struct Query{ 7 int i,l,r; 8 bool operator<(const Query &q)const{ 9 if(l/block==q.l/block) return r<q.r; 10 re

【分块】Codeforces 86d Powerful array

通道 题意:询问[l,r]区间的权和,权定义为sum(k^2*a[i]),k表示a[i]出现的次数 思路:区间每增加一个a[i],增量是(2*x+1)*a[i],因为(x+1)^2*a[i] = (x^2 +2*x + 1)*a[i] 分块排序即可,块内按r排序 代码: #include <cstdio> #include <algorithm> #include <cmath> using namespace std; #define N 200100 typedef

CodeForces - 86D Powerful array (莫队)

题意:查询的是区间内每个数出现次数的平方×该数值的和. 分析:虽然是道莫队裸体,但是姿势不对就会超时.答案可能爆int,所以要开long long 存答案.一开始的维护操作,我先在res里减掉了a[pos]*cnt[a[pos]]*cnt[a[pos]],将cnt[a[pos]]+1,再将乘积加回.其实根据数学原理,K^2和(K+1)^2差值是2K+1,那么其实每次更新时只要加上或减去a[pos]*(2*cnt[pos]+1)即可,这样更高效. #include<bits/stdc++.h>

codeforces 86D D. 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, wh

D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array 典型的莫队算法题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include

D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;

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?

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 th