寒假Day46:POJ2155-Matrix-二维树状数组
题面:

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. The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 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.
样例:
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
output
1 0 0 1
题意:
一个N*N的矩阵,初始化为0,且只能为1或0,
C x、y、xx、yy 两个坐标,代表一个矩阵的左上角和右下角所包围的这个矩阵进行翻转(0变1,1变0)
Q x、y代表输出该坐标对应的点到该图最左下角累计共进行过几次翻转操作=ans
最后ans为奇数输出1,偶数输出0
思路:
对一个二维树状数组进行修改和查询操作
1和0的相互变换利用异或操作即可
update是修改操作:
void update(int x, int y) { for(int i=x; i<=n; i+=lowbit(i)) { for(int j=y; j<=n; j+=lowbit(j)) c[i][j]^=1;//++也对 } }
sum是查询操作:
int sum(int x, int y) { int ret=0; for(int i=x; i>0; i-=lowbit(i)) { for(int j=y; j>0; j-=lowbit(j)) ret+=c[i][j]; } return ret; }
对于区域的更新操作:
(我的理解是对于点(x,y)到(n,n)的区域分成四个部分,分别进行更新)(不知道这样理解是否正确??)
update(x,y),update(xx+1,y),update(x,yy+1),update(xx+1,yy+1);
画了一幅图,5*5的矩阵,假设给的数据是C 2 2 3 3,那么就是如下图所示(我的坐标是按照二维数组的定义来画的)
那么就将这块区域的下面、右面、右下方全部进行更新,就相当于该区域进行了单独变换
也就是需要更新(2,2)、(4,2)、(2,4)、(4,4)到(5,5)的区域,四个部分
AC代码:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<queue> using namespace std; #define inf 0x3f3f3f3f typedef long long ll; const int N = 1010; int n,c[N][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)) c[i][j]^=1; } } int sum(int x, int y) { int ret=0; for(int i=x; i>0; i-=lowbit(i)) { for(int j=y; j>0; j-=lowbit(j)) ret+=c[i][j]; } return ret; } int main() { int t,q; scanf("%d",&t); while(t--) { memset(c,0,sizeof(c)); scanf("%d %d",&n,&q); for(int i=1; i<=q; i++) { getchar(); char ch; scanf("%c",&ch); if(ch=='C')//4 { int x,y,xx,yy; scanf("%d %d %d %d",&x,&y,&xx,&yy); update(x,y),update(xx+1,y),update(x,yy+1),update(xx+1,yy+1); } else//2 { int x,y; scanf("%d %d",&x,&y); printf("%d\n",sum(x,y)%2); } } printf("\n"); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」