【BZOJ2038】[2009国家集训队]小Z的袜子(hose)【莫队】

裸的模板。


/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 50005, maxm = maxn, maxsqrtn = 224;

int n, m, num[maxn], cnt[maxn];
LL ans[maxm], deno[maxm];

struct _query {
	int id, l, r;
} q[maxm];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline bool cmp(_query a, _query b) {
	return a.l / maxsqrtn < b.l / maxsqrtn || (a.l / maxsqrtn == b.l / maxsqrtn && a.r < b.r);
}

inline LL gcd(LL a, LL b) {
	for(; b; b ^= a ^= b ^= a %= b);
	return a;
}

int main() {
	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) num[i] = iread();
	for(int i = 1; i <= m; i++) {
		int l = iread(), r = iread();
		q[i] = (_query){i, l, r};
		deno[i] = (LL)(q[i].r - q[i].l + 1) * (q[i].r - q[i].l);
	}
	sort(q + 1, q + 1 + m, cmp);

	LL tmp = 0;
	int l = 1, r = 0;
	for(int i = 1; i <= m; i++) {
		while(q[i].l < l) {
			l--;
			tmp += cnt[num[l]] << 1LL;
			cnt[num[l]]++;
		}
		while(l < q[i].l) {
			cnt[num[l]]--;
			tmp -= cnt[num[l]] << 1LL;
			l++;
		}
		while(q[i].r < r) {
			cnt[num[r]]--;
			tmp -= cnt[num[r]] << 1LL;
			r--;
		}
		while(r < q[i].r) {
			r++;
			tmp += cnt[num[r]] << 1LL;
			cnt[num[r]]++;
		}
		ans[q[i].id] = tmp;
	}

	for(int i = 1; i <= m; i++) {
		LL div = gcd(ans[i], deno[i]);
		printf("%lld/%lld\n", ans[i] / div, deno[i] / div);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值