树状数组 poj 2352 Stars
来源:互联网 发布:cp雾化器做芯数据 编辑:程序博客网 时间:2024/05/29 04:30
Stars
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 18416Accepted: 8041
Description
Astronomers often examine star maps where starsare represented by points on a plane and each star has Cartesiancoordinates. Let the level of a star be an amount of the stars thatare not higher and not to the right of the given star. Astronomerswant to know the distribution of the levels of thestars.
For example, look at the map shown on the figure above. Level ofthe star number 5 is equal to 3 (it's formed by three stars with anumbers 1, 2 and 4). And the levels of the stars numbered by 2 and4 are 1. At this map there are only one star of the level 0, twostars of the level 1, one star of the level 2, and one star of thelevel 3.
You are to write a program that will count the amounts of the starsof each level on a given map.
For example, look at the map shown on the figure above. Level ofthe star number 5 is equal to 3 (it's formed by three stars with anumbers 1, 2 and 4). And the levels of the stars numbered by 2 and4 are 1. At this map there are only one star of the level 0, twostars of the level 1, one star of the level 2, and one star of thelevel 3.
You are to write a program that will count the amounts of the starsof each level on a given map.
Input
The first line of the input file contains a numberof stars N (1<=N<=15000). Thefollowing N lines describe coordinates of stars (two integers X andY per line separated by a space,0<=X,Y<=32000). There can be only onestar at one point of the plane. Stars are listed in ascending orderof Y coordinate. Stars with equal Y coordinates are listed inascending order of X coordinate.
Output
The output should contain N lines, one number perline. The first line contains amount of stars of the level 0, thesecond does amount of stars of the level 1 and so on, the last linecontains amount of stars of the level N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
题目分析:就是让您求位于没颗星星左下方的包括自己正下方的星星个数。因为他已经按Y轴排好序的输入了,所以只需要按着顺序在X轴上建立树状数组就可以了。in为树状数组,i为星星的x坐标。Out记录了level为i的星星个数。因为树状数组要求从1开始,故要对x坐标加1处理。按照已经排好序的输入顺序读入一个星星坐标并加1,即x=x+1,计算然后计算到目前为止,所有x坐标小于或等于x的星星个数sum。因为sum包含了当前星星,所以当前星星的level=sum-1,然后Out[level]++。最后输出Out数组元素。
#include<stdio.h>
int in[32001];
int Out[15001];
int N=32001;
int lowbit(int i)
{
return i&(-i);
}
void plus(int pos, int num)
{
while(pos <=N)
{
in[pos] += num;
pos += lowbit(pos);
}
}
int sum(int end)
{
int sum = 0;
while(end > 0)
{
sum += in[end];
end -= lowbit(end);
}
return sum;
}
int main()
{
int n;
int i,x,y;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
x+=1;
plus(x,1);
Out[sum(x)-1]++;
}
for(i=0;i<n;i++)
printf("%d\n",Out[i]);
return 0;
}
- 树状数组 poj 2352 Stars
- poj 2352 Stars(树状数组)
- poj 3067 Japan(树状数组)
- poj 2352 Stars 线段树
- poj 3067 japan 树状数组
- poj 3321 Apple Tree(树状数组)
- poj 2155 Matrix (二维树状数组)
- ACM: 树状数组 poj 1195
- ACM: 树状数组 poj 2155 学习《浅…
- ACM: 树状数组 poj 3321 图论+树状…
- ACM: 树状数组 poj 2029 动态规划…
- poj 2481 Cows(树状数组)
- poj 1195 Mobile phones(二维树状…
- poj 1463 Strategic game(树状DP…
- ACM: 树状DP 动态规划题 poj 1463 …
- ACM: 动态规划题 poj 2057 树状DP
- poj 2488
- POJ 3261 Milk Patterns[后缀数组]
- 树状数组
- 树状数组题目总结
- 一个常用定理表 - [武林秘籍…
- 如何避免由于编译器差别带来…
- 一个常用定理表 - [武林秘籍…
- 树状数组 poj 2352 Stars
- 完整版 线段树
- 线段树和树状数组题目总结(未完)
- 线段树 hdu3468 A Simple Problem…
- 位运算 学习
- dll传递stl注意
- 大牛写的10种排序
- 一个大牛的博客,有空进去看看
- 线段树 I Hate It