Stars HDU 1541
题意:每个星星有一个等级,该等级为它左下角的星星的个数。星星的坐标按Y坐标为第一关键字X坐标为第二关键字的递增的顺序给出。最后输出0~n-1等级的星星一共有多少个。
题解:树状数组。
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 const int maxn=32005; 6 int a[maxn],sum[maxn]; 7 void Updata(int p,int c){ 8 while(p<=32005){ 9 a[p]+=c; 10 p+=p&(-p); 11 } 12 } 13 int SuM(int x){ 14 int total=0; 15 while(x>0){ 16 total+=a[x]; 17 x-=x&(-x); 18 } 19 return total; 20 } 21 int main() 22 { 23 int n,x,y,i; 24 while(scanf("%d",&n)!=EOF){ 25 memset(a,0,sizeof(a)); 26 memset(sum,0,sizeof(sum)); 27 for(i=0;i<n;i++){ 28 scanf("%d %d",&x,&y); 29 x++; 30 sum[SuM(x)]++; 31 Updata(x,1); 32 } 33 for(i=0;i<n;i++) 34 printf("%d\n",sum[i]); 35 } 36 return 0; 37 }
posted on 2012-10-13 18:57 Acmer_Roney 阅读(563) 评论(0) 编辑 收藏 举报