ZOJ 2352 Stars

Stars


Time Limit: 1000MS


Memory Limit: 65536K


Total Submissions: 22870


Accepted: 9967

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.

第一个树状数组题目,感觉也能理解了 ,里面确实好多细节的东西,容易导致的误区是,树状结构是一个特定的结构,而不是动态构造的。

#include <iostream>
#include<string.h>
using namespace std;
const int M=43000;
int cnt[M];
int total[M];

int lowbit(int x)
{
    return x&(-x);
}

int sum(int x)
{
    int s=0;
    while(x>0)
    {
        s+=cnt[x];
        x-=lowbit(x);
    }
    return s;
}

void add(int x,int val)
{
    while(x<=M)
    {
        cnt[x]+=val;
        x+=lowbit(x);
    }
}

int main()
{
    int N,x,y;
    cin>>N;
    for(int i=0;i<N;i++)
    {
        cin>>x>>y;
        add(x+1,1);
        total[sum(x+1)]++;
    }
    for(int j=1;j<=N;j++)
        cout<<total[j]<<endl;
    return 0;
}
时间: 2024-08-04 13:36:23

ZOJ 2352 Stars的相关文章

poj 2352 Stars 数星星 详解

题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时则按照横坐标从小到大输出. (0 <= x, y <= 32000) 要求输出等级0到n-1之间各等级的星星个数. 分析: 这道题不难想到n平方的算法,即从纵坐标最小的开始搜,每次找它前面横坐标的值比它小的点的个数,两个for循环搞定,但是会超时. 所以需要用一些数据结构去优化,主要是优化找 横坐

hdu 1541/poj 2352:Stars(树状数组,经典题)

Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4052    Accepted Submission(s): 1592 Problem Description Astronomers often examine star maps where stars are represented by points on a plan

求左下角星星之和 树状数组或线段树 poj 2352 Stars

http://poj.org/problem?id=2352 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37696   Accepted: 16419 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coo

POJ 2481 Cows &amp;&amp; POJ 2352 Stars(树状数组妙用)

题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 "小" 的有多少点. Cows需要自己排序, Stars题目已经给排好序. POJ 2352 Stars 题目大意为在二维坐标上给出一些星星的坐标,求某一个星星左方,下方,左下方的星星个数.题目已经把星星按照Y坐标从小到大,X从小到大排序.因此,在每次对一个星星进行统计时,之前出现过的星星,只要X

poj 2352 Stars (树状数组)

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37108   Accepted: 16173 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 st

POJ 2352 Stars(树状数组)

                                                                 Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44309   Accepted: 19236 Description Astronomers often examine star maps where stars are represented by points on a pla

POJ - 2352 - Stars

题目链接:POJ - 2352 题目大意: 给你N个星星的坐标,问每个星星的左下角有几颗星星. 注意输入坐标y是按递增输入的. 题目分析: 算是树状数组的一个基础题目吧,有些题目也是以这道题为基础的. 直接运用数组数组即可,具体看代码. 不过注意由于x坐标有可能是0,所以要对所有的X坐标加一(不要忘了区间上界也要增大一个), 防止add()函数出现死循环超时. 给出代码: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cs

POJ 2352 Stars(树状数组 or 线段树)

链接: http://poj.org/problem?id=2352 题目大意: 在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y0),x0<=x 且y0<=y.如果左下位置有a个星星,就表示这个星星属于level x 按照y递增,如果y相同则x递增的顺序给出n个星星,求出所有level水平的数量. 思路: 由于输入的顺序,对于第i颗星星,它的等级是之前输入的星星中,横坐标x小于等于i星横坐标的那些星星的总数量(前面的y一定比后面的y小). 所以是查询+更新操作

POJ 2352 Stars(线段树)

题目地址:POJ 2352 今天的周赛被虐了. . TAT..线段树太渣了..得好好补补了(尽管是从昨天才開始学的..不能算补...) 这题还是非常easy的..维护信息是每个横坐标的出现的次数. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h>