转载请注明出处:忆梦http://blog.csdn.net/yimeng2013/article/details/13510899
类型 : 单点查询,区间修改
题目大意:给定区间[1,N],初始时区间的每个数都是0,对其进行多次操作,每次给出两个数a和b,要求你对在区间[a,b]的数全部加1,在操作结束后输出每个数的值
#include<stdio.h>
#include<string.h>
#define N 100010
int c[N];
int n;
int lowbit(int x)
{
return x & (-x);
}
int sum (int x)
{
int ret = 0;
while(x > 0)
{
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void add(int x, int d)
{
while(x <= n)
{
c[x] += d;
x += lowbit(x);
}
}
int main ()
{
while(scanf("%d", &n) && n != 0)
{
memset(c, 0, sizeof(c));
int a, b;
for(int i = 1; i <= n; i++)
{
scanf("%d %d", &a, &b);
add(a,1);
add(b+1,-1);
}
for(int i = 1; i < n; i++)
printf("%d ", sum(i));
printf("%d\n", sum(n));
}
return 0;
}