莫队算法

/*
莫队算法:用于处理离线的区间问题 
复杂度O(nsqrt(n)) 
1.将元素分块
2.将查询离线排序,按左区间的块编号排序,相同按右区间大小排序
3.维护一个当前的区间,根据访问的区间将左右区间扩张或收缩,整个过程与划窗很像 
*/ 
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;
const int maxn = 5e4 + 5;

ll a[maxn],ans[maxn],pos[maxn],cnt[maxn];
ll res = 0;   //全局变量维护答案 

struct Q{
	int l,r,k;
	bool operator<(const Q&q)const
	{
		if( pos[l] == pos[q.l] ) return r < q.r;
		return pos[l] < pos[q.l];
	}
}q[maxn];

//询问区间的元素个数的平方和 
void add(int x)
{
	res -= cnt[a[x]] * cnt[a[x]];
	cnt[a[x]] ++;
	res += cnt[a[x]] * cnt[a[x]];
}

void sub(int x)
{
	res -= cnt[a[x]] * cnt[a[x]];
	cnt[a[x]] --;
	res += cnt[a[x]] * cnt[a[x]];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m,k;
	cin >> n >> m >> k;
	int siz = sqrt(n);
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		pos[i] = i / siz;   //分块 
	}
	for (int i = 1; i <= m; i++)
	{
		cin >> q[i].l >> q[i].r;
		q[i].k = i;
	}
	sort(q+1,q+1+m);   //排序访问 
	int l = 1,r = 0;   //当前维护的区间 
	for (int i = 1; i <= m; i++)   //移动区间 
	{
		while( q[i].l < l ) add(--l);
		while( q[i].r > r ) add(++r);
		while( q[i].l > l ) sub(l++);
		while( q[i].r < r ) sub(r--);
		ans[q[i].k] = res;
	}
	for (int i = 1; i <= m; i++)
	{
		cout << ans[i] << '\n';
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值