hdu 1556 Color the ball 树状数组

简介:

    最基本的树状数组,成段更新,感觉只要构造的时候保证两端平衡,即对后面非更新地方无影响即可,所以这题是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));
    }
}


目录
相关文章
HDU 1506 Largest Rectangle in a Histogram(单调栈)
HDU 1506 Largest Rectangle in a Histogram(单调栈)
|
测试技术
POJ3687---Labeling Balls
POJ3687---Labeling Balls
POJ3687---Labeling Balls
CodeForces - 1469B Red and Blue (前缀和)
CodeForces - 1469B Red and Blue (前缀和)
76 0
hdu 1312 Red and Black(BFS)
hdu 1312 Red and Black(BFS)
121 0
|
机器学习/深度学习
HDOJ/HDU 1556 Color the ball(树状数组)
HDOJ/HDU 1556 Color the ball(树状数组)
82 0
|
Java 机器学习/深度学习
HDU 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18404    Accepted Submission(s...
811 0