數星星(樹狀數組)

 

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;
}

 

發佈了89 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章