Poj 2352 Stars(树状数组)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdz20172133/article/details/84112807

Stars

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 55490

 

Accepted: 23840

Description

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. 

http://poj.org/images/2352_1.jpg


For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. 

You are to write a program that will count the amounts of the stars of each level on a given map.

Input

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate. 

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains 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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

Source

Ural Collegiate Programming Contest 1999

 

算法分析

 

题意:

 

给你星星的坐标(y递增,若y相等,x递增),每个星星都有一个等级,规定它的等级就是在它左下方的星星的个数。输入所有星星后,依次输出等级为0到n-1的星星的个数。

 

思路:

 

由于题目中的Y轴坐标已经排好序了,所以如果要找比当前i点的x坐标y坐标小(或等)的点坐标,必然是在这个点的前面去找,所以可以用X轴坐标建立一维树状数组。就是统计x前面比它小的星星的个数,符合树状数组最基本的应用。

 

注意的是:树状数组下标为0的位置不可用,所以我们需要在输入x坐标时+1.

#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;

const int MAXN=32000+5;//最大元素个数
 
int n;//元素个数
int num[MAXN],x,y,c[MAXN];
 
//返回i的二进制最右边1的值
int lowbit(int i)
{
    return i&(-i);
}
 
//返回A[1]+...A[i]的和
int sum(int i)
{
    int res=0;
    while(i>0)
    {
        res += c[i];
        i -= lowbit(i);
    }
    return res;
}
 
//令A[i] += val
void add(int i,int val)
{
    while(i<=MAXN)
    {
        c[i] += val;
        i += lowbit(i);
    }
}
int main()
{
	
    
    while(scanf("%d",&n)!=EOF)
    {   
        memset(num,0,sizeof(num));
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)  
		{
		  scanf("%d%d",&x,&y); 
		  x++;            //树状数组下标不能为0
		  num[sum(x)]++; //先统计
		  add(x,1);
		  
        } 
         for(int i=1;i<=n;i++)  
		{
		  printf("%d\n",num[i-1]);
        } 
      
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/84112807