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

【题意】给出n个星星的位置(y坐标是升序),求各阶级星星的个数,(阶级是该星星左下角星星的个数)

【思路】树状数组直接套就可以求出当前星星的level,用一个数组存储各阶级的星星数就可以了

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=+;
int n;
int sum[N*];
int level[N*];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int v)
{
while(x<=)
{
sum[x]+=v;
x+=lowbit(x);
}
}
int query(int x)
{
int res=;
while(x)
{
res+=sum[x];
x-=lowbit(x);
}
return res;
}
int main()
{
while(~scanf("%d",&n))
{
memset(level,,sizeof(level));
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
level[query(++x)]++;
update(x,);
}
for(int i=;i<n;i++)
printf("%d\n",level[i]);
}
return ;
}

Stars_树状数组的更多相关文章

  1. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  2. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  3. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  5. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  6. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  7. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  9. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. RMAN 前期准备工作和实例

    理解恢复目录,RMAN可以在没有恢复目录(NOCATALOG)下运行,这个时候备份信息保存在控制文件.保存在控制文件的备份信息是很危险的,如果控制文件的破坏将导致备份信息的丢失与恢复的失败,而且,没有 ...

  2. DataTable数据导出CSV文件

    public static void SaveAsExcel(DataTable dt1) { //System.Windows.Forms.SaveFileDialog sfd = new Syst ...

  3. 立体角的单位——立体弧度(sr)

    国际单位制(SI)中,关于物理量 发光强度 的介绍: 1cd(坎德拉)为一光源在给定方向的发光强度,该光源发出频率为540×1012Hz(赫兹)的单色辐射,且在此方向上的辐射强度为 1/683 W/s ...

  4. JavaWeb AJAX

    1.  Asynchronized JavaScript And XML 异步JavaScript和XML,它并不是一门新的语言或技术,实际是几项技术按一定的方式组合在一起共同的协作中发挥各自的作用, ...

  5. Spring Framework的核心:IOC容器的实现

    2.1   Spring IoC容器概述 2.1.1 IoC容器和依赖反转模式 依赖反转的要义,如果合作对象的引用或依赖关系的管理由具体对象来完成,会导致代码的高度耦合和可测性的降低.依赖控制反转的实 ...

  6. mysql TIMESTAMP 设置为可NULL字段

    今天遇到问题是mysql新建表的时候TIMESTAMP 类型的字段 默认是NOT NULL 然后上网查了一下 发现 很多都说 就是不能为NULL的 这都什么心态 其实设置为空很简单 只要在字段后面加上 ...

  7. NOIP2016之反面教材提供

    NOIP 2016信息竞赛总结 竞赛历程总结: 算下来一共学了11个月的信息竞赛,从最初进来的时候大概会一点最最基础的语法,上课什么也听不懂,然后一直追进度,我想在这个阶段中我的问题主要是自己知道自己 ...

  8. 如何理解和熟练运用js中的call及apply?

    改变this指向 要先明白存在call和apply的原因,才能记得牢一点: 在javascript OOP中,我们经常会这样定义: function cat(){ } cat.prototype={ ...

  9. Android中的Context

    Context用来访问全局信息的接口,比如影城程序的资源.一些常用的组件都是继承自Context,目的就是方便的访问资源,比如Activity, Service.... 从Context访问本组件的资 ...

  10. ICML历年Best Papers

    作者:我爱机器学习原文链接:ICML历年Best Papers ICML (Machine Learning)(1999-2016) 2016 Dueling Network Architecture ...