转自:https://blog.csdn.net/flushhip/article/details/79165701#commentBox

1.首先其中讲到了一个问题,就是如何求一个数的二进制表示的最末位的1.

int lowbit(x)
{
return x & -x;
}

用这个即可。假设x=5,那么x的二进制就是0101,-x的二进制1011.(负数补码是除符号位外,从右边起遇到第一个1将其左边取反,并+1)

主要原因是,负数在计算机中是以补码来存储的。所以是这么样子的。(首位表示符号位),运行结果如下:

对于x=8来说,也是同样的到离,没毛病。

2.查询前缀和代码

int sum(int x, ArrayInt c, int n)
{
int ret = ;
for ( ; x > ; ret += c[x], x -= lowbit(x));
return ret;
}

//感觉也是比较好理解的,

比如x=6,那么c[x]所包含的和都有谁呢?

lowbit(6)=2,那么只包含两个数 即a[5]和a[6].

所以一次for循环之后,ret中是5.6的和;

第二次for循环中,x=4.

lowbit(4)=4,那么即包含1,2,3,4的和,此时ret就包含了1~4的和,返回。

3.更新后缀和

void update(int x, int val, ArrayInt c, int n)
{
for ( ; x <= n; c[x] += val, x += lowbit(x));
}

比如此时x=5,那么就需要知道如果5被更新了,都有哪个c和需要被更新,

当然首先是5,接着lowbit(5)=1,那么x=6;更新6,接着lowbit(6)=2,x=8;

更新8,此时x=12退出循环。

总:感觉更新后缀和比较难理解。总是就是,查询前缀和是一直-lowbit(x),更新后缀和就是一直+lowbit(x)即可。

从这里也理解了一些思想,对于数据结构无非就是“增删改查”。针对的就是单点更新,区间查询问题。

最后一张经典图进行理解:

题目

Stars
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 54809   Accepted: 23580

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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

//emmm,还是不太会, 跟我理解的树状数组不太一样啊!

题目大意:从左到右,从上到下,给出星星的level,星星的level是其左和下所有的星星和。

代码来自:https://blog.csdn.net/ACMer_hades/article/details/46274927

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 32222
//这道题目很巧妙,首先是根据x的增长来的,然后再根据y的增长来排的;
//所以这样就能理解为什么每次就只要求x之前的就好了
int c[maxn];
int levels[maxn];
int lowbit(int x){
return x&(-x);
}
//这里是对前x进行求和;
int sum(int x){//求x左边有多少星星。
int res=;
while(x>){ //注意这里是x>0,不能写成x>=0;
res+=c[x];
x-=lowbit(x);
}
return res;
}
//这里的目的是给下标为pos的加上1,这样的话下次询问前面的就能够累加上去了;
void update(int pos){
while(pos<=){//要加这么多的,所有包含当前的都加进来
c[pos]++;
pos+=lowbit(pos);
}
}
int main(){
int n,x,y;
while(~scanf("%d",&n)){
memset(levels,,sizeof(levels));
int tt=n;
while(tt--){
scanf("%d%d",&x,&y);
levels[sum(x+)]++;//因为坐标是从0开始的,但是树状数组是从1开始计算,所以+1.
update(x+);
}
for(int i=;i<n;i++) printf("%d\n",levels[i]);
}
}

//这代码真的很神。

我理解的误区其实就在,数据输入时已经按照y递增,y相同时,x递增来排序了,相当于从底向上,从左向右扫描。

不会出现计数不上的情况。厉害。

