Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8610    Accepted Submission(s): 3428

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和横纵坐标,输出等级从0~n-1。

解题思路:
刚开始的时候不怎么有思路,直接暴力接触,开一个x[]的数组保存每个点的横坐标,开一个y[]的数组保存每一个点的纵坐标。用双重for循环去跑,直接统计左下方星星的个数,然后对应的等级上面++。投了很多次,侥幸AC。然后看了网上的题解,明白是用树状数组过。首先我们可以假设存在一个数组a[]。它是用来保存当前x坐标为i的星星的个数的a[i]。为什么要这么做?因为我们观察数据输入可以知道,输入是有规律的,先按照y坐标不变,x坐标依次增大的顺序,然后y坐标变大,再按照x坐标依次增大的顺序来输入。假如,y坐标不变,每次输入进来一个坐标,只要看x就可以了,y可以忽略。每次进来一个星星,就a[i]++,同时计算这个星星的等级,level=a[0]+a[i]+a[2]+...a[i]。看着很像树状数组里面的区间求和啊。那么y增大会不会有影响呢?答案是不会的,因为y增大后新进来的星星肯定在原来星星的上方,相当于直接对a[]数组更新,有点滚动数组的味道,然后求得是左下方星星的个数,只不过是a[i]需要再次进行a[i]++的操作,与y安全没有关系。

防坑指南:
树状数组里面的下标是从1开始的,同时应该先计算新添加进来的星星的level,然后再跟新这个点对c[]数组的影响,这样会好一些



源代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; typedef long long ll;
#define eps 1e-6
#define e exp(1.0)
#define pi acos(-1.0)
const int MAXN = 32005;
const int INF = 0x3f3f3f3f; int c[MAXN];
int level[MAXN]; int lowbit(int x)
{
return x&(-x);
} void add(int x, int num)
{
while(x<=MAXN)
{
c[x]+=num;
x+=lowbit(x);
}
} int sum(int x)
{
int res = 0;
while(x>0)
{
res+=c[x];
x-=lowbit(x);
}
return res;
} int main()
{
int t;
int i,j,x,y;
while(~scanf("%d",&t))
{
memset(level,0,sizeof(level));
memset(c,0,sizeof(c));
for(i = 0; i < t; i++)
{
scanf("%d%d",&x,&y);
level[sum(x+1)]++;
add(x+1,1);
}
for(j = 0; j < t; j++)
{
printf("%d\n",level[j]);
}
}
return 0;
}

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

  1. HDU-1541 Stars 树状数组

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

  2. hdu1541 Stars 树状数组

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

  3. POJ-2352 Stars 树状数组

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

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

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

  5. Stars(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 Stars Time Limit: 2000/1000 MS (Java/Others)    Memor ...

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

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

  7. HDU1541 经典树状数组

    HDU1541 题意: 如图,等级为0的点有1,等级为1得点有4,2  等级为2的点有3,等级为3的点有5-------即即左下角的点的个数 现给你一些点(x,y),输入顺序按y升序,y相等时按x升序 ...

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

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

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

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

  10. POJ2352 Stars 树状数组

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

随机推荐

  1. 机器学习技法:06 Support Vector Regression

    Roadmap Kernel Ridge Regression Support Vector Regression Primal Support Vector Regression Dual Summ ...

  2. Problem G

    Problem Description A relay is a race for two or more teams of runners. Each member of a team runs o ...

  3. T9

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

  4. zabbix 2.2.20 安装详解(Centos6.9)

    环境说明 [root@centos ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@centos ~]# uname -a L ...

  5. Python pip 下载速度慢? Windows 设置 国内源,用 阿里云 国内镜像 加速

    pip 提供了对 Python 包的查找.下载.安装.卸载的功能,是非常方便的 Python 包管理工具.但是,令人苦恼的是 pip 在国内的下载速度非常慢,速度常常只有每秒几十 K,甚至才几 K,小 ...

  6. AngularJS学习篇(九)

    AngularJS XMLHttpRequest $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据. $http.get('someUrl',config).then(s ...

  7. javascript中自定义事件

    自定义事件:用户可以指定事件类型,这个类型实际上就是一个字符串,然后为这个类型的事件指定事件处理函数,可以注册多个事件处理函数(用数组管理),调用时,从多个事件处理函数中找到再调用. function ...

  8. touch pointer

    在早期的浏览器,输入的事件其实相对单纯,只有考虑到鼠标和键盘两种:而当时的鼠标事件,其实就是 click.mousedown.mouseup 等等的事件.但是当手机.平板开始流行时候,再移动装置上的主 ...

  9. 大家好,我是ZCDHJ

    大家好,我是ZCDHJ.CJ C2017级的一名Oier.

  10. mysql查询锁表及解锁

    SHOW PROCESSLIST; KILL ; 锁表网上解释: 这牵涉到mysql的事务,简单通俗的话,就这样给你解释有一个任务序列控制sql语句的执行,第一次有select的语句查询表a,mysq ...