poj 2352 树状数组 基本应用

题意:

给定N个星星的X和Y坐标,求出各个亮度的星星个数

亮度为,星星坐下侧,x和y均不大于当前星星的星星星个数

输入数据已经按照x递增,y递增顺序排好


解法:

标准的树状数组查询问题,每次插入一个星星,统计满足条件的“左下角”星星个数

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定义
const int  INF = 990000000;
const int maxn = 65010 ;
const int MAXN = maxn;
///全局变量 和 函数
int max(int a, int b)
{
	return a > b ? a : b;
}
int min(int a, int b)
{
	return a < b ? a : b;
}
//
// 区间最*大*值
const int maxlog = 20000;
//inline int lowbit(int x) { return x&(x^(x-1)); }
inline int lowbit(int x) { return x&-x; }

struct FenwickTree 
{
	int n;
	vector<int> C;
	
	void resize(int n) { this->n = n; C.resize(n); }
	void clear() { fill(C.begin(), C.end(), 0); }
	
	// 计算A[1]+A[2]+...+A[x] (x<=n)
	int sum(int x) {
		int ret = 0;
		while(x > 0) {
			ret += C[x]; x -= lowbit(x);
		}
		return ret;
	}
	
	// A[x] += d (1<=x<=n)
	void add(int x, int d) {
		while(x <= n) {
			C[x] += d; x += lowbit(x);
		}
	}
};
FenwickTree f;
int level[maxn];
//
int N;
int main()
{
	///变量定义
	int i, j;	
	while (scanf("%d", &N) != EOF)
	{
		memset(level, 0, sizeof(level));
		f.resize(maxn);//开大一些否则会RE
		f.clear();
		for (i = 0; i < N; i++)
		{
			int x, y;
			scanf("%d %d", &x, &y);
			x++;
			level[f.sum(x)]++;
			f.add(x, 1);
		}
		for (i = 0; i < N; i++)
			printf("%d\n", level[i]);
	}
	///结束
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值