且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HDU 1541 树状数组

更新时间:2022-08-13 16:44:34

题目意思就是说,给你一张star maps,每个star有自己的level,level是这样计算的:(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.)统计这个star左下角star的个数,就是这个star的level。现在要你总计图中level从0到N-1的star分别有多少个。

如入是按照X Y Y的递增顺序输入的 Y相同时按照X的递增顺序 所以只要求X之前小于等于X的星星的个数就可以了 也就是按照X的值建树 

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 32005
int tree[maxn];
int n;
int ans[15005];
inline int Lowbit(int x)
{
    return x & (-x);
}
inline void Update(int pos, int val)
{
    while (pos <= maxn)
    {
        tree[pos] += val;
        pos += Lowbit(pos);
    }
}
inline int Query(int x)
{
    int sum = 0;
    while (x > 0)
    {
        sum += tree[x];
        x -= Lowbit(x);
    }
    return sum;
}
int main()
{
    int x,y;
    while(~scanf("%d",&n))
    {
        memset(tree,0,sizeof(tree));
        memset(ans,0,sizeof(ans));
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            ans[Query(x+1)]++;
            Update(x+1,1);
        }
        for(int i=0; i<n; i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}