hdu1556 Color the ball【树状数组 || 线段树 || 技巧*区间修改】
来源:互联网 发布:大屏数据可视化 编辑:程序博客网 时间:2024/05/22 14:38
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端减一就好,最后正序相加输出;
#include<cstdio>#include<cstring>#define max_n 100010int 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 100010int 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 100010int 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 1000010using 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;}
阅读全文
2 0
- hdu1556 Color the ball【树状数组 || 线段树 || 技巧*区间修改】
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- hdu1556 Color the ball(线段树区间修改)
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- HDU1556 Color the ball【树状数组】【区间更新】
- 【树状数组-区间更新】hdu1556 Color the ball
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- HDU1556 Color the ball 树状数组 修改区间值 查找单点值
- HDU1556 Color the ball(树状数组BIT 区间修改单点询问)
- HDU1556:Color the ball(线段树区间更新)
- hdu1556 Color the ball(线段树区间更新)
- hdu1556 Color the ball(线段树区间更新)
- HDU1556 Color the ball(线段树区间更新)
- hdu1556 Color the ball 线段树区间染色问题
- HDU1556:Color the ball(线段树区间更新)
- hdu1556 Color the ball (区间修改)
- hdu1556 Color the ball (树状数组)
- 资讯精选|潜在杀人凶器?VR设备、无人机、平衡车的死穴在“声音”!
- LCT模板(link-cut-tree)
- Selenium环境的安装搭建
- 希尔排序和shell之希尔排序
- HTTP系列之HTTP概念解释
- hdu1556 Color the ball【树状数组 || 线段树 || 技巧*区间修改】
- eclipse4.x下安装SVN时,选择SVN CONNECTOR时报错解决方法!!!
- 设计模式之代理模式
- 自定义控件(8)处理标签间的内容ParseChildrenAttribute
- ext中根据grid中当前row的内容设置row的背景色
- JAVA设计模式——单例模式
- 日语词汇辨析:以来と以降と以後
- 《Visual Basic 程序设计》——>过程
- 隐马尔科夫模型(HMM)