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 Submission(s): 1632

Problem 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

题目:

HDOJ:    http://acm.hdu.edu.cn/showproblem.php?pid=1541

POJ:        http://poj.org/problem?id=2352

又是一道树状数组,要注意题目求的是 每个等级星星的个数。

并不是第i个星星的等级。

等级判别:只要该星星左下方(包括正下方和正左方,不包括自己)有几个星星,等级就是几。

用树状数组的原理,三个函数一上,就出来了。

因为坐标是从0开始,所以相应的坐标要+1操作,否则会崩溃的。

/*******************************************
********************************************
*           Author:Tree                    *
*    From :  blog.csdn.net/lttree          *
*      Title : Stars                       *
*    Source: hdu 1541                      *
*      Hint :  树状数组                    *
********************************************
********************************************/

#include <stdio.h>
#include <string.h>
#define RANGE 32005

// star存等级数,c存星星
int c[RANGE],star[15001];

int lowbit( int x )
{
    return x&(-x);
}
void add( int i )
{
    while( i<=RANGE )
    {
        ++c[i];
        i+=lowbit(i);
    }
}
int sum( int i )
{
    int sum=0;
    while( i>0 )
    {
        sum+=c[i];
        i-=lowbit(i);
    }
    return sum;
}
int main()
{
    int n,i,x,y;
    while( ~scanf("%d",&n) )
    {
        memset(star,0,sizeof(star));
        memset(c,0,sizeof(c));

        for ( i=0;i<n;i++ )
        {
            scanf("%d%d",&x,&y);
            // 坐标是从0开始的,所以x+1
            star[sum(x+1)]++;
            // 后执行add函数,否则会把当前的星星算进去
            add(x+1);
        }
        for( i=0;i<n;i++ )
            printf("%d\n",star[i]);
    }
    return 0;
}

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

时间: 2024-10-11 11:28:56

ACM-树状数组之Stars——hdu1541,poj2352的相关文章

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

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

树状数组 - 2352 Stars

题目地址: http://poj.org/problem?id=2352 分析: - 题意分析:  有n个星星, 它的左下方(x和y不超过它)的星星的数目就是它的level, 分别计算level 为 0 到 n-1 的星星的数目. 输入是先按照 y 从小到大排序, 如果y相同,就按照x递增排序, 不会有2个以上星星占同一个坐标. - 数据结构 - 用原始数据数组 a 表示同一个x坐标的星星数目. (a[i]为x坐标为 i-1的星星数目).  注意输入数据时排序的. - 树状数组s维护a的信息.

【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

树状数组小模板......

(1)"改点求段"型 树状数组模板题-hdu1166+poj2352 - Delacour_的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/delacour_/article/details/33364033 树状数组区间求和三种模型 - mr_lee - 博客频道 - CSDN.NET http://blog.csdn.net/q573290534/article/details/6664454 int lowbit(int a) { retur

树状数组详解(图形学算法)

目录 一.从图形学算法说起 1.Median Filter 概述 2.r pixel-Median Filter 算法 3.一维模型 4.数据结构的设计 5.树状数组华丽登场 二.细说树状数组 1.树 or 数组? 2.结点的含义 3.求和操作 4.更新操作 5.lowbit函数O(1)实现 6.小结 三.树状数组的经典模型 1.PUIQ模型 2.IUPQ模型 3.逆序模型 4.二分模型 5.再说Median Filter 6.多维树状数组模型 四.树状数组题集整理 一.从图形学算法说起 1.M

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

HDU1541 Stars(树状数组)

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