树状数组入门博客推荐 http://blog.csdn.net/qq_34374664/article/details/52787481

Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9768    Accepted Submission(s): 3914

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坐标为第一优先级,x坐标为第二优先级排好序,输入。level的定义为 某个点的左下方存在点的个数(不包括本身)就是其level。问 0~n-1中 每个level点的个数。
思路  A[i]表示x坐标为i的个数 ( A[]无形数组)  C[]为A[]的树状数组,那么GetSum(i)就是 序列中前i个元素的和,即x小于等于i的点数。getsum(i)就是x坐标为i的点的level。
注意  0<=X,Y<=32000  我们要将x坐标都加1 才会避免函数add进入死循环
 
更加详细解读请参考博客 http://blog.csdn.net/ljd4305/article/details/10183285
 
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#define maxn 32050
using namespace std;
int c[maxn];
int level[maxn];
int lowbit(int x)
{
return x&(-x);
}
int getsum(int x)
{
int ans=;
while(x>)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
void add(int x,int y)
{
while(x<maxn)
{
c[x]+=y;
x+=lowbit(x);
}
}
int main()
{
int n;
int x,y;
int i,j,k;
while(scanf("%d",&n)!=EOF)
{
memset(c,,sizeof(c));
memset(level,,sizeof(level));
for(i=;i<n;i++)
{
cin>>x>>y;
x++;
level[getsum(x)]++;
add(x,);
}
for(i=;i<n;i++)
cout<<level[i]<<endl;
}
return ;
}

HDU 1541 树状数组的更多相关文章

  1. 【模板】HDU 1541 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...

  2. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  5. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  6. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  7. HDU 1934 树状数组 也可以用线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...

  8. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  9. hdu 5147 树状数组

    题意:求满足a<b<c<d,A[a]<A[b],A[c]<A[d]的所有四元组(a,b,c,d)的个数 看到逆序对顺序对之类的问题一开始想到了曾经用归并排序求逆序对,结果 ...

随机推荐

  1. iOS 计时器三种定时器的用法NSTimer、CADisplayLink、GCD

    原文:http://www.cocoachina.com/ios/20160919/17595.html DEMO链接

  2. Java中list<Object[]>、list<Student>、list<Map<String,String>>排序

    1:list<Object[]>的排序   public static void main(String[] args) { // TODO Auto-generated method s ...

  3. userdel 命令详解

    userdel  作用: 删除指定用户,以及用户相关的文件. 如不加选项,则仅删除用户账号,而不删除相关文件 选项: -f:强制删除用户,即时用户当前已登录 -r:删除用户的同时删除与用户相关的所有文 ...

  4. 未来五年什么样的IT技术最具颠覆性?这里有你想知道的答案

    据外媒报道称,近日Gartner研讨会在美国弗罗里达州奥兰多举行,智能化.大数据和物联网成为届研讨会的三大主题.市场研究机构Gartner Research的副总裁兼资深研究员大卫·卡利(David ...

  5. 韩顺平教学资源java、oracle、linux

    http://blog.itpub.net/28688617/viewspace-766392/

  6. java 集合类基础问题汇总

     1.Java集合类框架的基本接口有哪些? 参考答案 集合类接口指定了一组叫做元素的对象.集合类接口的每一种具体的实现类都可以选择以它自己的方式对元素进行保存和排序.有的集合类允许重复的键,有些不允许 ...

  7. ABP 框架从源码学习——abp框架启动核心类AbpBootstrapper(2)

    在AbpBootstrapper中的两个至关重要的属性:IIocManager 和 IAbpModuleManager  public class AbpBootstrapper : IDisposa ...

  8. OLAP与数据仓库------《Designing Data-Intensive Applications》读书笔记4

    由于第三章的内容比较多,这里我们拆分成两篇读书笔记来记录.上一章我们聊了聊如何数据库是如何实现存储和检索的,今天这篇我们继续来看看OLTP与OLAP存储引擎的区别与联系. 1.OLTP与OLAP 联机 ...

  9. 使用ui-route实现多层嵌套路由

    一.预期实现效果: https://liyuan-meng.github.io/uiRouter-app/index.html (项目地址:https://github.com/liyuan-meng ...

  10. ajax写登录页面

    静态配置 STATICFILES_DIRS = ( os.path.join(BASE_DIR,'my_blog','static'), ) AUTH_USER_MODEL = "app01 ...