poj2352 Stars(树状数组)

问题要求统计左下角(x,y均小于当前star的star)有0颗星星、1颗星星.……n-1颗星星的星星个数,顺序输出。题目中给的数据是按y从小到大,所以对于star i(xi,yi),可能在它左下角的star(y<yi)肯定已经出现过了,用树状数组统计star i前有多少颗star 满足x<xi,累加就行了。star坐标可能有0,记得+1.

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define DEBUG
#define LL long long
#define maxn 33000

using namespace std;

int fin[maxn/2];
int tree[maxn];

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

void update(int pos,int num,int n)
{
	while(pos<=n)
	{
		tree[pos]+=num;
		pos+=lowbit(pos);
	}
}
int getsum(int en)
{
	int sum=0;
	while(en>0)
	{
		sum+=tree[en];
		en-=lowbit(en);
	}
	return sum;
}
int  main()
{
	int n;
	scanf("%d",&n);
	  memset(tree,0,sizeof(tree));
	for(int i=0;i<n;++i)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		fin[getsum(x+1)]++;
		update(x+1,1,maxn);
	}
	for(int i=0;i<n;++i)
	{
		printf("%d\n",fin[i]);
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值