hdu1541 Stars 树状数组

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541

题目大意就是统计其左上位置的星星的个数

由于y已经按升序排列,因此只用按照x坐标生成一维树状数组即可

比较简单的题目:

代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7 const int max_n=15010;
 8 const int max_c=32010;
 9 int c[max_c];
10 int cnt[max_n];
11 int lowbit(int x)
12 {
13   return x&(-x);
14 }
15 void add(int i,int val)
16 {
17    while(i<=max_c)
18    {
19       c[i]+=val;
20       i+=lowbit(i);
21    }
22 }
23 int sum(int i)
24 {
25     int s=0;
26     while(i>0)
27     {
28       s+=c[i];
29       i-=lowbit(i);
30     }
31     return s;
32 }
33 int main()
34 {
35    int n;
36    int x,y;
37    while(scanf("%d",&n)!=EOF)
38    {
39      memset(c,0,sizeof(c));
40      memset(cnt,0,sizeof(cnt));
41      for(int i=0;i<n;i++)
42      {
43         scanf("%d%d",&x,&y);
44          add(x+1,1);
45          int tmp=sum(x+1);
46          cnt[tmp-1]++;
47      }
48 
49      for(int i=0;i<n;i++)
50       cout<<cnt[i]<<endl;
51    }
52    return 0;
53 }
原文地址:https://www.cnblogs.com/xiaozhuyang/p/hdu1541.html