#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后再进行操作,否则会出错。
POJ2352(树状数组)
最新推荐文章于 2021-08-05 18:44:22 发布