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

题目意思:有 n 个气球从左到右排成一排,编号依次为1,2,3,...,n。给出 n 对 a, b,表示编号为 a ~b 的气球被涂过一次色。n 次之后,问所有气球被上色的次数分别是多少。

  典型的树状数组题!这种是:每次修改一个区间,问的是最后每个点被修改的次数。要注意update() 函数对 a 处理 的时候,向上修改的过程中把不必要的区间都加了一遍。例如对a = 2时,update修改了c[2],c[4],c[8],那么还需要对 b+1 进行update,把不必要的被修改区间调整回来,即减去1。

  树状数组还是很不熟悉,尤其对于get_sum() 函数。对样例

8
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
0

处理完之后得出以下这个图:

ans:  8 7 6 5 4 3 2 1

update 比较好理解,不再累赘。

  get_sum(1) = c[1] = 8;    get_sum(2) = c[2] = 7;                     get_sum(3) = c[3] + c[2] = -1 + 7 = 6

  get_sum(4) = c[4] = 5;    get_sum(5) = c[5] + c[4] = -1 + 5 =4;       get_sum(6)  = c[6] + c[4] = -2 + 5 = 3

  get_sum(7) = c[7] + c[6] + c[4] = -1 -2 + 5 = 2;         get_sum(8) = c[8] = 1

  经过手动模拟,对这个求和函数了解了不少  (灵活运用还需要一段时间,继续努力吧[^_^])

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
int n;
int c[maxn]; int lowbit(int x)
{
return x & (-x);
} void update(int x, int num)
{
while (x <= n) {
c[x] += num;
x += lowbit(x);
}
} int get_sum(int x)
{
int s = ;
while (x > ) {
s += c[x];
x -= lowbit(x);
}
return s;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &n) != EOF && n) {
memset(c, , sizeof(c));
int a, b;
for (int i = ; i < n; i++) {
scanf("%d%d", &a, &b);
update(a, );
update(b+, -);
}
for (int i = ; i <= n; i++) {
printf("%d%c", get_sum(i), (i == n ? '\n' : ' '));
}
}
return ;
}

  

hdu 1556.Color the ball 解题报告的更多相关文章

  1. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. HDU 1556 Color the ball【算法的优化】

    /* 解题思路:每次仅仅求解一開始的第一个数字,让第一个数字加一,最后的一个数字的后面一个数减一.我们能够想想,最后加的时候,就是加上前面一个数出现的次数和自己本身出现的次数. 解题人:lingnic ...

  6. 线段树(求单结点) hdu 1556 Color the ball

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 1556 Color the ball(区间更新,单点求值)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  9. hdu 1556 Color the ball (线段树+代码详解)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. Linux里startup.sh 和 shutdown.sh

    最近用socket编写了一个服务端程序,监听1024端口,检测客户端发来的请求,所在Linux里写启动和停止的脚本: 在Eclipse里java写好程序,右击导出生成 Runnable JAR fil ...

  2. Markdown 写作工具选择

    Markdown 写作工具选择 候选产品 参考了少数派网站 markdown 写作工具2015年度盘点 http://sspai.com/32483, Windows 下 Markdown 的编辑工具 ...

  3. git checkout -b 的详细讲解

    创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...

  4. html 图像映射

    个人先做而一个例子 <body> <img src="图像映射/enterdesk.com-D69055E2B422567CB273963EA05FF7D4.jpg&quo ...

  5. 利用PHP读取文件

    $fp=fopen("D:\\phpStudy\\www\\date\\file\\2.txt","r");if($fp){    while(!feof($f ...

  6. PHP get_class 返回对象的类名

    get_class (PHP 4, PHP 5) get_class — 返回对象的类名 说明 string get_class ([ object $obj ] ) 返回对象实例 obj 所属类的名 ...

  7. php网络编程

    php面试题之三--PHP网络编程(高级部分) 三.PHP网络编程 [!!!]1.禁用COOKIE后SEESION还能用吗?(51.com笔试题) 可以,COOKIE和SESSION都是用来实现会话机 ...

  8. View和ViewImage设置图片

    1.view类的设置背景android:background --setBackgroundResource(int) --A drawable to use as the background. s ...

  9. C,C++经典笔试题(答案)转自:http://blog.163.com/jianhuali0118@126/blog/static/377499702008230104125229/

    一.请填写BOOL , float, 指针变量 与“零值”比较的 if 语句.(10分) 请写出 BOOL   flag 与“零值”比较的 if 语句.(3分) 标准答案:      if ( fla ...

  10. ajaxfileupload回到json带<pre>

    ajaxfileupload返回json带<pre> 老系统,将文件上传方式修改为ajax上传,调用ajaxfileupload.js 出错现象: 文件正常提交,后台接收正常,action ...