POJ2352 Stars 树状数组

原创 2016年06月01日 16:20:31

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40834 Accepted: 17783
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.

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.
题目大意:
有一堆(些)星星,在同一平面,按以下方法分星星的级别:
xy值都不大于它的星星的个数。
给你星星的数量和位置,输出0~n-1级星星的数量。
注:输入保证按y的大小顺序排列,y相同则比x。
思路:这题因为y递增,所以后输入的只要不比先输入的x小就等级加一,顺便用树状数组优化,不然依然超时。

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int n,i,x,y,f[50000],ans[50000];
int count(int x){
    return x&(-x);
}
int sum(int x){//求前x的和
    int re=0,k=x;
    while(k>0){
        re+=f[k];
        k-=count(k);
    }
    return re;
}
void pluss(int x){//累加
    int k=x;
    while(k<=32005){
        f[k]++;
        k+=count(k);
    }
}
int main(){
    while(scanf("%d",&n)!=EOF){
        memset(f,0,sizeof(f));
        memset(ans,0,sizeof(ans));
        for(i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            x++;
            ans[sum(x)]++;
            pluss(x);
        }
        for(i=0;i<n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
版权声明:本文为博主原创文章,未经博主允许不得转载。

poj-2352-Stars【树状数组】

题目链接:点击打开链接 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...

POJ - 2352 - Stars (树状数组!!)

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34244   Accepte...

POJ2352 Stars 【树状数组】or【线段树】

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31172   Accepte...

POJ-2352-Stars-树状数组

树状数组中,每个结点管辖了不同的原数组元素的和。 令A[1...n]表示原数组,C[1...n]表示树状树组。 观察图,可知: C1 = A1 C2 = A1 + A2 C3 = A...
  • lihao21
  • lihao21
  • 2011年03月28日 22:39
  • 903

POJ 2352 Stars(简单树状数组)

什么是树状数组在这里就不说了啊,这里有解释了啊。http://blog.csdn.net/fulongxu/article/details/19701281 就是一个模版题目,直接套模版都可以过。先建...

POJ - 2352 Stars解题报告(树状数组求二维区域和)

题目大意:给你一个二维的坐标系(32000*32000),里面有n(15000)个点,告诉你每个点的坐标(各个点各不相同)。定义:(x0,y0)(x_0,y_0)的左下角区域为:{(x,y)(x,y)...

poj 2352 Stars (树状数组/线段树)

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:50142   Accepted...
  • lj130lj
  • lj130lj
  • 2017年12月02日 14:41
  • 13

POJ 2352 Stars 树状数组(入门)

/** * 树状数组(入门): * 3次TLE,一直弄不懂原因,还是看discuss,因为依题X取值是[0, 32000] 可能取0 * 所以当x等于0的时候,lowbit(0) ...

poj 2352 Stars(树状数组)

StarsDescriptionAstronomers often examine star maps where stars are represented by points on a plane...

POJ 2352 Stars (树状数组)

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32431   Accepte...
  • steveyg
  • steveyg
  • 2014年08月13日 10:00
  • 268
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ2352 Stars 树状数组
举报原因:
原因补充:

(最多只允许输入30个字)