描述

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].

输入

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.

输出

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.

样例输入

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

样例输出

1
0
0
1

题意

初始n*n的矩阵全为0

Q个操作

1.[X1,Y1]-[X2,Y2]中取反操作

2.查询[X1,Y1]的值

题解

1.区间更新分成4块,([X1,Y1]-[n,n])([X2,X2]-[n,n])([X2+1,Y1]-[n,n])([X1,Y2+1]-[n,n]),每个区间都+1操作,只保证[X1,Y1]-[X2,Y2]+1,其余+2或者+4

2.单点查询[X1,Y1]的值,只需要查询[X1,Y1]的值%2即可

代码

 #include<bits/stdc++.h>
using namespace std; const int N=;
int n; struct BIT2{
int sum[N][N];
void init(){memset(sum,,sizeof(sum));}
int lowbit(int x){return x&(-x);}
void update(int x,int y,int w)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
sum[i][j]+=w;
}
int query(int x,int y)
{
int ans=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
ans+=sum[i][j];
return ans;
}
}T; int main()
{
int t,q,o;
scanf("%d",&t);
while(t--)
{
if(o++)printf("\n");
T.init();
scanf("%d%d",&n,&q);
for(int i=;i<q;i++)
{
char op[];
int x1,y1,x2,y2;
scanf("%s",op);
if(op[]=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
T.update(x1,y1,);
T.update(x2+,y1,);
T.update(x1,y2+,);
T.update(x2+,y2+,);
}
else
scanf("%d%d",&x1,&y1),printf("%d\n",T.query(x1,y1)%);
}
}
}

TZOJ 2722 Matrix(树状数组区间取反单点查询)的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

  2. POJ 2155 Matrix (树状数组 && 区间计数)

    题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, ...

  3. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace-树状数组-区间修改,单点查询

    赛后和队友讨论了一波,感谢无敌的队友给我细心的讲题 先埋坑 #include<iostream> #include<string.h> #include<algorith ...

  5. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. 树状数组区间加法&区间求和操作

    树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...

  9. hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场

    题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...

随机推荐

  1. 机器学习进阶-图像形态学操作-膨胀操作 1.cv2.dilate(进行膨胀操作)

    1.cv2.dilate(src, kernel, iteration) 参数说明: src表示输入的图片, kernel表示方框的大小, iteration表示迭代的次数 膨胀操作原理:存在一个ke ...

  2. WPF 透明窗体

    窗体属性中设置:Background="Transparent" AllowsTransparency="True" WindowStyle="Non ...

  3. elasticSearch-DSL

    DSL: query_string match match_phrase match_phrase_prefix multi_match   simple_query_string term term ...

  4. springboot 停止

    因springboot内嵌tomcat或jetty使得我们没法去操作服务: 因此,常常是服务起来后,要重启时会端口占用,我们只能无情的kill掉端口. 不过spring也设置有配置停止的请求: App ...

  5. 使用CLR Function代替T-SQL函数,优化检索效率

    前言: 在使用存储过程查询数据中,T-SQL字符串拆分函数效率低下,这个时候我们可以采用CLR Function代替T-SQL函数,使用DLL执行字符串分解过程,并返回值到SQL中.测试复杂运行的速度 ...

  6. Java面试题_简答题

    作为一个大三在校生,很快就要去实习了,但总感觉自己连一个刚入门的菜鸟都不如,哎.发现自己连那个程序员的门槛都还没进,有点小伤心,不过伤心没用,努力向前才是我们现在应该做的事情. 下面是我之前在学校所从 ...

  7. APP发行渠道

    1,安卓APP发行:google play,原名android store 2,IOS APP: apple store 3,国内各大平台,应用宝,360,小米,华为 ...

  8. Mac下的SecureCRT使用技巧

    1.secureCRT session manager 怎么添加到标题栏里? Options - Global Options - General 把 Use dockable session man ...

  9. Android app 性能优化的思考--性能卡顿不好的原因在哪?

    说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才 ...

  10. C# winform进度条 (异步)

    进度条页面: http://www.cnblogs.com/Deckard/archive/2009/06/24/1510451.html //============================ ...