hdu 1556 Color the ball(树状数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球染一次色,最后问每个气球染了多少种颜色。
分析:这是树状数组的第二种应用,区间成段更新,然后求某点的值。
update(x,num)表示x位置的值增加num,sum(x)表示求1到x的和。
更新[l,r]区间时,
先update(l,k),然后update(r+1,-k),就会导致sum(l)到sum(r)的值均增加了k,然后[1,l)和(r,max)这两个范围内的sum都不变,这样sum(x)就是x这个点当前的值了。
在纸上自己画一画,模拟一下就能明白了。
AC代码如下:
#include<stdio.h>
#include<string.h>
#define N 100010
int d[N],n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=n)
{
d[x]+=num;
x+=lowbit(x);
}
}
int GetSum(int x)
{
int s=;
while(x>)
{
s+=d[x];
x-=lowbit(x);
}
return s;
}
int main()
{
int i,a,b;
while(scanf("%d",&n)&&n)
{
memset(d,,sizeof(d));
for(i=;i<n;i++)
{
scanf("%d%d",&a,&b);
update(a,);
update(b+,-);
}
for(i=;i<n;i++)
printf("%d ",GetSum(i));
printf("%d\n",GetSum(n));
}
return ;
}
上面的方法是树状数组最基本的向上修改,向下统计的形式,其实还可以向上统计,向下修改。
树状数组中的每个节点都代表了一段线段区间,每次更新的时候,根据树状数组的特性可以把b以前包含的所有区间都找出来,然后把b以前的
区间全部加一次染色次数。然后,再把a以前的区间全部减一次染色次数,这样就修改了树状数组中的[a,b]的区间染色次数,查询每一个点总的染色次
数的时候,就可以直接向上统计每个父节点的值,就是包含这个点的所有区间被染色次数,这就是树状数组中向下查询,向上统计的典型应用
这样的话update()和sum()的代码就需要修改了,详见代码。
AC代码如下:
#include<stdio.h>
#include<string.h>
#define N 100010
int d[N],n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x>)
{
d[x]+=num;
x-=lowbit(x);
}
}
int sum(int x)
{
int s=;
while(x<=n)
{
s+=d[x];
x+=lowbit(x);
}
return s;
}
int main()
{
int i,a,b;
while(scanf("%d",&n)&&n)
{
memset(d,,sizeof(d));
for(i=;i<n;i++)
{
scanf("%d%d",&a,&b);
update(a-,-);
update(b,);
}
for(i=;i<n;i++)
printf("%d ",sum(i));
printf("%d\n",sum(n));
}
return ;
}
hdu 1556 Color the ball(树状数组)的更多相关文章
- HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...
- HDU 1556 Color the ball (树状数组区间更新)
水题,练习一下树状数组实现区间更新. 对于每个区间,区间左端点+1,右端点的后一位-1,查询每个位置的覆盖次数 #include <cstdio> #include <cstring ...
- HDU 1556 Color the ball 树状数组 题解
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...
- Color the ball(树状数组+线段树+二分)
Color the ball Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- HDU 1556 Color the ball【差分数组裸题/模板】
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...
- 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 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
随机推荐
- 【HEOI2016】序列
题面 题解 很像最长不下降子序列对吧(废话) 设$up[i]$和$down[i]$分别表示$i$最大最小能取多少 注意到: $$ f[i] = max_j\left\{f[j]\right\} + 1 ...
- 【JLOI2013】卡牌游戏
题面 题解 概率$dp$ 设$f[i][j]$表示还剩$i$个人时,第$j$个人获胜的概率. 边界$f[1][1] = 1$ 转移: 枚举庄家抽到的卡牌$k$,得到这一轮被淘汰的位置$c$. 可以知道 ...
- Kubernetes学习之路(七)之Coredns和Dashboard二进制部署
一.CoreDNS部署 在 Cluster 中,除了可以通过 Cluster IP 访问 Service,Kubernetes 还提供了更为方便的 DNS 访问. (1)编辑coredns.yaml文 ...
- [BZOJ4484][JSOI2015]最小表示[拓扑排序+bitset]
题意 给你一个 \(n\) 个点 \(m\) 条边的 \(\rm DAG\) ,询问最多能够删除多少条边,使得图的连通性不变 \(n\leq 3\times 10^4\ ,m\leq 10^5\) . ...
- Zabbix实战-简易教程--WEB类--Nginx
一.开启Nginx status状态 1.在默认主机里面加上location添加ngx_status 如下操作: server { listen 127.0.0.1:8080; server_name ...
- Python标准库学习之zipfile模块
ZipFile模块里有两个非常重要的class, 分别是 ZipFile和ZipInfo. ZipFile是主要的类,用来创建和读取zip文件,而ZipInfo是存储的zip文件的每个文件的信息的. ...
- HT7A6312—— 离线开关电源小功率初级转换开关IC 记录总结
1. 芯片特性 a. 固定60KHz开关频率: b. 宽Vcc输出电压范围:9V - 38V: c. 宽交流输入电压范围:85Vac - 265Vac: d. 电流模式PWM控制: e. 带迟滞的辅助 ...
- TPO-19 C1 Discussing A Point Raised In A Lecture
TPO-19 C1 Discussing A Point Raised In A Lecture 第 1 段 1.Listen to a conversation between a student ...
- yocto-sumo源码解析(七): BitBakeServer
1. 创建域套接字,管道以及锁: self.configuration = configuration self.featureset = featureset self.sockname = soc ...
- Discuz3.3精仿小米风格整站模板制作——1、新建模板方案
术语说明: 模板——模板是一堆按照规定命名方式的html文件,用于指定整个论坛不同页面的外观. 标签——标签和模板共同作用以实现论坛换肤功能,其中标签主要控制页面显示什么数据,显示多少条等. 风格—— ...