树状数组的进阶运用(Stars 数星星)

英文原题

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 }

时间: 2024-10-07 05:02:01

树状数组的进阶运用(Stars 数星星)的相关文章

树状数组+逆序数与顺序数——HDU 2492

对应HDU题目:点击打开链接 Ping pong Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description N(3N20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skil

BZOJ1878 SDOI2009 HH的项链 树状数组

题意:给定一个颜色序列,每组询问给出区间[l,r],求[l,r]中不同颜色的数量 题解: 首先把所有颜色离散化,然后离线,将询问按右区间升序排列.从1-N把整个序列扫一遍,设Pos[i]为第i个颜色最后出现的位置,假定当前扫到的位置为i,则更新Pos[a[i]],那么问题变成了:求一个序列(Pos)中,大于等于一个数(L)的数的数量. 用树状数组维护Pos=j的数的数量,每次查询树状数组中L-N的和即可. 貌似SDOI不喜欢考大型数据结构啊……坐等今年打脸 #include <cstdio>

HDU 1394 树状数组+离散化求逆序数

对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们通过树状数组可以明显提高时间效率. 我们可以按照排列的顺序依次将数字放入树状数组中,并依次更新预与之相关联的树状数组元素.那么在将其更新完毕后,我们知道每个数对应的树状数组元素的左边的数肯定比它小,我们在以序列顺序依次更新树状数组时,如果有值在它前面出现,那么它对应的树状数组元素(在这个题目里存放的

【bzoj2789】[Poi2012]Letters 树状数组求逆序对

题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n (2<=n<=1,000,000),表示字符串的长度. 第二行和第三行各一个长度为n的字符串,并且只包含大写英文字母. 输出 一个非负整数,表示最少的交换次数. 样例输入 3 ABC BCA 样例输出 2 题解 树状数组求逆序对 一个结论:将序列A通过交换相邻元素变换为序列B,需要的最小次数为A中

hdu 5869 区间不同GCD个数(树状数组)

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 58 Problem Description This is a simple problem. The teacher gives Bob a list of probl

Codeforces Round #365 (Div. 2) D 树状数组+离线处理

D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her wit

HDU 1541 Stars (树状数组)

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

hdu 1541/poj 2352:Stars(树状数组,经典题)

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 plan

Stars(树状数组单点更新)

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 wa