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

Stars

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

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
 
题解:这个题,一定要注意读题,首先,它给出的是已经按y和x 排好序的输入,所以要是采用输入一个点就处理一个点的话,肯定不可能在处理完后再输入当前点左下角的点,所以直接输入后直接统计其左下角的点的个数即可,所以输入一个处理一个即可,然后用这个点更新后面的每个点的level+1即可,而且这样就可以不用考虑y了,直接建立一维的数组x就可以了
特别注意的是:树状数组不能处理标号为0的点,所以讲所有的点的标号+1即可
所以以后见到树状数组编号从0 开始的情况都将标号依次+1 即可
下面是代码
 #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring> using namespace std; int n;
const int N=;
const int M=; int a[M],c[N]; int lowbit(int t)
{
return t&(-t);
} void insert(int t,int d)
{
while (t<=M)
{
a[t]+=d;
t+=lowbit(t);
}
} int getsum(int t)
{
int zs=;
while (t>)
{
zs+=a[t];
t-=lowbit(t);
}
return zs;
} int main()
{
int i,x,y,p,n;
while (scanf("%d",&n)!=EOF)
{
memset(a,,sizeof(a));
memset(c,,sizeof(c));
for (i=;i<=n;i++) {
scanf("%d%d",&x,&y);
insert(x+,);
p=getsum(x+)-;
c[p]++;
}
for (i=;i<n;i++) printf("%d\n",c[i]);
}
return ;
}

Stars(树状数组)的更多相关文章

  1. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  2. Stars(树状数组或线段树)

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...

  3. Stars(树状数组单点更新)

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  4. HDU-1541 Stars 树状数组

    题目链接:https://cn.vjudge.net/problem/HDU-1541 题意 天上有许多星星 现给天空一个平面坐标轴,统计每个星星的level, level是指某一颗星星的左下角(x& ...

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

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

  6. hdu1541 Stars 树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意就是统计其左上位置的星星的个数 由于y已经按升序排列,因此只用按照x坐标生成一维树状数组 ...

  7. Stars(树状数组+线段树)

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

  8. POJ2352 Stars 树状数组

    emm,ssy说可以直接CDQ分治...%%%但是注意到y是有序的,所以可以直接求一下前缀和就行了. 题干: Astronomers often examine star maps where sta ...

  9. POJ2352 Stars [树状数组模板]

    题意:输入一n颗星星的x,y坐标,给定判断level的标准,即某颗星星左下边(不高于它,不超过他,相当于以他为基准的第三象限)星星的数目为level, 输出level从0到n的星星个数. //poj2 ...

随机推荐

  1. Java I/O---类体系总结

    1.Java I/O常用 (1)File 对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹. (2)FileInputStream 从文件系统中的某个文件中获得输入字节: ...

  2. scalajs_初体验

    scalajs是将scala编译成js的编译器,目的在于使用scala的众多类库和强类型特征构建出稳定可扩展的js应用. build.sbt构建文件如下: enablePlugins(ScalaJSP ...

  3. Xamarin android CardView的使用详解

    android 5.0新增加的一个控件CardView,在support v7兼容包中,意思就是卡片View,虽然可以设置阴影,圆角等等样式,但是我们也可以自己写出来,谷歌工程师之所以出这个,肯定是帮 ...

  4. Python安装和开发环境搭建

    1.官网:http://www.python.org/download/下载安装包,目前最新版本为3.6,安装包很多地方可以下,也可以在360软件管家上下载安装  特别要注意勾选:Add Python ...

  5. Webpack 2 视频教程 013 - 自动分离 CSS 到独立文件

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  6. popupwindow那些坑

    1. new PopupWindow(vw, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 如果 ...

  7. dubbo源码—service export

    在应用编写好服务并进行之后,dubbo负责将服务export出去,dubbo export服务的时候主要做了以下几件事: 将服务export到本地(根据scope的配置) 创建Invoker(启动本地 ...

  8. Java实现2048小游戏【代码】

    元旦刚过,祝大家新年快乐呀! 感觉2017实在是过得太快了... 正如之前所说,这个游戏最开始的版本其实在去年5月份就写好了,其实当时就已经实现了主要功能,后来经历了几次更新,加入了Undo功能,加入 ...

  9. ts中interface与class的区别

    interface -- 接口只声明成员方法,不做实现. class -- 类声明并实现方法. 那么接口有什么用呢?设想如下需求: 要实现一个print函数,它将传入的对象打印出来.在实际实现上,它将 ...

  10. 关于 for 循环与 循环嵌套

    FOR循环精讲 > 1.初步结识 for是写出题的重要组成部分之一,每个题如果没有for循环根本是无法做出来的,可见for循环在c++语言中是有多么重要,那么for的格式是怎样的呢?? for( ...