POJ 2352 解题报告
2014-11-18 11:08
190 查看
还是线段树。题目中给的数据已经按照y值排好了序,那么问题就是看前面的输入中x值不大于当前输入x值的有多少个。这样的话可以根据x值建立一个线段树,边查边插入。线段树的值的范围是x的范围,即0~32000。
2352 | Accepted | 1248K | 672MS | G++ | 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; }
相关文章推荐
- [解题报告]POJ2352 Stars 树状数组
- 【原】 POJ 2352 Stars 树状数组 解题报告
- POJ 2352 stars【树状数组】解题报告
- POJ - 2352 Stars解题报告(树状数组求二维区域和)
- POJ 1702 解题报告——Eva's Balance:用砝码为物体称重
- openoj的一个小比赛(J题解题报告)poj1703(并查集)
- POJ1503 解题报告
- POJ - 2481 Cows解题报告(树状数组 相同点重复计数处理)
- POJ 1002 487-3279 解题报告
- POJ-1411 & HDOJ-1239 Calling Extraterrestrial Intelligence Again 解题报告
- POJ3299解题报告
- POJ 2719 Faulty Odometer 解题报告
- POJ 1019 解题报告
- POJ - 2418 Hardwood Species解题报告(trie树的建立以及遍历)
- POJ 3580 较复杂的Splay区间维护题解题报告
- POJ 1007 解题报告 DNA Sorting
- POJ 1657 Distance on Chessboard 解题报告
- POJ - 3414 Pots解题报告(输出路径的bfs)
- 【POJ】【1033】【Defragment】【模拟】解题报告
- POJ 1065 解题报告