英文原题

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
翻译题意

天文学家把天上的每颗星星都画在一个平面上,并给定每颗的坐标。同时把星星左下方(包括横坐标或者纵坐标一样的)的星星数目定义为该星星的等级。天文学家想知道星星等级的分布。
例如上图中,5号星星的等级为3(左下方的星星为1,2,4),2号与4号星星等级为1。上图中等级为0的星星有1颗,等级为1的星星有2颗,等级为2的星星有1颗,等级为3的星星有1颗。
请写一个程序计算屏幕上每一个等级的星星数。

仔细审题之后,发现是树状数组,因为输入的xy的值是有序的,所以只需要建立c[x]表示横坐标小于等于x的符合要求的点有几个,运用到树状数组的基础知识,统计出即可。

下面给出代码

 1 //树状数组基本框架的搭建(维护和查询都是O(lgn)的复杂度)
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<string>
 7 using namespace std;
 8 int n,a,b;
 9 int c[40000],ans[40000];//注意数组的大小
10
11 int lowbit(int k)
12 {
13      return k&(-k);
14
15 }
16 int add(int k,int num)
17 {
18     while(k<=32010)//此处的32010应为n的最大值+2
19     {
20          c[k]+=num;
21          k+=lowbit(k);
22     }
23 }
24 int sum(int k)
25 {
26     int Sum=0;
27     while(k>0)
28     {
29         Sum+=c[k];
30         k-=lowbit(k);
31     }
32     return Sum;
33 }
34
35
36 int main()
37 {
38      cin>>n;
39      for(int i=1;i<=n;i++)
40      {
41         cin>>a>>b;
42         a=a+1;//树状数组的下标需要从1开始,而数据从0开始,故此+1;
43         ans[sum(a)]++;
44         add(a,1);
45      }
46      for(int i=0;i<n;i++)
47        cout<<ans[i]<<endl;
48     return 0;
49 }

树状数组的进阶运用(Stars 数星星)相关推荐

  1. ZOJ - 4117 BaoBao Loves Reading(树状数组求区间内不同数的个数+思维)

    题目链接:点击查看 题目大意:给出一个长度为 n 的序列,其意义为第 i 秒需要看第 a[ i ] 种书,书架上可以供应无限种书,但是书桌有容量,当书桌上的容量达到上限后,如果还想从书架上拿新书来看, ...

  2. 树状数组再进阶(区间修改+区间查询)

    今天,我们再在树状数组上进一步突破,我们来讲一讲区间修改和区间查询.同样的,这需要各位对树状数组的基本知识有所了解,大家可以看看我的另一篇文章:树状数组趣解. 下面进入正题. 同样的,我还是先给代码, ...

  3. 牛客练习赛52.Galahad(树状数组维护区间不相同数的和)

    链接:https://ac.nowcoder.com/acm/contest/1084/B 来源:牛客网 Galahad 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K ...

  4. 第五讲 树状数组与线段树 【未完结】

    目录 1264. 动态求连续区间和 [树状数组板子题] 1265. 数星星 [树状数组变种] 1270. 数列区间最大值 [线段树 / 区间内求最大值] 1215. 小朋友排队 [树状数组] AcWi ...

  5. 数据结构一【树状数组】普通、二维、离线树状数组的(单点修改,单点查询,区间修改,区间查询)模板及应用例题总结

    文章目录 树状数组 lowbit 线段树与树状数组 单点修改 区间查询 区间修改 区间求和 二维树状数组 离线树状数组 例题 POJ:stars MooFest [SDOI2009]HH的项链 Tur ...

  6. jzoj3512-游戏节目【树状数组,双向dfs】

    正题 大意 有n个节目,每个节目对3个东西贡献不同,要求选择至少k个让第一个东西的值最大.求方案数 解题思路 至少k个我们可以计算选择任何个数的结果减去选择k个的结果.由于k比较小,我们考虑直接暴搜 ...

  7. 【算法】树状数组 P1908 逆序对

    P1908 逆序对 #include<iostream> #include<cstdio> #include<algorithm> #define ll long ...

  8. 小沙的remark(朴素DP、优化DP+树状数组OR线段树)

    题目 原题链接 问题描述 给定n(1≤n≤2∗106)n(1\leq n\leq2∗10^6)n(1≤n≤2∗106)和seedseedseed,将基于seedseedseed生成两个长度为nnn的序 ...

  9. [树状数组]数星星 Stars

    数 星 星 S t a r s 数星星 Stars 数星星Stars 题目描述 天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标.如果一个星星的左下方(包含正左和正下)有 k k k 颗星星 ...

最新文章

  1. 2015年我国互联网行业概况及现状分析
  2. linux更改甜器名称,Linux添加swap分区
  3. 给谷歌浏览器安装vue调试工具:vue-devtools
  4. 机器学习笔记(七):神经网络:表示
  5. 如何在下一个网页设计项目中使用Google字体
  6. mysql驱动rpm和jar_mysql5.7.11对应的JDBC驱动是哪个版本
  7. docker for mac的JSON配置文件中的hosts项修改后无法生效
  8. 机器学习7-主成分分析
  9. 2019-05-22 Java学习日记 day12
  10. Hadoop组件之Yarn
  11. hi3559AV100上交叉编译faiss(facebook research)
  12. 禁用 device/credential guard_iOS 13.3.1 Beta版中引入了禁用U1超宽带芯片的开关
  13. [第四篇] PostGIS:“我让PG更完美”
  14. QFD质量机能展开,了解一下呀!
  15. LeetCode简单题643.子数组的最大平均数I
  16. 服务号模板消息群发二代服务器,服务号模板消息群发
  17. DIJ(单源次短路) - Two Paths - HDU 6181
  18. Ubuntu14.04虚拟机下基本操作(typical安装)
  19. [篇五章三]-关于 Windows 10 安装好后系统自带的微软输入法没有输入框的 BUG 解决办法
  20. VideoPlayer怎么判断视频结束

热门文章

  1. 通用方法 关闭Outlook最小化非直接退出
  2. 度量空间,赋范空间,內积空间,希尔伯特空间
  3. 【mysql】You must reset your password using ALTER USER statement before executing this statement报错处理
  4. 报错:The media could not be loaded, either because the server or network failed or ...
  5. 《认知天性》这本书对我的启发,以及我在日常中的应用
  6. matlab的多线程操作
  7. Blender 导出obj到 OpenGL
  8. 韩国三星android多少钱,韩国首款安卓翻盖 三星GALAXY GOLDEN赏
  9. 怎么开传奇sf?传奇开服交流分享。
  10. 07-15 shell命令 man ps linux各个文件夹的含义