POJ2352 Stars(樹狀數組)

和之前那個Japan 那題差不多,而且這個還給排好序了。。。贊!


這樣的話 ,只要比較x座標, 因爲y是排好的,後面的點不會對前面的點產生影響。

這樣,還是求逆序數。


AC Memory : 908 KB     Time : 391 ms


代碼:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int c[32005];
int res[32005];
int lowbit(int x)
{
	return x&(-x);
}

void update(int i,int val)
{
	while(i<=32002)
	{
		c[i]+=val;
		i += lowbit(i);
	}
}

int sum(int i)
{
	int s= 0;
	while(i>0)
	{
		s+=c[i];
		i-=lowbit(i);
	}
	return s;
}

int main()
{
	int n,x,y;
	scanf("%d",&n);
	memset(c,0,sizeof(c));
	memset(res,0,sizeof(res)); 
	for(int i = 0;i<n;++i)
	{
		scanf("%d%d",&x,&y);
		++res[sum(x+1)];
		update(x+1,1);
	}
	for(int i = 0;i<n;++i)
	{
		printf("%d\n",res[i]);
	}
	
	return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章