POJ_2352_Stars

本文介绍了一种利用Binary Indexed Tree (BIT) 数据结构解决区间查询与更新问题的方法。通过具体的代码实现,展示了如何构建BIT并进行元素的添加与求和操作,特别适用于离线处理大量查询与更新操作的场景。
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<bitset>
#include<map>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::map;
class BIT
{
private:
	vector<int>tree;
public:
	BIT(){}
	BIT(const int &size)
	{
		tree.resize(size);
	}
	int lowbit(const int &x)
	{
		return x&-x;
	}
	void add(int i,const int &value)
	{
		for (; i < tree.size(); i += lowbit(i))
		{
			tree[i] += value;
		}
	}
	int sum(int i)
	{
		int ret = 0;
		for (; i; i -= lowbit(i))
		{
			ret += tree[i];
		}
		return ret;
	}
};
int main()
{
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (scanf("%d",&n)!=EOF)
	{
		BIT value(33000);
		vector<int>star(n);
		for (int i = 0; i < n; i++)
		{
			int x, y; scanf("%d%d",&x,&y);
			star[value.sum(x+1)]++;
			value.add(x + 1, 1);
		}
		for (int i = 0; i < n; i++)
		{
			printf("%d\n", star[i]);
		}
	}
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值