码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1556 【树状数组】(改段求点)

时间:2014-08-08 17:55:46      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   strong   for   div   amp   

#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 100050
using namespace std;

int b[maxn];
int n;
int lowbit(int x)
{
    return x&(-x);
}

void ADD(int x, int c)    //向下查询
{
    for (int i=x; i>0; i-=i&(-i)) 
    b[i] += c;
}
int SUM(int x)     //向上统计
{
    int s = 0;
    for (int i=x; i<=n; i+=i&(-i)) 
    s += b[i];
    return s;
}

int main()
{
    while(scanf("%d",&n)&&n)
    {
        int x,y;
        memset(b,0,sizeof b);
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            ADD(y,1);
            ADD(x-1,-1);
        }
        for(int i=1; i<n; i++)
        {
            printf("%d ",SUM(i));
        }
        printf("%d\n",SUM(n));
    }
    return 0;
}

 

心得体会

树状数组主要就分为3种类型。针对这3种类型,我已经转载了一篇文章,里面有3种类型的模板。

做题的时候,只要分清楚是考查哪种类型,然后套模板就可以了。

 

针对这道题,还有几点要注意的地方。

(1) 不要忘了对数组b进行初始化。

(2)输入   while(scanf("%d",&n)&&n)   的运用,既完成了输入,同时判断了n 是否为0.

(3)输出中的小细节。

       前n个数之间存在空格,最后一个数后面不输出空格,直接换行。

       注意细节~~~~~

HDU1556 【树状数组】(改段求点),布布扣,bubuko.com

HDU1556 【树状数组】(改段求点)

标签:style   blog   color   io   strong   for   div   amp   

原文地址:http://www.cnblogs.com/fightfor/p/3899621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!