hdu1556(区间更新,单点求值)

http://acm.hdu.edu.cn/showproblem.php?pid=1556

代码:

#include<iostream>

int n,c[100003];

int lowbit(int x)

{

	return x&(-x);

}

int sum(int x)

{

	int sum=0;

	while(x>0)

	{

		sum+=c[x];

		x-=lowbit(x);

	}

	return sum;

}

void inster(int x,int i)

{

	while(x<=n)

	{

		c[x]+=i;

		x+=lowbit(x);

	}

}

int main()

{

	int a,b,i;

	while(scanf("%d",&n)>0&&n)

	{

		memset(c,0,sizeof(c));

		for(i=0;i<n;i++)

		{

			scanf("%d%d",&a,&b);

			inster(b+1,-1);

			inster(a,1);

		}

		for(i=1;i<n;i++)

			printf("%d ",sum(i));

		printf("%d\n",sum(n));

	}

	return 0;

}

 

你可能感兴趣的:(HDU)