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 ...
随机推荐
- Angularjs 日期格式转换
我自己的随笔,记录我编码的点滴. <!DOCTYPE HTML><html><head> <meta charset="utf-8" ...
- MFC下的aero效果学习笔记
最近想在MFC中用下aero 首先参考了 http://blog.csdn.net/polytechnic/article/details/5696797 中的一系列的步骤,进行了初步学习 但是对于其 ...
- RCP打包出来 运行 出现 JVM terminated.exit code = 13
在建立PM.product,即打包时,没有添加相应的插件,导致无法运行
- 将HTML格式的String转化为HTMLElement
代码如下: <meta charset="UTF-8"> <title>Insert title here</title> </head& ...
- 提交 应用ID 证书
https://developer.apple.com/account/ios/profile/profileCreate.action?formID=960914622
- a:hover和a:visited书写顺序的重要性
2a:hover和a:visited书写顺序的重要性今天在用a:hover属性的时候发现一个奇怪的问题,同一个页面里面有些链接的a:hover效果不能正常表现出来.链接的代码是一样,没有使用其它样式固 ...
- android run process
http://www.jb51.net/article/32798.htm http://www.elecfans.com/tongxin/119/20120315263977.html 图 1 详细 ...
- hdu 1255 覆盖的面积
http://acm.hdu.edu.cn/showproblem.php?pid=1255 #include <cstdio> #include <cstring> #inc ...
- 25045操作标准子程序集41.C
/* ;程 序 最 后 修 改 时 间 0-4-3 23:43 ;软 件 标 题:25045操作标准子程序集41 ;软 件 说 明:25045 I2C 串行EEPROM 驱动 ;___________ ...
- 用continue语句的时候,要千万小心内存泄漏,当然还有return和break也是
疑惑了大半年的内存泄漏,居然是因为这个原因- 有空学学QT的指针使用,可以使得代码更简洁.更不容易内存泄漏-