• Stars(树状数组)


    http://acm.hdu.edu.cn/showproblem.php?pid=1541

    Stars

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5993    Accepted Submission(s): 2384


    Problem 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
     
    题解:这个题,一定要注意读题,首先,它给出的是已经按y和x 排好序的输入,所以要是采用输入一个点就处理一个点的话,肯定不可能在处理完后再输入当前点左下角的点,所以直接输入后直接统计其左下角的点的个数即可,所以输入一个处理一个即可,然后用这个点更新后面的每个点的level+1即可,而且这样就可以不用考虑y了,直接建立一维的数组x就可以了
    特别注意的是:树状数组不能处理标号为0的点,所以讲所有的点的标号+1即可
    所以以后见到树状数组编号从0 开始的情况都将标号依次+1 即可
    下面是代码
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 
     7 using namespace std;
     8 
     9 int n;
    10 const int N=16000;
    11 const int M=33000;
    12 
    13 int a[M],c[N];
    14 
    15 int lowbit(int t)
    16 {
    17     return t&(-t);
    18 }
    19 
    20 void insert(int t,int d)
    21 {
    22     while (t<=M)
    23     {
    24         a[t]+=d;
    25         t+=lowbit(t);
    26     }
    27 }
    28 
    29 int getsum(int t)
    30 {
    31     int zs=0;
    32     while (t>0)
    33     {
    34         zs+=a[t];
    35         t-=lowbit(t);
    36     }
    37     return zs;
    38 }
    39 
    40 int main()
    41 {
    42     int i,x,y,p,n;
    43     while (scanf("%d",&n)!=EOF)
    44     {
    45         memset(a,0,sizeof(a));
    46         memset(c,0,sizeof(c));
    47         for (i=1;i<=n;i++)
    48 
    49         {
    50             scanf("%d%d",&x,&y);
    51             insert(x+1,1);
    52             p=getsum(x+1)-1;
    53             c[p]++;
    54         }
    55         for (i=0;i<n;i++) printf("%d
    ",c[i]);
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    1. 马尔科夫决策过程
    梯度下降法、随机梯度下降法、小批量梯度下降法
    计算曲线与坐标轴的面积
    鼠标进入,显示div
    codewar
    Django firstDay
    C#学习之关于lock
    winfrom 界面中表格数据修改及刷新(DEV)
    SVN 分支合并 版本控制
    wpf 绑定非元素对象
  • 原文地址:https://www.cnblogs.com/shanyr/p/4694212.html
Copyright © 2020-2023  润新知