POJ 2155 解题报告
来源:互联网 发布:gta5没网络怎么玩单机 编辑:程序博客网 时间:2024/05/19 17:57
这道题最简单的方法是用二维树状数组。
一维树状数组比较常见,资料也比较多:
TopCoder: https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-%20trees/
http://dongxicheng.org/structure/binary_indexed_tree/
看完后大致了解了。
但是这道题需要用到二维树状数组,并且是“区域更新,单点查询”,这也与常见用法不一致。所以我也不清楚为什么需要这样更新和查询(重点在于查询出来的不是区域么?),尽管博客讲解很详细:
http://www.cnblogs.com/chenxiwenruo/p/3388783.html
/* ID: thestor1 LANG: C++ TASK: poj2155 */#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 MAXN = 1000;int N, T;int C[MAXN + 1][MAXN + 1];inline int lowbit(int x){return x & (-x);}void updateBIT(int x, int y, int delta){for (int i = x; i > 0; i -= lowbit(i)){for (int j = y; j > 0; j -= lowbit(j)){C[i][j] += delta;}}}int queryBIT(int x, int y){int sum = 0;for (int i = x; i <= N; i += lowbit(i)){for (int j = y; j <= N; j += lowbit(j)){sum += C[i][j];}}return sum;}int main(){int X;scanf("%d", &X);char type;int x1, y1, x2, y2;for (int x = 0; x < X; ++x){scanf("%d%d", &N, &T);for (int i = 1; i <= N; ++i){for (int j = 1; j <= N; ++j){C[i][j] = 0;}}for (int t = 0; t < T; ++t){scanf(" %c ", &type);if (type == 'C'){scanf("%d%d%d%d", &x1, &y1, &x2, &y2);updateBIT(x2, y2, 1);updateBIT(x1 - 1, y2, -1);updateBIT(x2, y1 - 1, -1);updateBIT(x1 - 1, y1 - 1, 1);}else if (type == 'Q'){scanf("%d%d", &x1, &y1);printf("%d\n", queryBIT(x1, y1) % 2);}else{assert(false);}}// There is a blank line between every two continuous test cases. printf("\n");}return 0; }
0 0
- POJ 2155 解题报告
- POJ 2155 二维树状数组 解题报告
- poj数百篇解题报告
- poj 1141解题报告
- poj 1001解题报告
- poj 1011 解题报告
- poj 1087 解题报告
- poj 2153解题报告
- poj 2051解题报告
- POJ 1167 解题报告
- poj 1026 解题报告
- POJ 1077 解题报告
- POJ 1042 解题报告
- POJ 1095 解题报告
- POJ 1159解题报告
- poj 2312解题报告
- POJ 1001解题报告
- POJ 1002解题报告
- 第三章 :介绍 Auto Layout
- spring,cxf,restful发布webservice传递List,Map,List<Map>
- Achive后 没有ios apps 只有other items
- BZOJ 1492 Cash(CDQ分治论文题)
- Codeforces Round #319 (Div. 2)(A,B,C,E)
- POJ 2155 解题报告
- Statement和PreparedStatement的区别
- Zabbix 监控 Nginx
- 计票统计
- 学长们的求职血泪史(C/C++/JAVA)
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- 八大排序算法
- 【小熊刷题】3Sum Closest <Leetcode 16, Java>
- Leet Code Medium 18 4Sum