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 ...
随机推荐
- 【转】10 个迅速提升你 Git 水平的提示
最近我们推出了两个教程:熟悉Git的基本功能和让你在开发团队中熟练的使用Git . 我们所讨论的命令足够一个开发者在Git使用方面游刃有余.在这篇文章中,我们试图探索怎样有效的管理你的时间和充分的使用 ...
- iOS学习之自定义视图时,在屏幕发生旋转时触发重新布局方法
如果要对自定义的视图在屏幕旋转时重新布局,则在自定义视图中定义以下触发方法: -(void)layoutSubviews { [super layoutSubviews]; //1.获取到屏幕旋转的方 ...
- No.5 表达式中的陷阱
1. 关于字符串的陷阱 JVM对字符串的处理 String java = new String("Java"); 创建了几个对象? 2个."Java"直接量对应 ...
- 自定义 cell 自适应高度
#import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- POJ-2955括号匹配问题(区间DP)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4834 Accepted: 2574 Descript ...
- c++在函数后面加const
非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...
- 【转】Linux下查看TCP网络连接情况
查看TCP网络连接情况 命令:netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’ www.2cto.com 返回结 ...
- jQuery 动画之 添加商品到购物车
前台页面 <link href="MyCar.css" rel="stylesheet" /> <script src="../jq ...
- python标准库 bisect模块
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #bisect #作用:维护有序列表,而不必在每次向列表增加一个元素 ...