题目链接: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. php正则表达式匹配用户名规则:由字母开头的6-16位字母和数字组成的字符串

    $pattern = '/^[a-zA-Z]{1}[a-zA-Z0-9]{5,15}$/i';

  2. jQuery 学习之路(2):选择器与过滤器

    一.基本选择器 标签选择器: $('button') ID选择器: $('#id1') 类选择器: $('.class1') 多重选择器: $('#id1,.class1,button') 全体选择器 ...

  3. vs2010 中无法打开 源文件 "stdafx.h" 未定义标识符 “xxx”

    解决方案: 项目属性->配置属性->C/C++->常规->附加包含目录->$(ProjectDir)

  4. Junit初级编码(一)第一个Junit测试程序

    序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.目前最新版本JAR包为4.12,官网地址为http://junit.org/ 一.第一个Junit测试程序 1 去官网下 ...

  5. mysql 删除重复数据,并保存最新一条数据

    删除重复行 DELETE FROM ecm_member_login_session WHERE (number , client_code) IN ( ) AND update_time NOT I ...

  6. springMVC之国际化

    1.工程结构 2.jar包 3.配置文件spring-config.xml,springMVC配置文件 <?xml version="1.0" encoding=" ...

  7. Codeforces Round #336 Hamming Distance Sum

    题目: http://codeforces.com/contest/608/problem/B 字符串a和字符串b进行比较,以题目中的第一个样例为例,我刚开始的想法是拿01与00.01.11.11从左 ...

  8. BZOJ4612——[Wf2016]Forever Young

    1.题意:将一个数y转化成b进制数,使得他>=l,且<y,且转化后均为0~9,使b最大. 2.分析:我们发现要么答案的b很小,要么y很小..那我们直接暴力枚举就好了啊 然后判断一下...另 ...

  9. 一起入门python3之元组和数列

    这一节我们来说一下,元组(tupe)&数列(list).每天苦逼的工作不易啊,哎.不过呢一腔热血学习.哈哈哈哈 #井号代表注释哈. 0x01 数列-list        数列可以说是一种集合 ...

  10. OpenCv椭圆皮肤模型

    Mat input_image; Mat output_mask; Mat output_image; void main() { VideoCapture cam(); if (!cam.isOpe ...