# Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25435    Accepted Submission(s): 12362

Problem Description

N个气球排成一排，从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了，你能帮他算出每个气球被涂过几次颜色吗？

Input

Output

Sample Input




31 12 23 331 11 21 30

Sample Output




1 1 13 2 1

这时候就可以想到什么算法是可以更改区间值得呢？树状数组，时间是logn。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<cmath>
#include<math.h>
using namespace std;

const int maxn=100000+10;
int c[maxn];
int n,a,b;

int lowbit(int x)
{
return x&(-x);
}
void update(int i,int value)
{
while(i>0)
{
c[i]+=value;
i-=lowbit(i);
}
}
int getSum(int i)
{
int sum=0;
while(i<=n)
{
sum+=c[i];
i+=lowbit(i);
}
return sum;
}

int main()
{
while(~scanf("%d",&n),n)
{
memset(c,0,sizeof(c));
for(int i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
update(b,1);//update(a,1)则update里面的i-=lowbit改为i+=lowbit且i<=n因为i-=lowbit是区间b到0递减的,
//而i+lowbit是从n到a递减。通常我还是喜欢第一种。要是变为i+=lowbit则getSum也要改为i>0且i-=lowbit.
update(a-1,-1);
}
for(int i=1; i<n; i++)
{
printf("%d ",getSum(i));
}
printf("%d\n",getSum(n));
}
return 0;
}