树状数组——pku2352

题意:统计左下方的星星数
容易出现TLE的情况
因为 0<=X,Y<=32000 
x=0时,因为0&(-0)=0,所以会在树状数组里出现死循环
解决办法是每个x++;
由于y是升序的,故不用考虑,就统计X即可
View Code
#include<cstdio>
#include
<iostream>
using namespace std;

int n;
int tree[50009];
int add[50009];
int a[50009];

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

void updata(int x,int c)
{
int i;
for(i=x;i<=n;i+=lowbit(i))
{
tree[i]
+=c;
}
}

int getsum(int x)
{
int i;
int temp=0;
for(i=x;i>=1;i-=lowbit(i))
{
temp
+=tree[i];
}
return temp;
}

int main()
{
int t;
scanf(
"%d",&t);
{
n
=0;
int i;
for(i=0;i<t;i++)
{
int b;
scanf(
"%d%d",&a[i],&b);
a[i]
++;//为了防止在树状数组中产生0结点的更新
if(n<a[i])
n
=a[i];
}

for(i=0;i<=n;i++)
{
tree[i]
=0;
add[i]
=0;
}
for(i=0;i<t;i++)
{
int min=getsum(a[i]);
add[min]
++;
updata(a[i],
1);
}
for(i=0;i<t;i++)
{
printf(
"%d\n",add[i]);
}
}
return 0;
}

  

原文地址:https://www.cnblogs.com/huhuuu/p/2107648.html