解题报告:hdu 1556 Color the ball(区间修改,单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
解题思路:树状数组裸题,还是套模板。此题要求的是区间修改,不是单点修改,只需利用"差分"思想,即从a开始向上加1实现区间[a,n]中每个元素值加1,同时从b+1开始向上减1即可实现区间[b+1,n]中每个元素值减1,这样就可以实现区间[a,b]中每个元素值加1的效果。时间复杂度是O(nlogn)。可以再复习一下模板加深理解:掌握树状数组~彻底入门
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,c[100010];//表示N 4 int lowbit(int i){//数组下标二进制非0数字 5 return i & -i; 6 } 7 void update(int i,int x) 8 { 9 while(i<=n){ 10 c[i]+=x; 11 i+=lowbit(i); 12 } 13 } 14 int sum(int i) 15 { 16 int ret=0; 17 while(i>0){ 18 ret+=c[i]; 19 i-=lowbit(i); 20 } 21 return ret; 22 } 23 int main() 24 { 25 int a,b; 26 while(~scanf("%d",&n),n){ 27 memset(c,0,sizeof(c)); 28 for(int i=1;i<=n;i++){ 29 scanf("%d %d",&a,&b); 30 update(a,1);//a向上加1 31 update(b+1,-1);//b+1向上减1 32 //这样就可以实现区间[a,b]每个数加1 33 } 34 for(int i=1;i<=n;i++)//单点查询,统计前缀和即可 35 printf("%d%c",sum(i),i==n?'\n':' '); 36 } 37 return 0; 38 }
转载于:https://www.cnblogs.com/acgoto/p/8588859.html
解题报告:hdu 1556 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 ...
- 树状数组之区间修改单点查询
树状数组的区间修改单点查询 树状数组其实本质还单点修改区间查询,但是我们怎么延伸到这个呢,我们建立一个差分数组, 比如: a[10]={4, 6, 7, 5, 1, 6, ...
- 1631 小鲨鱼在51nod小学(线段树区间修改+单点查询:不用下传lazy的区间修改)
题目描述: 1631 小鲨鱼在51nod小学 鲨鱼巨巨2.0(以下简称小鲨鱼)以优异的成绩考入了51nod小学.并依靠算法方面的特长,在班里担任了许多职务. 每一个职务都有一个起始时间A和结束时间B, ...
- 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 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 1556 Color the ball
题解:基础的树状数组区间修改,单点查询. #include <cstdio> #include <cstring> int c[100005],a,b,n; int modif ...
- HDU 5861 Road 线段树区间更新单点查询
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Road Time Limit: 12000/6000 MS (Java/Othe ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- HDU - 1556 Color the ball(树状数组)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...
最新文章
- asp.net mvc @RenderBody()的问题
- linux cat EOF 变量自动解析问题
- centos6安装配置
- 微信小程序学习笔记(4)--------框架之逻辑层
- extract进程 oracle,ogg extract进程stoped问题
- Python中的驻留机制:小数据池和代码块
- 度量学习和pytorch-metric-learning的使用
- python核心编程 第八章
- Linux 进程通信之FIFO
- android模块化 osgi,蚂蚁金融级移动应用 osgi 模块化架构实践.pdf
- latex中空一整行,新的一行顶格写
- oracle erp 表结构
- 把window的鼠标指针改成Mac的
- 官方发布 激情世界杯】浩海技术 GHOST XP SP3 世界杯装机特别版★用稳定压倒一切!...
- 造车新势力回港:蔚来能重拾“老大哥”头衔吗?
- linux支持的阵列硬盘柜,在Linux上玩好
硬盘
阵列
- 字体反爬案例解析:大众点评
- CodeForces Round #521 (Div.3) B. Disturbed People
- 书籍扫描图像几何畸变校正
- numpy中np.nan(pandas中NAN)