现在的位置: 首页 > 综合 > 正文

hdu 1556 Color the ball 树状数组

2013年09月04日 ⁄ 综合 ⁄ 共 812字 ⁄ 字号 评论关闭

    最基本的树状数组,成段更新,感觉只要构造的时候保证两端平衡,即对后面非更新地方无影响即可,所以这题是a++ (b+1)--

   温习了下快速IO,速度果然提升1倍

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int c[100005],n;
inline int low(int x)
{
    return x&-x;
}
int update(int pos,int num)
{
    while(pos<=n)
    {
        c[pos]+=num;
        pos+=low(pos);
    }
}
int query(int pos)
{
    int ans=0;
    while(pos>0)
    {
        ans+=c[pos];
        pos-=low(pos);
    }
    return ans;
}
void input(int &n)
{
    n=0;
    int ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')
        n=(n<<3)+(n<<1)+ch-'0',ch=getchar();
}
int main()
{
    while(~scanf("%d",&n),n)
    {
        memset(c,0,sizeof(c));
        int i,a,b;
        for(i=0;i<n;i++)
        {
            input(a);input(b);
            update(a,1);update(b+1,-1);
        }
        for(i=1;i<n;i++)
          printf("%d ",query(i));
        printf("%d\n",query(n));
    }
}

抱歉!评论已关闭.