算法学习:http://www.cnblogs.com/George1994/p/7710886.html

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541

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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
 
题目大意:求左下方有多少个点,包括左方和下方
个人思路:也没什么思路,就是用树状数组吧,第一次接触,有点难
看代码:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int ans[maxn];//ans[i]代表有i个星星的点有多少个
int a[maxk];//a[i]代表横坐标为i的左下角(包括左和下)有多少个星星
int lowbit(int x)//可以求出x的二进制最低位1的位置的数值比如110,答案就是10
{
return x&(-x);
}
int getsum(int x)//求有多少个点
{
int sum=;
while(x>)
{
sum+=a[x];
x-=lowbit(x);
}
return sum;
}
void updata(int x)//更新操作
{
while(x<maxk)
{
a[x]++;
x+=lowbit(x);
}
}
int main()
{
int n,x,y;
while(scanf("%d",&n)!=EOF)
{
memset(ans,,sizeof(ans));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
{
cin>>x>>y;
ans[getsum(x+)]++;//因为x可能为0,但是树状数组是从1开始的,所以整个坐标往右移
updata(x+);
}
for(int i=;i<n;i++)
cout<<ans[i]<<endl;
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=;
const ll maxa=;
#define INF 0x3f3f3f3f3f3f
int a[maxn],c[maxa];
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int res=;
while(x>)
{
res+=c[x];
x=x-lowbit(x);
}
return res;
}
void updata(int x)
{
while(x<maxa)
{
c[x]++;
x=x+lowbit(x);
}
}
int main()
{
int n,x,y;
memset(a,,sizeof(a));
memset(c,,sizeof(c));
scanf("%d",&n);
for(int i=;i<n;i++)
{
cin>>x>>y;
a[getsum(x+)]++;
updata(x+);
}
for(int i=;i<n;i++)
cout<<a[i]<<endl;
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(树状数组)

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

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

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

  5. HDU-1541 Stars 树状数组

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

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

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

  7. hdu1541 Stars 树状数组

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

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

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

  9. POJ2352 Stars 树状数组

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

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

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

随机推荐

  1. linux命令-tar工具详解

    把文件和目录打成一个包 文件打包 [root@wangshaojun ~]# tar -cvf 1.tar 1.txt 123 234 ///-c创建 -v可视化 -f file放最后面1.txt12 ...

  2. C++中的友元小结

    我们知道,在一个类总可以有公有的(public)成员和私有的(private)成员.在类外可以访问公用成员,只有本类中的函数可以访问本类的私有成员. 现在,我们学习一种新的情况--友元. 在C++中, ...

  3. 【spring boot logback】日志颜色渲染,使用logback-spring.xml自定义的配置文件后,日志没有颜色了

    接着spring boot日志logback解析之后,发现使用logback-spring.xml自定义的配置文件后,日志没有颜色了 怎么办? 官网处理日志链接:https://logback.qos ...

  4. 转:Linux下用Jmeter做接口测试

    本地设计 首先在本地设计 Apache JMeter 测试计划,大家可以参考<接口测试之 JMeter 初探> ,这里不再重复. 服务器配置 确保服务器已经安装了JDK和Python. 在 ...

  5. idea中java项目删除一个module

    在idea中删除一个module,需要两步: 1. 使用Remove Module命令,快捷键是Delete 2. 经过第一步后,module图标上的小方块,消失,编程一个普通文件夹,这时使用Dele ...

  6. 【TMF eTOM】业务流程框架介绍

    TMF文档版权信息 Copyright © TeleManagement Forum 2013. All Rights Reserved. This document and translations ...

  7. MVC过滤大法(过滤静态文件)

    参考文章:https://prerakkaushik.wordpress.com/2014/02/12/routing-request-for-static-files-with-or-without ...

  8. MVC下使用ajax后台查询值赋值到前端控件

    初学MVC,今天做个简单的功能,就是输入BeginDate和EndDate,从后台计算后赋值给另外一个文本框Amount 界面很简单,方法也很简单,今天就使用jquery的post方法,先准备后台代码 ...

  9. Pillow不支持color emoji font!

    我想在MAC下面用pillow把一些文本转换成PNG图片,在转普通文字的时候都没问题,但在遇到emoji字符的时候就搞不定了,代码如下: import loggingimport PIL.Image ...

  10. vue -- 异常处理集合

    1.npm run dev 运行出错,报错如下: > webpack-dev-server --inline --progress --config build/webpack.dev.conf ...