首页 > 其他 > 详细

nyoj 322 Sort 【树状数组】

时间:2014-10-06 21:11:52      阅读:261      评论:0      收藏:0      [点我收藏+]

这道题其实就是考试树状数组。

代码:

#include <cstdio>
#include <cstring>
int c[1005];

int lowbit(int x){
	return x&(-x);
}

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

void add(int x, int val){
	while(x <= 1004){
		c[x] += val;
		x += lowbit(x);
	}
}
int main(){
	int t, n, ans, s;
	scanf("%d", &t);
	while(t --){
		scanf("%d", &n);
		ans = 0;
		memset(c, 0, sizeof(c));
		for(int i = 1; i <= n; i ++){
			scanf("%d", &s);
			ans += (s-getsum(s)-1);
			add(s, 1);
		}
		printf("%d\n", ans);
	}
	return 0;
}        
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=322

nyoj 322 Sort 【树状数组】

原文:http://blog.csdn.net/shengweisong/article/details/39831639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!