hdu1556 简单的树状数组

题意:
给你temp个数,用a,b表示temp次更新[a, b]区间,求出temp次后这temp个数的值.
#include<stdio.h>
#include<string.h>
int ans[110000];
int temp;
int lowBit(int x)
{
    return x&-x;
}
int getSum(int x)
{
    int sum=0;
    while(x<=temp)
    {
        sum+=ans[x];
        x+=lowBit(x);
    }
    return sum;
}
void upDate(int x,int y)
{
    while(x>0)
    {
        ans[x]+=y;
        x-=lowBit(x);
    }
}
int main()
{
    int a,b,c;
    while(~scanf("%d",&temp),temp)
    {
        memset(ans,0,sizeof(ans));
        for(int i=0;i<temp;i++)
        {
            scanf("%d%d",&a,&b);
            upDate(a-1,-1);
            upDate(b,1);
        }
        for(int i=1;i<=temp;i++)
        {
            printf("%d",getSum(i));
            if(i<temp) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(hdu1556 简单的树状数组)