hdu 1556 Color the ball(修改区间)
2013-07-10 15:18
459 查看
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5442 Accepted Submission(s): 2913
[align=left]Problem Description[/align]
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
[align=left]Input[/align]
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
[align=left]Output[/align]
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
[align=left]Sample Input[/align]
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
[align=left]Sample Output[/align]
1 1 1 3 2 1
题意:更新区间,查询点
思路:这里采用向下修改,向上统计的方法。(当然也可以采用向上修改,向下统计的方法)
AC代码:
#include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <vector> #include <cstdlib> #include <iostream> #define max2(a,b) ((a) > (b) ? (a) : (b)) #define min2(a,b) ((a) < (b) ? (a) : (b)) using namespace std; const int N=100005; int n; int c ; int lowbit(int x) { return x&(-x); } void modify(int x,int d) //向下修改 { for(int i=x;i>0;i-=lowbit(i)) c[i]+=d; } int sum(int x) //向上统计 { int ret=0; for(int i=x;i<=n;i+=lowbit(i)) ret+=c[i]; return ret; } int main() { int a,b; while(scanf("%d",&n),n) { memset(c,0,sizeof(c)); for(int i=0;i<n;i++) { scanf("%d%d",&a,&b);//1 1 2 2 3 3 modify(b,1); //向下修改 modify(a-1,-1); //把不必修改的区间还原 } for(int i=1;i<n;i++) printf("%d ",sum(i)); //向上统计 printf("%d\n",sum(n)); } return 0; }
相关文章推荐
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)
- 【线段树II:区间修改+点查询】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 (数状数组 区间更新)
- HDU 1556-Color the ball(线段树区间更新)
- hdu 1556 Color the ball (树状数组解决区间覆盖+单点查询)
- HDU 1556 Color the ball【线段树区间更新,一次查询+数组模拟】