数星星(树状数组)

Description

天文学家经常观察星象图。星象图中用平面上的点来表示一颗星星,每一颗星星都有一个笛卡尔坐标。设定星星的等级为其左下角星星的总数。天文学家们想知道星星等级的分布情况

                                             **(5)

                       **(4)

*(1)                                  **(2)         **(3)


比如上图,5号星星的等级为3(其左下角有编号为1、2、4的星星共三颗)。2号星星和4号星星的等级为1。在上图中只有一颗星星等级为0,两颗星星等级为1,一颗星星等级为2,一颗星星等级为3。
给定一个星象图,请你写一个程序计算各个等级的星星数目。

Input

输入的第一行包含星星的总数N (1<=N<=15000)。接下来N行,描述星星的坐标(X,Y)(X和Y用空格分开,0<=X,Y<=32000)。星象图中的每个点处最多只有一颗星星。所有星星按Y坐标升序排列。Y坐标相等的星星按X坐标升序排列。

Output

输出包含N行,每行一个整数。第一行包含等级0的星星数目,第二行包含等级1的星星数目,依此类推,最后一行包含等级为N-1的星星数目。

Sample Input

5

1 1

5 1

7 1

3 3

5 5

Sample Output

1

2

1

1

0

#include <bits/stdc++.h>
using namespace std;
const int maxn = 15010;
const int maxx = 32010;
/*struct node 
{
	int x,y;
}p[maxn];
*/
int n,m=32010;
int c[maxx],ans[maxx];
int lowbit(int x)
{
	return x&(-x);
}
void update(int x,int y)
{
	for(;x<=m;x+=lowbit(x))
	{
		c[x] += y;
	}
}
int sum(int x)
{
	int res = 0;
	for(;x;x-=lowbit(x))
		res += c[x];
	return res;
}
int main()
{
	scanf("%d",&n);
	int x,y;
	for(int i=1;i<=n;i++)
	{	
		scanf("%d%d",&x,&y);
		x++;
		int temp = sum(x);
		update(x,1);
		ans[temp]++;
	} 
	/*for(int i=1;i<=n;i++)
	{
		int u = p[i].x+1;
		int v = sum(u);
		update(u,1);
		ans[v]++;
	}*/
	for(int i=0;i<n;i++)
		printf("%d\n",ans[i]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/waterboy_cj/article/details/81365639