HDU 1556 | Color the Ball —— 树状数组
来源:互联网 发布:java常用api总结 编辑:程序博客网 时间:2024/05/22 13:30
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17227 Accepted Submission(s): 8614
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
Author
8600
Source
HDU 2006-12 Programming Contest
Recommend
LL
参考文章:http://www.cnblogs.com/zichi/p/4807072.html
这道题是在一段区间内进行一定的操作,并要问每一点的情况,和上一题敌兵布阵刚好相反。
用参考博客的话说就是 改段求点。
由于树状数组的c[i]表示从1~i每一个点的变化情况,那么对于一个区间段a~b的变化,可以考虑为:
1~b的区间变化 - 1~a-b区间的变化。
其实和询问区间长度的思维不谋而合。
对于此题需要对整个树状数组的含义进行修改。
定义c[i]表示从1~i区间内已经刷过的次数
我们知道,从c[i]+1表示区间1~i内所有元素【A[i]】全部+1;如果是在一个区间内【a,b】,就需要将没有修改的那一部分1~a-1全部-1
拿用例来说
31 11 22 3
首先c[1]=1,
第二步c[1]=2,c[2]=1
第三部c[1]=3,c[3]=1;
求每个涂色的次数即:
从k开始向祖先节点寻找到n,其累加总量即为所需要的值。
也就是把能管辖到A[i]的父节点的c值累积起来即可
#include<stdio.h> #include<stdlib.h> #include<string.h>#include<string>#include<vector>#include<ctype.h>#define N 100001using namespace std;int tree[N],n;void update(int k,int val) {while(k){tree[k]+=val;k-=k&-k;}}int read(int k){int sum=0;while(k<=n){sum+=tree[k];k+=k&-k;}return sum;}int main(){int a,b;while(scanf("%d",&n)!=EOF){if(n==0) break;memset(tree,0,sizeof(tree));for(int i=1;i<=n;i++){scanf("%d%d",&a,&b);update(b,1);update(a-1,-1);}for(int i=1;i<=n;i++){if(i<n)printf("%d ",read(i));else printf("%d\n",read(i));}}return 0;}
0 0
- hdu——1556Color the ball(树状数组)
- HDU 1556 | Color the Ball —— 树状数组
- HDU 1556 color the ball 树状数组
- hdu 1556 Color the ball (树状数组)
- hdu 1556 Color the ball 树状数组
- HDU 1556 Color the ball(树状数组)
- HDU 1556--Color the ball【树状数组】
- hdu 1556 Color the ball 树状数组
- hdu 1556 Color the ball树状数组
- 树状数组-HDU-1556-Color the ball
- hdu 1556 Color the ball(树状数组)
- HDU 1556 Color the ball(树状数组)
- HDU 1556 Color the ball (树状数组)
- hdu 1556 Color the ball(树状数组)
- Color the ball——树状数组
- HDU 1556 Color the the ball(树状数组)
- HDU Color the ball (树状数组)
- hdu Color the ball树状数组
- Codeforces#385C_素数打表+dp
- 组队赛之想出了sg函数也不会实现
- 最小圈
- NOIP2012 开车旅行 [Splay] [ST倍增]
- 单元测试框架-Junit介绍
- HDU 1556 | Color the Ball —— 树状数组
- 约瑟夫问题(Josephus Problem)
- drawableleft与文本一起居中显示
- [原创]`Constructor` has no propType for native prop `RCTView.maxHeight` of native type
- js中函数参数值传递和引用传递
- 自己动手写工具包——目录扫描器
- 语义性与元素名全称问题
- FreeCMS视频教程 敏感词管理
- Application——基本知识