Ural P1028 星星stars【树状数组】

在这里插入图片描述

题目大意

天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标。如果一个星星的左下方(包含正左和正下)有k颗星星,就说这颗星星是k级的。给定星星的位置,输出各级星星的数目。

思路

树状数组模板题
因为y轴已经是升序
所以直接对x进行树状数组处理即可
就是模板

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

int c[100010],ans[100010];
int n,x,y;

int lowbit(int x)
{
    
    
    return x&(-x);
}
int find(int x)    //统计
{
    
    
	int ans=0;
	for(; x; x-=lowbit(x))
	   ans+=c[x];
	return ans;
}
void query(int x)  //修改
{
    
    
	for(; x<=32001; x+=lowbit(x))
	   c[x]+=1;
}
int main()
{
    
    
    cin>>n;
    for(int i=1; i<=n; i++)
     {
    
    
       scanf("%d%d",&x,&y);
       x++;
       ans[find(x)]++;
       query(x);
     }
    for(int i=0; i<=n-1; i++)
       cout<<ans[i]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Jackma_mayichao/article/details/108063660