树状数组第一题: POJ 2352 Stars

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. 

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. 

You are to write a program that will count the amounts of the stars of each level on a given map.

#include
#include
using namespace std;
int n,f[100005];
int lowbit(int x){
return x&(-x);
}
int sum(int l,int r){//求和
int ans=0;
for(int i=r;i>0;i-=lowbit(i))ans+=f[i];
for(int i=l;i>0;i-=lowbit(i))ans-=f[i];
return ans;
}
void add(int x){//进行修改操作
for(int i=x;i<=65536;i+=lowbit(i))f[i]++;
}
int x,y,vis[100005];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    scanf("%d%d",&x,&y);
    vis[sum(0,++x)]++;
    add(x);
    }
for(int i=0;i printf("%d\n",vis[i]);
}
}

你可能感兴趣的:(树状数组)