算法学习: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. BZOJ2809:[APIO2012]dispatching

    浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  2. WPF如何更改系统控件的默认高亮颜色 (Highlight brush)

    我们在用WPF时, 经常会对系统控件的默认高亮等等颜色进行更改. 以前通常是用controlTemplate来实现. 今天发现一个更合理或者简单的方法: 用系统默认颜色的key, 比如 SystemC ...

  3. HDOJ1024(最大M子段和)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. Excel对重复数据分组,求出不同的数据(office 2013)

    第一步: 第二步: 第三步:

  5. Puppet master nginx 扩展提升性能(puppet自动化系列4)

    puppet使用SSL(https)协议来进行通讯,默认情况下,puppet server端使用基于Ruby的WEBRick HTTP服务器.由于WEBRick HTTP服务器在处理agent端的性能 ...

  6. java继承示例

    package day07; class Fu { int num = 5; } class Zi extends Fu { int num =7; void show() { int num =9; ...

  7. C语言学习笔记--C语言中的逗号表达式

    逗号表达式:exp1,exp2,epx3,...,expN; (1)逗号表达式是 C 语言中的“粘贴剂” (2)逗号表达式用于将多个子表达式连接为一个表达式 (3)逗号表达式的值为最后一个子表达式的值 ...

  8. ubuntu下sourceinsight的安装

    转载自blog.csdn.net/zzobin/article/details/7376616 1. 安装wine 详看:http://wiki.ubuntu.org.cn/Wine sudo apt ...

  9. JWT使用过程中遇到的问题

    1.创建token的盐设置过于简单,出现secret key byte array cannot be null or empty. 异常 解决方法:jwt:config:key:hwy ------ ...

  10. 【msyql_获取时间的前后几天函数date_sub】

    select now()-- 2017-05-16 16:48:02select curdate()  -- 2017-05-16 select curdate() + 1 -- 20170517 s ...