题目链接: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. word中替换被批注的正文的值

    word中替换被批注的正文的值   word批注winform替换值正文             try            {                Word.Document docum ...

  2. 找不到类型“{x}.{x}”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    最近在搞一个WCF的项目... 刚开始在这条路上走... 各种崎岖... 网上搜到的一种解决方案(也是大多数情况的解决方案): 原文:http://www.cnblogs.com/Olive116/p ...

  3. codevs2574 波兰表达式

    题目描述 Description 对于 加.减.乘.除这种四则运算的表达式,我们使用的是先乘除.后加减的从左到右的顺序进行运算,如果要指定特定的顺序,就要增加括号进行表达,比如 (A+B)*C , A ...

  4. uva10870 递推关系Recurrences

    Consider recurrent functions of the following form:f(n) = a1f(n - 1) + a2f(n - 2) + a3f(n - 3) + : : ...

  5. [AngularJS] jQuery时代

    抹平浏览器差异的jQuery出现了 jQuery有什么 jQuery使得开发无刷新动态页面(AJAX)或者单页应用(SAP)变得 相当简单. 标准的HTML页面是静态的,被浏览器渲染后就产生了一个DO ...

  6. Linux下查看nginx安装目录

    输入命令行: ps  -ef | grep nginx master process后边的目录即是.

  7. php删除字符串中的所有空格

    function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n" ...

  8. Vijos P1769 网络的关键边

    Description 一个连通的无向图,有些点有A属性,有些点有B属性,可以同时具有.删掉某条边后,如果使得连通块中一些点不具有A,B属性的点,求这些边. Sol Tarjan求割边. 首先这些边一 ...

  9. iframe自适应宽度

    <iframe id="course_content" style="width:100%;margin:5px 0 0;" scrolling=&quo ...

  10. linux 命令free -m 命令结果分析

    free -m 命令详解 free -m 分析系统内存使用情况: