POJ 2352 解题报告

还是线段树。题目中给的数据已经按照y值排好了序,那么问题就是看前面的输入中x值不大于当前输入x值的有多少个。这样的话可以根据x值建立一个线段树,边查边插入。线段树的值的范围是x的范围,即0~32000。

2352Accepted1248K672MSG++1531B
/* 
ID: thestor1 
LANG: C++ 
TASK: poj2352 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

const int MAXM = 32000;

int query(std::vector<int> &segmentTree, int left, int right, int index, const int x)
{
	if (left > x)
	{
		return 0;
	}

	if (right <= x)
	{
		return segmentTree[index];
	}
	
	int mid = left + ((right - left) >> 1);
	return query(segmentTree, left, mid, 2 * index + 1, x) + query(segmentTree, mid + 1, right, 2 * index + 2, x);
}

void update(std::vector<int> &segmentTree, int left, int right, int index, const int x)
{
	if (left > x || right < x)
	{
		return;
	}

	segmentTree[index]++;
	
	if (left != right)
	{
		int mid = left + ((right - left) >> 1);
		update(segmentTree, left, mid, 2 * index + 1, x);
		update(segmentTree, mid + 1, right, 2 * index + 2, x);	
	}
}


int main()
{
	std::ios::sync_with_stdio(false);
	int N;
	cin >> N;
	std::vector<int> segmentTree(4 * MAXM, 0);
	std::vector<int> cnt(N, 0);
	for (int i = 0; i < N; ++i)
	{
		int x, y;
		cin >> x >> y;
		int pos = query(segmentTree, 0, MAXM, 0, x);
		assert (0 <= pos && pos <= N - 1);
		cnt[pos]++;
		update(segmentTree, 0, MAXM, 0, x);
	}
	for (int i = 0; i < N; ++i)
	{
		cout << cnt[i] << endl;
	}
	return 0;  
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值