二维树状数组-POJ 2155 Matrix
文章目录
- 树状数组
- 例题
- 题意
- 分析
- 代码
- 小结
树状数组
- 什么是树状数组? 简单来说,就是暴力遍历数组来解决区间问题等,不过遍历的路径使用了位运算来进行压缩,复杂度是O(log2(n))这样就不会超时了(为所欲为?)。
- lowbit()操作 其核心是神奇的lowbit操作,lowbit(x)=x&(-x),它的功能是找到x的二进制数的最后一个1,原理是利用负数的补码表示,补码是原码取反加一。例如x=6=00000110(2),-x=x补=11111010(2),那么lowbit(x)=x&(-x)=10(2)=2。 从lowbit()引出数组a[],a[x]的值是把ax(题目输入初始值)和他前面的m个数相加,如下表所示:
图形化:
那么通过数组a[],就可以求sum,例如sum(8)=a[8],sum(7)=a[7]+a[6]+a[4],sum(6)=a[6]+a[4],如此一来不就是我们要的路径压缩了吗? 同样地,更新ax时也要更新a[],例如更新a3,那么首先更改a[3];然后3+lowbit(3)=4,更改a[4];接着4+lowbit(4)=8,更改a[8]。
- 二维树状数组 相应的,我们可以推导出二维树状数组,例如更新点求区间输入坐标(x,y),(u,v)求sum黄色区域,如下图所示:
由图易得所求区间黄色区域为sum(u,v)-sum(x-1,v)-sum(u,y-1)+sum(x-1,y-1)
例题
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N). We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using “not” operation (if it is a ‘0’ then change it into ‘1’ otherwise change it into ‘0’). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 1 C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 2.Q x y (1 <= x, y <= n) querys A[x, y].
input:
The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case.
output:
For each querying output one line, which has an integer representing A[x, y]. There is a blank line between every two continuous test cases.
Sample Input:
1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1
Sample Output:
1
0
0
1
题意
在n*n矩阵中每次对一个子矩阵进行翻转(0变1,1变0),然后多次询问某个点是0还是1。
分析
更新区间求点,用二维树状数组解决,每次更新子矩阵+1,最后求点%2就得到结果。(其实就是二维数组模拟的思路,压缩路径罢了)。
代码
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1003;
char s[5];
int a[maxn][maxn], n;
int lowbit(int x) {
return (x & (-x));
}
void update(int x, int y) {
for (int i = x; i <= n; i += lowbit(i)) {
for (int j = y; j <= n; j += lowbit(j)) {
a[i][j]++;
}
}
}
int sum(int x, int y) {
int res = 0;
for (int i = x; i > 0; i -= lowbit(i)) {
for (int j = y; j > 0; j -= lowbit(j))
res += a[i][j];
}
return res;
}
int main() {
int ca, t;
int x, y, u, v;
scanf("%d", &ca);
while (ca--) {
memset(a, 0, sizeof(a));
scanf("%d%d", &n, &t);
while (t--) {
scanf("%s%d%d", s, &x, &y);
if (s[0] == 'C') {
scanf("%d%d", &u, &v);
x++, y++;
u++, v++;
update(u, v);
update(x - 1, y - 1);
update(x - 1, v);
update(u, y - 1);
}
else {
printf("%dn", sum(x, y) % 2);
}
}
printf("n");
}
return 0;
}
小结
- 树状数组能解决的题都能用线段树解决,但是树状数组编程复杂度低,完成效率更高!(大佬请无视)
原创不易,请勿转载(
本不富裕的访问量雪上加霜) 博主首页:https://blog.csdn.net/qq_45034708
- OpenProcess打开进程返回错误的问题
- Python标准库01 正则表达式 (re包)
- 剑指OFFER之栈的压入、弹出序列(九度OJ1366)
- Python标准库03 路径与文件 (os.path包, glob包)
- AI人工智能时代已经到来 “北斗即时判”实现纯语音交互
- 剑指OFFER之链表中倒数第k个节点(九度OJ1517)
- 用Qt写软件系列四:定制个性化系统托盘菜单
- Linux简介与厂商版本
- 用Qt写软件系列三:一个简单的系统工具之界面美化
- VS编译链接时错误(Error Link2005)的解决方法
- HttpClient使用心得
- 剑指OFFER之重建二叉树(九度OJ1385)
- 记录visual Studio使用过程中的两个问题
- 剑指OFFER之二维数组中的查找(九度OJ1384)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Windows下Docker安装ClickHouse
- ArrayList源码分析(基于jdk1.8)(三):Arrays.asList方法带来的问题
- 对基本类型包装类常量池的补充
- 与IntegerCache有关的一个比较坑的面试题
- C# Foreach循环本质与枚举器
- Java中的时间和日期(一):有关java时间的哪些坑
- Java中的时间和日期(二):java时间存储的基本原理
- 常用SQL语句
- Java中的时间和日期(三):java8中新的时间API介绍
- Java中的时间和日期(四):与java8时间API有关的一些总结和补充
- Head First设计模式——策略模式
- 可重用性的6个级别
- 您可能不需要使用Vue 3的Vuex
- 谈一谈若干的K-V NoSQL应用:LevelDB、Redis、Tair、RockesDB
- 高通量数据中批次效应的鉴定和处理(六)- 直接校正表达矩阵