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 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   |   We have carefully selected several similar problems for you:  1166 1394 3450 1542 1255 

 
  树状数组,经典题
  这道题里,树状数组的作用是记录到某x坐标的所有星星的数目,这个数目就是该星星的等级,然后把level[]数组对应的等级计数+1。最后输出level数组即可。
  树状数组模板
int lowbit(int x)
{
return x & -x;
}
int sum(int a[],int x) //求出第x个元素之前的和
{
int ans = ;
while(x>){
ans+=a[x];
x -= lowbit(x); //向左上爬
}
return ans;
}
void add(int a[],int x,int d,int n) //将编号为x的数加d
{
while(x<=n){
a[x]+=d;
x+=lowbit(x);
}
}

  代码:

 #include <stdio.h>
#include <iostream>
using namespace std;
int lowbit(int x)
{
return x & -x;
}
int sum(int a[],int x) //求出第x个元素之前的和
{
int ans = ;
while(x>){
ans+=a[x];
x -= lowbit(x); //向左上爬
}
return ans;
}
void add(int a[],int x,int d,int n) //将编号为x的数加d
{
while(x<=n){
a[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF){
int level[]={};
int x[]={};
for(i=;i<=n;i++){ //输入,计算当前星星前面的总星数(level),对应等级值+1,更新x[]数组
int a,b,num=;
scanf("%d%d",&a,&b);
level[sum(x,a+)]++;
add(x,a+,,); //这个位置的星星数+1
}
for(i=;i<n;i++) //输出
printf("%d\n",level[i]);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1541/poj 2352:Stars(树状数组,经典题)的更多相关文章

  1. POJ 2352 【树状数组】

    题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...

  2. POJ 2481Cows(树状数组 + 好题)

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15222   Accepted: 5070 Description ...

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. HDU 1166 敌兵布阵(线段树/树状数组模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. HDU 1166 敌兵布阵 树状数组小结(更新)

    树状数组(Binary Indexed Tree(BIT), Fenwick Tree) 是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有 元素之和,但是每次只能修改一 ...

  7. st表树状数组入门题单

    预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...

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

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

  9. Stars(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others)    Memor ...

随机推荐

  1. Initialization of deep networks

    Initialization of deep networks 24 Feb 2015Gustav Larsson As we all know, the solution to a non-conv ...

  2. dedecms升级后报错

    DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...

  3. Integer Inquiry

    Integer Inquiry Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Sub ...

  4. 磁盘 I/O 性能监控指标和调优方法

    在介绍磁盘 I/O 监控命令前,我们需要了解磁盘 I/O 性能监控的指标,以及每个指标的所揭示的磁盘某方面的性能.磁盘 I/O 性能监控的指标主要包括:指标 1:每秒 I/O 数(IOPS 或 tps ...

  5. 教你安装漂亮的Arc GTK主题

    导读 近日,我们又发现了一款深受 Linux 用户喜爱的桌面主题 — Arc GTK,Arc GTK 主题已被很多 GNU/Linux 操作系统支持和采用,其中就包括即将到来的 Linux Mint ...

  6. Unity3d 经验小结

      Unity3d 经验小结 文本教程 你是第2541个围观者 0条评论 供稿者:Jamesgary 标签:unity3d教程 Fbx.贴图导入Unity时的注意事项: 在导出Fbx之前,Maya中已 ...

  7. 添加删除一个controller

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) rails generate controller Users rails destroy cont ...

  8. [codeforces 549]G. Happy Line

    [codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...

  9. OpenGL Vertex Array

    转载 http://blog.csdn.net/dreamcs/article/details/7699603

  10. C++模板常用使用方法介绍

    转载:http://developer.51cto.com/art/201002/182202.htm C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率.我们在这篇文章中为大家详细讲解一 ...