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. hdu 2018 母牛的故事

    #include<stdio.h> int main(void) { int i,n,j,k; long long narr[60]; narr[1]=1; narr[2]=2; narr ...

  2. zabbix 汉化

    zabbix2.x的版本自带汉化,3.x的版本也可以通过修改配置文件强制使用自带的汉化,但是不管哪种,翻译的精准度令人费解:偶然发现一个专门翻译zabbix的网站https://www.zabbix. ...

  3. C语言运算符优先级和口诀 (转)

    一共有十五个优先级: 1   ()  []  .  -> 2   !  ~   -(负号) ++  --   &(取变量地址)*   (type)(强制类型)    sizeof 3   ...

  4. 昂贵的聘礼(dijkstra)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38549   Accepted: 11158 Descripti ...

  5. Third scrum meeting - 2015/10/28

    在一天的工作中明显发现到,无法和网站开发团队进行交流会严重导致我们的进程拖延,所以我们在现有的情况下也把大家的goal初步完成了,我们也对代码规范进行了详细的讨论,以及UI的设计完成,所以整个团队都真 ...

  6. 基于 MeanShift 算法的目标跟踪问题研究

    参考:http://www.cnblogs.com/tornadomeet/archive/2012/03/15/2398769.html MeanShift 算法作为一种基于特征的跟踪方法,基本思想 ...

  7. 2014.7建兰NOIP模拟Day1 Running

    突然间翻到着题,想想那时的我真是垃圾,这么简单的tarjan缩点+树上倍增都不会..还想了3h+.. 什么时候写了它吧...

  8. 每天一个脚本解析day1==》《service xxxxx status》之service脚本解析

    vim    /sbin/service #!/bin/sh . /etc/init.d/functions #读取环境变量. VERSION="$(basename $0) ver. 0. ...

  9. SSM框架Web程序的流程(Spring SpringMVC Mybatis)

    SSM框架的Web程序主要用到了三个技术: Spring:用到了注解和自动装配,就是Spring的两个精髓IOC(反向控制)和 AOP(面向切面编程). SpringMVC:用到了MVC模型,将逻辑代 ...

  10. Power of Two & Power of Three & Power of Four

     Check Power of 2 Using O(1) time to check whether an integer n is a power of 2. Example For n=4, re ...