Stars HDU - 1541(树状数组的典型应用)

star

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
思路讲解:
刚开始写这一道 题的时候 我怎么也没有想到 竟然是用树状数组 来写的 , 而且 看了 好几个 博客上的 讲解 ,这道题 还是 树状数组 的 一道 典型的 题,好啦 废话不多说啦 ,进入正题。先分析一下题意,这道题 先给你拉 所有星星的坐标(二维的) 然后 计算出 每一个星星对应的 等级,最后 将每一种 等级 对应星星的数量 依次输出出来,星星等级的求解规则也告诉你啦,一个星星的等级 = 他左下方 星星的数量,不包括本身,题意很简单,有的朋友可能会想到 某种方法,用类似于 “数塔”一类的 方法求解 ,但是 题目的数据量很大的,而且还是多实例(我都没看出来竟然是多实例。。)竟然这道题使用树状数组做的 ,那我们就从树状数组的功能来想,树状数组 大多数是用来求解 区间和 ,单点更新, 对于区间更新 和 求解区间最值问题 我认为 还是用线状树来做比较方便,树状数组求解区间和中 他用了一个数组 来存储 一些数, 这些数 是一些 数的和 ,而且是相邻的一个数或者是多个数的和,其中用到啦 lowbite 最低位 简单来数就是 一个数对应二进制数 中最后一个‘1’对应十进制的值,题目中数据给你的 比较 好 都是按照x ,y,递增的 顺序给你的, 所以就不用在把他们排序啦,竟然要用树状数组 来写,那么 就要把 二维坐标转变为一维的,应为数据是一行一行 的给你的, 那么就只用 x 坐标就行啦,先把 c【N】数组(树状数组所用到的数组)初始化为 0, 然后 第一行的第一个 星星的坐标输入,不管纵坐标,只用横坐标,而且 要记住 横坐标的只要 “x++”(,这是因为 x 有可能等于零,所以 要把 所有的星星向右移) 然后 查找 1 =》x 之间 星星的数量 , (此时查找的不包含该星星,因为该星星还没有进入数组 c呢) 然后 应为 进入了一个星星 那么 后面的星星的等级都要加一的 于是 单点更新 x =》32000 另外要用一个 数组来存储 每一等级星星的数量,左后输出即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 32003
using namespace std;
int c[32007], num[32006];
void Update(int pos, int num)
{
    while(pos <= N)
    {
        c[pos] += num;
        pos += pos & (-pos);
    }
}

int Sum(int pos)
{
    int ans = 0;
    while(pos >= 1)
    {
        ans += c[pos];
        pos -= pos & (-pos);
    }
    return ans;
}

int main()
{
    int x, y;
    int n;

    while(scanf("%d", &n) != EOF)
    {
        memset(c, 0, sizeof(c));
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &x, &y);
            x++;
            num[Sum(x)]++;
            Update(x, 1);
        }

        for(int j = 0; j < n;j++)
            printf("%d\n", num[j]);
    }


    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值