POJ2352 Stars

B - Stars

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
2352

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.

/*
Author: 2486
Memory: 604 KB		Time: 391 MS
Language: G++		Result: Accepted
Public:		No
*/
//进行横坐标标注
//题目数据的输入已经确定,是先按照Y坐标升序,当y相等时,按照X坐标升序
//如此,在现在输入的数的X坐标之前已经是满足条件的数即只要是属于此时输入的数的左下角的所有星星个数之和就是他的等级
//通过树状数组可以快速的求解某个区间之前的所有数的和。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=32000+5;
int A[maxn],B[maxn],n,x,y;
int lowbit(int x) {
    return x&(-x);
}
int sum(int x) {
    int cnt=0;
    while(x>0) {
        cnt+=A[x];
        x-=lowbit(x);
    }
    return cnt;
}
void add(int x,int d) {
    while(x<maxn) {
        A[x]+=d;
        x+=lowbit(x);
    }
}
int main() {
    while(~scanf("%d",&n)) {
        memset(B,0,sizeof(B));
        memset(A,0,sizeof(A));
        for(int i=0; i<n; i++) {
            scanf("%d%d",&x,&y);
            B[sum(++x)]++;
            add(x,1);
        }
        for(int i=0; i<n; i++)printf("%d\n",B[i]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 21:00:15

POJ2352 Stars的相关文章

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

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

POJ2352 Stars 树状数组

POJ2352 非常裸的树状数组的题. 注意数组下标不能从0开始 因为lowbit(0)==0 所以 所有横坐标统一加1 数组要开的够大 就酱 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn=15000,

poj--2352 Stars(树状数组)

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. As

POJ2352 Stars (静态二叉检索树)

https://vjudge.net/problem/POJ-2352 分析: 由于是按照y坐标的升序,y坐标向等的按x的升序的顺序给出星星.那么某个星星的等级数就是在他前面x坐标小于等于他的x坐标的星星的个数. 暴力的时间复杂度为n^2,超时 所以我们要记录前面所有x坐标出现的次数.然后要求出[0,xi]出现的和. 这就是静态二叉检索树 的标准问题了. #include <iostream> #include <cstdio> #include <cstring> #

POJ 2481 树状数组 区间覆盖(POJ2352 Stars 的变形题)(线段化点)

0)学会将题目情景转化为自己熟悉的结构或模型. 题目大意: 每个奶牛有自己的一个区间,求每个奶牛的区间所覆盖的子区间个数(注意,真子集,相等的不算),按照输入的顺序输出. 转化: 要学会将题目情景转化为自己熟悉的模型或结构上.把每个区间的左端x值作为点的x坐标,右端x值作为点的y坐标,就可以把所有区间转化为一个二维坐标图上的点集,而此时每个点左上方的点(同Stars那道题目一样不包括自身)的个数,就是每个区间所覆盖的子区间的个数(对应题目要求,这里或许可以再变形). 同POJ2481 Stars

[POJ2352] Stars(树状数组)

传送门 先按照下标x排序,然后依次把y加入树状数组,边加入边统计即可. 注意下标re从零开始,需+1s ——代码 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <string> 5 # include <cmath> 6 # include <vector> 7 # include <map> 8 # includ

树状数组总结——转

转自:夏天的风 http://blog.csdn.net/shahdza/article/details/6314818#comments 又做了几道树状数组的题,决定放一块儿总结一下:恩,总结一下.. (ps:大牛可以直接跳过...) 这得从一张图说起: 树状数组中用的d[],每个点都有一定的管辖范围: 如d[1]=a[1]; d[2]=a[1]+a[2]; d[3]=a[3]; d[4]=a[1]+a[2]+a[3]+a[4]; 等等: 这样的结构关键是为了,对一个数组内部动态的删除,增加,

ACM-树状数组之Stars——hdu1541,poj2352

***************************************转载请注明出处:http://blog.csdn.net/lttree*************************************** Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4129    Accepted Submissi

【POJ2352】【树状数组】Stars

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. As