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. another app is currently holding the yum lock;waiting for it to exit解决

    有时用yum升级一些文件时,会出现以下情况:   another app is currently holding the yum lock;waiting for it to exit...   可 ...

  2. Assembly文件被锁定

    使用 Assembly.LoadFile 加载程序集后 ,被加载的文件就会被锁定,之后就不能对其执行转移.删除等操作 为了解决次问题,我们可以先读取成字节流,然后转换成Assembly.代码如下:复制 ...

  3. 兼容amd,commonjs和browser的模块写法

    从uuid.js中抽出来的写法. (function() { var _global = this; // Export public API var obj = {}; obj.attr = fun ...

  4. show slave各项参数解释

    how slave status 各个参数的解释 -- mysql 分类: mysql基础2012-08-23 11:03 2315人阅读 评论(0) 收藏 举报 服务器sslfilesqltable ...

  5. 分页控件-ASP.NET(AspNetPager)

    AspNetPager是asp.net中常用的分页控件,下载AspNetPager.dll,添加引用,在工具栏就可以看到AspNetPager控件: <div class="oa-el ...

  6. UIWebView

    本地html string文件 loadHTMLString: 本地/远程文件 loadRequest

  7. [BZOJ3670][UOJ#5][NOI2014]动物园

    [BZOJ3670][UOJ#5][NOI2014]动物园 试题描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学 ...

  8. HDU3348(贪心求硬币数

    ;} coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. make_head,,,pop_head,,,push_head,,,sort_head..

    STL中,有很多的排序函数模板供我们调用,省去我们自己编写一些排序过程的麻烦.本文是一篇关于STL中堆排序的一个介绍. 本文涉及的几个函数如下:make_heap(), push_heap(), po ...

  10. WCDMA是什么意思?CDMA是什么意思?GSM是什么意思

    有些朋友在购买3G智能手机的时候会遇到这样的困惑,为什么相同的手机会有不同手机网络制式之分呢?有的支持WCDMA/GSM,有的支持CDMA/GSM,到底自己应该选购哪一种手机好呢?WCDMA是什么意思 ...