码迷,mamicode.com
首页 > 编程语言 > 详细

树状数组hd1541

时间:2015-10-25 20:40:53      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <cstring>
using namespace std;
int t, x, y, a[32010], b[32010];
#define  N 32010
int lowbit(int x)
{
    return x&(-x);//返回x所在节点,很巧妙  eg:d[1]=a1;d[2]=a1+a2;d[3]=a3;d[4]=a1+a2+a3+a4;
}
void add(int x, int y)//所有包含a[x]的区间都加a[x]
{
    while(x<N)
    {
        b[x]+=y;
        x+=lowbit(x);//返回x所在节点的父节点
    }
}
int getsum(int x)//计算前x个数组的值
{
    int sum=0;
    while(x>0)
    {
        sum+=b[x];
        x-=lowbit(x);//返回x所在节点的子节点
    }
    return sum;
}
int main()
{
    while(cin>>t)
    {
        memset(a,0,sizeof(a));
        memset(b, 0, sizeof(b));
        for(int i=0; i<t; i++)
        {
            cin>>x>>y;
            x+=1;
            a[getsum(x)]++;
            add(x,1);
        }
        for(int i=0; i<t; i++)
            cout<<a[i]<<endl;
    }
    return 0;
}
//http://acm.hdu.edu.cn/showproblem.php?pid=1541

树状数组hd1541

标签:

原文地址:http://www.cnblogs.com/luomi/p/4909342.html

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