1265. Counting the Stars

Insert picture description here
Insert picture description here
Idea:
Insert picture description here
Because the title stars are given in increasing order of y-coordinates, and the same y-coordinates are given in increasing order of x-coordinates, so you only need to consider how many stars each abscissa x corresponds to, and then find the prefix of 0~x And you can get the number code of each level of stars
:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;

const int N = 32010;
int n;
int level[N],tr[N];//tr[i]表示该横坐标下有多少个星星,level[]表示第i级星星的数目

int lowbit(int x)
{
    
    
    return x & -x;
}

void add(int x)
{
    
    
    for(int i = x;i < N;i += lowbit(i))
    {
    
    
        tr[i]++;//放入一个星星,则后续所有相关的数目都加一
    }
}

int sum(int x)
{
    
    
    int res = 0;
    for(int i = x;i;i -= lowbit(i))
    {
    
    
        res += tr[i];
    }
    return res;
}

int main()
{
    
    
    int x,y;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
    
    
        scanf("%d %d",&x,&y);
        x++;//树状数组初始应该为1
        level[sum(x)]++;//第x级星星的数量
        add(x);//放入该星星
    }
    for(int i = 0;i < n;i++)
    printf("%d\n",level[i]);
    
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45812180/article/details/114797169