码迷,mamicode.com
首页 > 其他好文 > 详细

nyoj 322 Sort 【树状数组】

时间:2014-10-06 22:17:02      阅读:123      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!