hdu1556 Color the ball【树状数组 || 线段树 || 技巧*区间修改】
2017-08-02 13:47
471 查看
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22105 Accepted Submission(s): 10704
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,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
Author
8600
思路:
新生赛做过类似的题目,对【l,r】区间修改值,只需要l端加一,r+1端减一就好,最后正序相加输出;
树状数组写法,对于区间修改值,这是向上统计,向下查询;
比较喜欢这个修改方式,向上统计,向下查询;
线段树写法,异曲同工的操作:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 22105 Accepted Submission(s): 10704
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,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
Author
8600
思路:
新生赛做过类似的题目,对【l,r】区间修改值,只需要l端加一,r+1端减一就好,最后正序相加输出;
#include<cstdio> #include<cstring> #define max_n 100010 int a[max_n]; int main() { int n,l,r; while(scanf("%d",&n) && n) { memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { scanf("%d %d",&l,&r); a[l]++; a[r+1]--; } int ans=0; for(int i=1;i<=n;i++) { ans=ans+a[i]; if(i>1) printf(" "); printf("%d",ans); } printf("\n"); } return 0; }
树状数组写法,对于区间修改值,这是向上统计,向下查询;
#include<cstdio> #include<cstring> #define max_n 100010 int c[max_n]; int n,l,r; int lowbit(int x){ return x&(-x); } //向下查询,k是要更新的子节点,v是要修改的值 void updata(int k,int v){ while(k>0){ c[k]+=v; k-=lowbit(k); } } //向上统计每个区间被染色的次数 int query(int k){ int sum=0; while(k<=n){ sum+=c[k]; k+=lowbit(k); } return sum; } int main(){ while(scanf("%d",&n) && n){ memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ scanf("%d %d",&l,&r); updata(r,1); updata(l-1,-1); } for(int i=1;i<=n;i++){ if(i>1) printf(" "); printf("%d",query(i)); } printf("\n"); } return 0; }
比较喜欢这个修改方式,向上统计,向下查询;
#include<cstdio> #include<cstring> #define max_n 100010 int c[max_n]; int n,l,r; int lowbit(int x){ return x&(-x); } //向上查询,k是要更新的子节点,v是要修改的值 void updata(int k,int v){ while(k<=n){ c[k]+=v; k+=lowbit(k); } } //向下统计每个区间被染色的次数 int query(int k){ int sum=0; while(k>0){ sum+=c[k]; k-=lowbit(k); } return sum; } int main(){ while(scanf("%d",&n) && n){ memset(c,0,sizeof(c)); for(int i=1;i<=n;i++){ scanf("%d %d",&l,&r); updata(r+1,-1); updata(l,1); } for(int i=1;i<=n;i++){ if(i>1) printf(" "); printf("%d",query(i)); } printf("\n"); } return 0; }
线段树写法,异曲同工的操作:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define max_n 1000010 using namespace std; typedef long long LL; struct node{ int l; int r; int sum; }s[max_n]; void pushup(int root){ s[root].sum=s[root<<1].sum+s[root<<1|1].sum; } void build(int root,int L,int R){ s[root].l=L; s[root].r=R; if(L==R){ s[root].sum=0; return; } int mid=(L+R)>>1; build(root<<1,L,mid); build(root<<1|1,mid+1,R); pushup(root); } void updata(int root,int x,int y){ if(s[root].l==s[root].r){ s[root].sum+=y; return; } int mid=(s[root].l+s[root].r)>>1; if(mid>=x) updata(root<<1,x,y); else updata(root<<1|1,x,y); pushup(root); } int query(int root,int L,int R){ if(s[root].l==L && s[root].r==R){ return s[root].sum; } int mid=(s[root].l+s[root].r)>>1; if(mid>=R) return query(root<<1,L,R); else if(mid<L) return query(root<<1|1,L,R); else return (query(root<<1,L,mid)+query(root<<1|1,mid+1,R)); } int main(){ int n,l,r; while(scanf("%d",&n) && n){ build(1,1,n+1); for(int i=1;i<=n;i++){ scanf("%d %d",&l,&r); updata(1,r+1,-1); updata(1,l,1); } for(int i=1;i<=n;i++){ if(i>1) printf(" "); printf("%d",query(1,1,i)); } printf("\n"); } return 0; }
相关文章推荐
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- HDU1556 Color the ball(树状数组BIT 区间修改单点询问)
- hdu1556 Color the ball(线段树区间修改)
- hdu 1556 Color the ball 【树状数组:区间修改、单点求值】
- HDU 1556 Color the ball 很典型的更新区间查找点的题(线段树树状数组两种解法)
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU1556 Color the ball【树状数组】【区间更新】
- 【树状数组 区间修改,单点求值】1556 Color the ball
- 【树状数组-区间更新】hdu1556 Color the ball
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)
- hdoj 1556 Color the ball 【线段树 + lazy区间更新】 【树状数组】
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- HDU1556 color the ball(树状数组)向下查询,向上统计
- hdu1556 Color the ball (树状数组)
- [树状数组]HDU1556 Color the ball
- HDU 1556 Color the ball【线段树区间更新,一次查询+数组模拟】