树状数组 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. 
树状数组 <wbr>poj <wbr>2352 <wbr>Stars

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

1 1 
5 1 
7 1 
3 3 
5 5

Sample Output

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