Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3680    Accepted Submission(s): 1449

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
 
Source
 
Recommend
LL
 

树状数组:

对于这样的数据结构,有特定的函数,可用.....至于其中的具体原因,也非三言两语能见真相于天下....,推荐去看数据结构相关书籍...

树状数组开始有lowbit()-----》这个函数与数字的字节有关,.....,看书

代码模式很简单为:

     int lowbit(int x)
{
return x&(-x) //-x其实是~X+1的结果....
}

然后就是一个add()
代码模式为:

    void  add(int pos , int d )
{
while(pos<maxn) //为po【】数组的边界
{
po[pos]+=d; //d为所要加的数值
pos+=lowbit(pos); //pos 为数组指针....
}
}

最后为求和:

sum();

代码模式:

    int  sum(int  x)
{
int ans ;
while(x>)
{
ans+=po[x];
x-=lowbit(x);
}
}

树状数组的最普遍的用途是用来快速求解一条线段a___b的和.....

所以面对问题,就要想尽办法将他转化到这种模式上来...这样就可以用这种方法来求解了啊...

代码如下::

 /*作者 : 龚细军,树状数组*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 35000
#include<cstdlib>
using namespace std; int po[maxn+],leve[maxn+]; int lowbit(int x)
{
return x&(-x);
} void add(int x)
{
while(x<maxn)
{
po[x]++;
x+=lowbit(x);
}
} int sum(int pos)
{
int ans=;
while(pos>)
{
ans+=po[pos];
pos-=lowbit(pos);
}
return ans;
} int main()
{
int n,y,i,x;
while(cin>>n)
{
memset(po,,sizeof(po));
memset(leve,,sizeof(int)*n);
for(i=;i<=n;i++)
{
/*y轴是升序的...所以不要予以考虑....*/
scanf("%d %d",&x,&y);
x++; /*右移一位,搓掉o,那样的话,就要经历无数次的lowbit(),调用,会超时*/
leve[sum(x)]++; /*放大避免x,轴有相同的数字*/
add(x);
}
for(i=;i<n;i++)
{
printf("%d\n",leve[i]);
}
}
return ;
}

HDUOJ-----1541 Stars的更多相关文章

  1. hdu 1541 Stars

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 思路:要求求出不同等级的星星的个数,开始怎么也想不到用树状数组,看完某些大神的博客之后才用树状数 ...

  2. POJ 2352 Stars(HDU 1541 Stars)

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41521   Accepted: 18100 Descripti ...

  3. hdoj 1541 Stars【线段树单点更新+最大值维护】

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. HDU 1541 Stars (树状数组)

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

  5. POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

    一開始想,总感觉是DP,但是最后什么都没想到.还暴力的交了一发. 然后開始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~ ...

  6. HDU 1541 Stars (线段树)

     Problem Description Astronomers often examine star maps where stars are represented by points on ...

  7. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

  8. hdu 1541 Stars 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有 ...

  9. 题解报告:hdu 1541 Stars(经典BIT)

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

  10. hdu 1541 Stars 统计<=x的数有几个

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

随机推荐

  1. cout的输出格式初探3

    #include <iostream> #include <iomanip> using namespace std; int main() { double f=2.0/3. ...

  2. Gson 使用总结 高级用法

    Gson基本用法 参考:http://www.jianshu.com/p/e740196225a4 Gson提供了fromJson() 和toJson() 两个直接用于解析和生成的方法,前者实现反序列 ...

  3. Android常用http请求框架 简介及现状

    JDK支持的HttpUrlConnection HttpUrlConnection是JDK里提供的联网API,是最原始最基本的API,大多数开源的联网框架基本上也是基于此进行的封装的.HttpUrlC ...

  4. 如何解决 SQL Server 中的锁升级所致的阻塞问题

    概要 锁升级为表锁插入转换很多细粒度的锁 (如行或页锁) 的过程.Microsoft SQL Server 动态确定何时执行锁升级.作出决定之前,SQL Server 将特定的扫描,整个事务,并且用于 ...

  5. ASP.NET WebServices 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。

    今天在使用Asp.net做一个WebServices时通过Get方式调用方法时却出现了以下错误(仅在Get请求下,POST下正常)   在网络和MSDN上了解到WebServices默认只启用 Htt ...

  6. Cognos第三方用户认证(CustomJavaProvider)

    关于Cognos第三方用户认证(CustomJavaProvider)的demo网上的例子很多,当然最权威的你可以从Cognos安装的SDK中去探索,本文不详细的说明代码,主要说一下认证的处理过程,以 ...

  7. [Algorithm] Tree: Lowest Common Ancestor

    By given a tree structure, task is to find lowest common ancestor: For example, LCA(4, 5) --> > ...

  8. 【leetcode 桶排序】Maximum Gap

    1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...

  9. iOS判断日期A是否在日期B到日期C之间

    方法一: 可以用nsdate 的 timeIntervalSince1970 方法把时间转换成时间戳进行比较,这里timeIntervalSince1970返回的是NSTimeInterval(dou ...

  10. Chrome 对于 glyphicon 字体图标不显示的解决的方法

    在将Chome默认字体渲染为微软雅黑后,部分字体图标显示为方框,这里Chome扩展文档提供的解决的方法为: 找到  custom.css 文件,路径为: C:\Users\(username)\App ...