您的位置:首页 > 其它

POJ2352(树状数组)

2013-07-09 21:07 211 查看
#include <iostream>
using namespace std;

int c[32001]={0};

int lowbit(int x)
{
return x&(-x);
}

int getsum(int x)
{
int total=0;
while (x>0)
{
total+=c[x];
x-=lowbit(x);
}
return total;
}

void update(int index)
{
while (index<=32001)
{
c[index]+=1;
index+=lowbit(index);
}
}

int main()
{
int T;
int x,y;
int hash[15000]={0};
scanf("%d",&T);
int n=T;
while (T--)
{
scanf("%d%d",&x,&y);
update(x+1);
hash[getsum(x+1)-1]++;
}
int i;
for (i=0;i<=n-1;i++)
{
printf("%d\n",hash[i]);
}
return 0;
}
特别注意: 坐标是从(0,0)开始的而一般树状数组的下标是从1开始的,将坐标+1后再进行操作,否则会出错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: