树状数组 -- PKU 2352 Stars

  
Stars
Time Limit: 1000MS
 
Memory Limit: 65536K
Total Submissions: 4508
 
Accepted: 1869
Description
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.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
解题方案
典型的树状数组问题。根据X坐标建立数组C,每个X坐标对应一个数组元素,记录X坐标为该坐标的点的个数,共有32000+1个元素。由于输入是按照Y的升序并且Y相同时X的升序排列的,因此不需要考虑Y坐标。输入一个X坐标时,求出当前X坐标小于等于输入X坐标(Y坐标由输入顺序可以保证)的点的个数(sum(x)),然后增加该X坐标对应数组元素的统计值。求和以及增加的操作均是通过树状数组完成,sum(x)求count(1)+count(2)+..count(x),增加统计值的操作是modify(x,1)。
 
看到这道题目的第一感觉就是使用二维树状数组,但是根据问题规模,这显然是不允许的。同时由于输入的有序性保证了任何新插入的X坐标都是在前面X坐标之上或同一水平线的,因此只需要记录X坐标,统计出现在每个位置前面的星星的个数。
 
这道题目刚开始做的时候,老是TLE。原来以为是输入输出问题,找了半天没有找到答案。后来把题再仔细看了一遍,发现我的测试用例中没有包括X坐标为0的点。一加上,就死循环了。树状数组要求数组下标从1开始,把X坐标做为数组下标肯定要出问题。解决方法是X坐标+1做为数组下标,同时树状数组的元素个数等于最大X坐标+1,这样X坐标等于0的情况就被cover到了。
Code:
#include <stdio.h>
#define MAXN 32001
#define MAXNUM 15000
static int count[MAXNUM];
static int C[MAXN+1];
static int size;
inline int lowbit(int x)
{
 return (x & -x);
}
inline void modify(int k, int val)
{
 while (k <= size)
    {
  C[k] += val;
        k += lowbit(k);
    }
}
inline int sum(int k)
{
 if (k == 0)
  return 0;
 int t = 0;
 while (k > 0)
    {
  t += C[k];
  k -= lowbit(k);
 }
 return t;
}
inline int maxFunc(int a, int b)
{
 return a>b?a:b;
}
inline int toIndex(int x)
{
 return x+1;
}

int main(int argc,char **argv)
{
 int i,N;
 int x[MAXNUM];
 int level;
 bool ascend = false;
 scanf("%d",&N);
 size = 0;
 for(i=0;i<N;i++)
 {
  scanf("%d %*d",&x[i]);
  size = maxFunc(size,x[i]+1);
 }
 for(i=0;i<N;i++)
 {
  level = sum(toIndex(x[i]));
  /*printf("x[%d]=%d,level=%d/n",i,x[i],level);*/
  count[level]++;
  modify(toIndex(x[i]),1);
  /*
  for(int j=0;j<=size;j++)
   printf("%d ",count[j]);
  printf("/n");
  */
 }
 for(i=0;i<N;i++)
 {
  printf("%d/n",count[i]);
 }
 return 0;
}
 
树状数组的典型实现参考文章<<树状数组入门>>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值