Hdu1556 Color the ball [分块][树状数组]
2016-10-28 19:15
344 查看
Hdu1556 Color the ball
Problem DescriptionN个气球排成一排,从左到右依次编号为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
【题解】
讲真,这道题,线段树,树状数组都能做。。
不过,现在爱上分块了llll
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define cle(x) memset(x,0,sizeof(x)) const int N=100050; int block,n,m,belong ,add[335]; int a ,l[335],r[335]; inline int readin(){ int res=0,f=1; char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){ res=res*10+ch-'0'; ch=getchar(); } return res*f; } void updata(int x,int y){ if(belong[x]==belong[y]) for(int i = x; i <= y; i++)a[i]++; else { for(int i = x; i<=r[belong[x]];i++)a[i]++; for(int i = l[belong[y]];i<=y;i++)a[i]++; } for(int i=belong[x]+1;i<belong[y];i++) add[i]++; } int main(){ freopen("hdu.in","r",stdin); freopen("hdu.out","w",stdout); while((scanf("%d",&n))==1&&n){ cle(belong),cle(l),cle(r),cle(a),cle(add); int x=sqrt(n); if(n%x)block=n/x+1; else block=n/x; for(int i = 1; i<= n; i++) belong[i]=(i-1)/x+1; for(int i = 1; i<=block;i++) l[i]=(i-1)*x+1,r[i]=i*x; r[block]=n; for(int i = 1; i <= n; i++){ int x=readin(),y=readin(); updata(x,y); } printf("%d",a[1]+add[belong[1]]); for(int i = 2; i <= n; i++) printf(" %d",a[i]+add[belong[i]]); printf("\n"); } return 0; }
填坑,,树状数组:
;;;;#include<iostream> #include<cstdio> #include<cstring> using namespace std; int a[100005],n; //向下修改,向上求和(区间),向上修改,向下求和(点) //HDU 1556,树状数组修改区间,【3,5】,修改向下修改,求和向上求和,以他每次所管的都要+/-1 int lowbit(int x) { return x&-x; } int getsum(int i) { int sum=0; while(i<=n) { sum+=a[i]; i+=lowbit(i); } return sum; }//向上求和 void update(int i,int x) { while(i>0) { a[i]+=x; i-=lowbit(i); } }//向下修改 int main() { freopen("color.in","r",stdin); freopen("color.out","w",stdout); while((scanf("%d",&n))==1&&n) { memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); update(x-1,-1); update(y,1); } for(int i=1;i<=n-1;i++) { int sum=getsum(i); printf("%d ",sum); } printf("%d\n",getsum(n)); } return 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 (树状数组)
- 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【树状数组】解题报告
- HDU 1556--Color the ball【树状数组】
- hdoj 1556 Color the ball(树状数组)
- hdu 1556 Color the ball (树状数组)
- HDU 1566 Color the ball(树状数组)