POJ 2155 解题报告
2015-09-12 02:17
417 查看
这道题最简单的方法是用二维树状数组。
一维树状数组比较常见,资料也比较多:
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
一维树状数组比较常见,资料也比较多:
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
thestoryofsnow | 2155 | Accepted | 4088K | 422MS | C++ | 1520B |
/* 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; }
相关文章推荐
- Effective C++ 条款46 需要类型转换时请为模板定义非成员函数
- Codeforces Round #319 (Div. 2)(A,B,C,E)
- 使用ngx_lua构建高并发应用
- HYSBZ 1492 Cash(CDQ分治论文题)
- mongodb副本集群搭建
- 手机U盘制作成系统启动盘后在手机端无法识别
- Achive后 没有ios apps 只有other items
- spring,cxf,restful发布webservice传递List,Map,List<Map>
- CentOS 开放 80、8080 端口
- eclipse的svn插件出现问题,64位系统,解决方法
- 基础知识快速提升
- android-整体UI设计-(滑动导航栏+滚动页面)
- Unity3D简单电影视图编辑
- 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程
- java实现excel的导入导出(poi详解)
- oracle新建用户及授权
- 使用 apache-jmeter 完成WEB压力测试
- c++设计模式(五)Adapter class/object 适配器
- Leet Code 17 Letter Combinations of a Phone Number
- 第二章 :Hello World 应用程序详解