• HDU-1541 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.
    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

    题意:有n颗星星的坐标,按从左到右从下到上的顺序给出每个星星的坐标,现在规定,每颗星星都有等级,每颗星星的等级是其左下方区域内存在的星星数量。输出从0到N-1等级的每个星星的数量。

    思路:首先因为给出的星星顺序是有序的,也就是说按从下到上从左到右的顺序先被输入进来的星星肯定不会再有新的星星加入到其左下方的区域内,当一个星星被输入后,那么他的等级就已经是确定了的。因为后面输入的星星都是在其右上方的。因此,每次输入一个星星,记录其位置,然后统计在这个位置之前的星星有多少颗,此处Y坐标完全没用,因为从下到上,已经输入的全在自己下面,找到符合要求的X坐标的数量就好,此处的统计即用树状数组快速求前缀和。树状数组下标表示X坐标,存储的值表示每个坐标上有颗星星,前缀和即小于这个坐标的星星数量。
    注意坐标从0开始,直接更新会让树状数组死循环,要从1开始,所有坐标+1即可

    #include<stdio.h>
    #include<string.h>
    #define LL long long
    using namespace std;
    const int maxn=4e4;
    int tree[maxn];
    int n;
    int lowbit(int x)
    {
        return x&(-x);
    }
    void update(int p,int x)
    {
        while(p<=maxn)
        {
            tree[p]+=x;
            p+=lowbit(p);
        }
    }
    int sum(int x)
    {
        int sum=0;
        while(x>0)
        {
            sum+=tree[x];
            x-=lowbit(x);
        }
        return sum;
    }
    int num[maxn];
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            int x,y;
            memset(tree,0,sizeof tree);
            memset(num,0,sizeof num);
            for(int i=0;i<n;i++)
            {
                scanf("%d%d",&x,&y);
                num[sum(x+1)]++;
                update(x+1,1);
            }
            for(int i=0;i<n;i++)
            {
                printf("%d
    ",num[i]);
            }
        }
    }
    
  • 相关阅读:
    ajax _02【XML响应,post请求】
    ajax_01【httpRequest.responseText】
    方法的定义【js函数】
    Promise基本用法
    promise
    筛选(1)
    ng-cli 中HTTP请求思路(1) (接口请求处理)
    PHP占用CPU过高几种思路
    关于tcpdump的那点事~
    虚拟机固定IP那点事
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135777.html
Copyright © 2020-2023  润新知