码迷,mamicode.com
首页 > 编程语言 > 详细

hdu 5147 Sequence II(树状数组)

时间:2015-01-03 22:26:42      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:hdu 5147 Sequence II

预处理每个位置作为b和c可以组成的对数,然后枚举b的位置计算。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 50005;

int N, arr[maxn], fenw[maxn], lef[maxn], rig[maxn];

#define lowbit(x) ((x)&(-x))
void add(int x, int v) {
	while (x <= N) {
		fenw[x] += v;
		x += lowbit(x);
	}
}

int sum(int x) {
	int ret = 0;
	while (x) {
		ret += fenw[x];
		x -= lowbit(x);
	}
	return ret;
}

int main () {
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		scanf("%d", &N);
		memset(fenw, 0, sizeof(fenw));
		for (int i = 1; i <= N; i++)
			scanf("%d", &arr[i]);

		ll ans = 0, tmp = 0;
		for (int i = 1; i <= N; i++) {
			lef[i] = sum(arr[i]);
			rig[i] = N - i - arr[i] + lef[i] + 1;
			add(arr[i], 1);
			tmp += rig[i];
		}

		for (int i = 1; i <= N; i++) {
			tmp -= rig[i];
			ans += tmp * lef[i];
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


hdu 5147 Sequence II(树状数组)

标签:

原文地址:http://blog.csdn.net/keshuai19940722/article/details/42371559

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!