Color the ball(树状数组)
来源:互联网 发布:人工智能五子棋问题 编辑:程序博客网 时间:2024/06/05 19:58
Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
31 12 23 331 11 21 30
Sample Output
1 1 13 2 1
Source
HDU 2006-12 Programming Contest
最近在学树状数组,老师给我们了这道题,想了好久也不觉得它和之前学的树状数组有什么关系,那种与二进制相关的储存方式,不好修改也不好访问。而且是给定区间,访问点,和之前那种给点,修改父亲节点,访问区间完全不一样。呵呵O(∩_∩)O,整个人都要疯掉
解答:
a数组是每个气球的涂色次数,在这种解答里没有用,所以我没有定义出来,写在这里只是为理解方便
定义一个b数组 b[i]=a[i]-a[i-1] ( b[0]=0 所以是不是 b[1]=a[1] )
易知 a[k]=a[k-1]+b[k] ,a[k-1]是不是也可以分解成这种形式,最后a[i]=b[1]+b[2]+b[3]+…+b[n]
当给一个start,end,在【start,end】区间里的所有点是不是都要加1
换句话说,是不是这段区间内的差值,,也就是b是没变的( b[start+1] ~ b[end] ),唯一改变的只是b[start] 加了1和 b[end+1]减了1 而已
所以 每次操作只需修改两个变量
程序:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main(){ int b[500000],n,s,e; while(1) { memset(b,0,sizeof(b)); scanf("%d",&n); if(!n) return 0; //printf("#case \n"); for(int i=1;i<=n;i++) { scanf("%d%d",&s,&e); b[s]+=1; b[e+1]-=1; } int sum=0; for(int i=1;i<=n;i++) { sum+=b[i]; printf("%d ",sum); } printf("\n"); } return 0;}
0 0
- Color the ball (树状数组)
- Color the ball(树状数组)
- Color the ball(树状数组)
- hdu1556 Color the ball(树状数组)
- Color the ball(树状数组)
- 【HDU1556】Color the ball(树状数组)
- 【HDU1556-color the ball】(树状数组)
- HDU1556 color the ball (树状数组)
- 树状数组&&Color the ball
- (树状数组)Color the ball
- hdu 1556 Color the ball(树状数组)
- HDOJ 题目1556Color the ball(树状数组)
- 杭电1556 Color the ball(树状数组)
- HDU 1556 Color the ball(树状数组)(填坑)
- Color the ball(树状数组之插点问线问题)
- hdoj 1556 Color the ball(线段树||树状数组)
- HDU 1556 Color the ball (树状数组简单应用)
- HDU 1556 Color the ball (简单树状数组)
- centos重启报错Umounting file systems:umount:/opt:device is busy
- Ceph 网络通信源代码分析
- 【Github教程】史上最全github使用方法:github入门到精通
- 并发编程之Operation Queue
- java微信app支付-统一下单
- Color the ball(树状数组)
- 操作系统基础课堂笔记
- 一些JS特效
- java将控制台信息输出到文件
- C++ BF模式串匹配算法!
- Java--cvc-complex-type.4:Attribut ‘version’ must appear on element ‘web-app’
- android设备信息获取
- 从集合的resize看Java HashMap的死循环
- 如何使用瑞萨E10A调试SH系列不带片内FLASH的单片机