您的位置:首页 > 其它

POJ 2352 解题报告

2014-11-18 11:08 190 查看
还是线段树。题目中给的数据已经按照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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: