HDU 1556 Color the ball - from lanshui_Yang
当N = 0,输入结束。
1 1
2 2
3 3
3
1 1
1 2
1 3
0
3 2 1
这是一道典型的一维树状数组的变形,普通的一维树状数组的用途是:单点更新,区间求值。而这道题的则是用到树状数组的另一个用途:区间更新,单点求值。原理如下:
假设原始数组的各个元素为a[1] , a[2] ,…… a[n] , 那么 d[n] = a[1] + a[2] + …… + a[n] 求的就是前n项和,这就是树状数组的第一个用途:单点更新,区间求和。
然后,稍微做些改动,假设原始数组的各个元素为a[1] - 0 , a[2] - a[1] , a[3] - a[2] ,……,a[n] - a[n - 1] , 那么此时的前n项和 d[n] = a[n] ,也就是说,现在原始数组的前n项和d[n] 就等于单点的值a[n] 了 ,大家看到这里是不是就有些明白了呢?
接着,如果你想时区间[ a[m] …… a[n] ] 中的所有值都 + Val ,那么只需将原始数组的第m项 (a[m] - a[m - 1] ) 加上 Val , 和将第n + 1项 (a[n + 1] - a[n]) 减去 Val 就可以了, 这样当 m <= i <= n 时 ,
数列的前 i 项和:
d[i] = (a[1] - 0) + (a[2] - a[1]) + (a[3] - a[2]) + …… + (a[m] - a[m - 1] + val) + (a[m + 1] - a[m]) + …… + (a[i] - a[i - 1] ) = a[i] + val 。
同理当 i > n 时 ,d[i] 等于原来的 a[i] 。看到这里,大家是不是就豁然开朗啦。注意一点,这里a[1] …… a[n] 的初始值均为0 !!
下面请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std ;
const int MAXN = 1e5 + 5 ;
int C[MAXN] ;
int n ;
int lowbit (int x)
{
return x & -x ;
}
void add(int x , int d)
{
while(x <= n)
{
C[x] += d ;
x += lowbit(x) ;
}
}
int sum(int x)
{
int sumt = 0 ;
while (x > 0)
{
sumt += C[x] ;
x -= lowbit(x) ;
}
return sumt ;
}
int main()
{
while (scanf("%d" , &n) != EOF)
{
if(n == 0 )
break ;
memset(C , 0 , sizeof(C)) ;
int t = n ;
int i ;
while ( t-- )
{
int a , b ;
scanf("%d%d", &a , &b) ;
add(a , + 1) ;
add(b + 1 , -1) ;
}
for(i = 1 ; i <= n ; i ++)
{
printf("%d" , sum(i)) ;
if(i < n)
printf(" ") ;
}
puts("") ;
}
return 0 ;
}
HDU 1556 Color the ball - from lanshui_Yang的更多相关文章
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball (数状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 线段树(求单结点) hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball(区间更新,单点求值)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
随机推荐
- JDBC框架
一.元数据介绍 元数据指的是"数据库"."表"."列"的定义信息. 1.1.DataBaseMetaData元数据 Connection.g ...
- 拼接xml时出现内存溢出解决办法
/// designed by Alex /// 解决内存溢出问题,不可使用迭代器和foreach /// 循环遍历List, 别生成xml,一边释放 /// 反向遍历,相当于出栈,避免索引顺序乱的问 ...
- BZOJ 1977 次小生成树(最近公共祖先)
题意:求一棵树的严格次小生成树,即权值严格大于最小生成树且权值最小的生成树. 先求最小生成树,对于每个不在树中的边,取两点间路径的信息,如果这条边的权值等于路径中的权值最大值,那就删掉路径中的次大值, ...
- centos 6.4 更新源地址
执行如下命令: mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.old vim /etc/yum.repo ...
- js深入研究之匿名函数
/* 匿名函数*/ (function() { var foo = 10; var bar = 2; alert(foo * bar);})(); /* 匿名函数,带参数 */ (function(f ...
- 【转】用户空间使用i2c_dev--不错
原文网址:http://blog.csdn.net/yuanlulu/article/details/6161706 ========================================= ...
- 链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...
- 使用cx_Freeze 将python3代码打包成.exe程序
在这里分享一下如何在py3下使用cx_Freeze打包pyqt5的程序 首先吐槽下,深深鄙视一下百度,各种百度各种没有,之前我在py2.7下使用pyqt4开发过一个小软件,用的是py2exe进行打包的 ...
- ABAP - 日期格式转换 & ABAP经常使用日期处理函数
ABAP - 日期格式转换 如今提供下面一些日期格式转换的函数: Below are several FMs which can be used to convert date format. 1. ...
- HDU 4362 Dragon Ball 线段树
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...