知识点1-树状数组[带poj Stars作为巩固]的更多相关文章

  1. 【树状数组】POJ 2352 Stars

    /** * @author johnsondu * @time 2015-8-22 * @type Binary Index Tree * ignore the coordinate of y and ...

  2. 主席树套树状数组——带修区间第k大zoj2112

    主席树带修第k大 https://www.cnblogs.com/Empress/p/4659824.html 讲的非常好的博客 首先按静态第k大建立起一组权值线段树(主席树) 然后现在要将第i个值从 ...

  3. 【树状数组】POJ 2155 Matrix

    附一篇经典翻译,学习 树状数组  http://www.hawstein.com/posts/binary-indexed-trees.html /** * @author johnsondu * @ ...

  4. [树状数组]数星星 Stars

    数 星 星 S t a r s 数星星 Stars 数星星Stars 题目描述 天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标.如果一个星星的左下方(包含正左和正下)有 k k k 颗星星 ...

  5. 【树状数组】 poj 2352

    题意:给出n个平面二维坐标,对于每个坐标,如果这个坐标跟(0,0)形成的矩形内包含的点数为 k (包含边界,但不包含坐标本身),那么这个坐标就是 level k.输出level 0 - n-1的点数分 ...

  6. 树状数组入门 hdu1541 Stars

    树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次 ...

  7. 求逆序对常用的两种算法 ----归并排 & 树状数组

    网上看了一些归并排求逆序对的文章,又看了一些树状数组的,觉得自己也写一篇试试看吧,然后本文大体也就讲个思路(没有例题),但是还是会有个程序框架的 好了下面是正文 归并排求逆序对 树状数组求逆序对 一. ...

  8. 求逆序对 ----归并排 & 树状数组

    网上看了一些归并排求逆序对的文章,又看了一些树状数组的,觉得自己也写一篇试试看吧,然后本文大体也就讲个思路(没有例题),但是还是会有个程序框架的 好了下面是正文 归并排求逆序对 树状数组求逆序对 一. ...

  9. BZOJ3787 gty的文艺妹子序列 【树状数组】【分块】

    题目分析: 首先这种乱七八糟的题目就分块.然后考虑逆序对的统计. 一是块内的,二是块之间的,三是一个块内一个块外,四是都在块外. 令分块大小为$S$. 块内的容易维护,单次维护时间是$O(S)$. 块 ...

随机推荐

  1. 转的:burp suite小例子

    Web安全测试时经常会遇到一些蹩脚的注射点,而因各种原因利用注射又无法获取网站管理账号或拥有网站管理权限却迟迟不能upload一个shell的时候,可能会权衡一下web权限与数据库信息,哪个是我们所需 ...

  2. C++11新特性之八——函数对象function

    详细请看<C++ Primer plus>(第六版中文版) http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html ...

  3. BUG:给Nexus7编译Android4.2的时候出现 fatal error: map: No such file or directory

    情况是这样的,某人最近入手一台nexus7,于是在cyanogenmod 将nexus7的原代码下载到本地,编译环境是UBUNTU 12,04 然后编译的时候,出现了如下的错误导致编译失败 <p ...

  4. XML的一些点

    最近学习Spring会配置许多XML文件,没有系统学习过XML遇到了许多问题,系统的看了一下有些拨云见日的感觉. 推荐学习:http://www.w3school.com.cn/xml/xml_int ...

  5. hdu4028 The time of a day[map优化dp]

    The time of a day Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others ...

  6. all index range ref eq_ref const system 索引type说明

    背景知识 在使用sql的过程中经常需要建立索引,而每种索引是怎么处罚的又是怎么起到作用的,首先必须知道索引和索引的类型. 索引类型type 我们可以清楚的看到type那一栏有index ALL eq_ ...

  7. Thinkphp自定义工具类的使用!

    在使用Thinkphp做开发的时候,很多时候会用到一些自己写的类,为了方便管理,可以把这些类,单独放到一个文件里. 这就是自定义工具类: 首先在 Application 目录下新建 Component ...

  8. BFS+状态压缩DP+二分枚举+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others)   ...

  9. 从零打造在线网盘系统之Hibernate框架起步

    欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...

  10. MVC学习之HtmlHelper

    1.为什么要使用HtmlHelper? 1.首先HtmlHelper是一个类型,MVC中的ViewPage<TModel>中的一个属性Html属性,这个属性的类型就是HtmlHelper& ...