描述

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. day05-if-else语句

    1.Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块.Python程序语言指定任何非0和非空(null)值为true,0 或者 nu ...

  2. cookies_ajax

    views def test_user(request): print('start') if request.method=='POST': print('goon_test_user') user ...

  3. Delphi 三层TDataSetProvider

    在Delphi想使用三层架构或者使用TClientDataSet控件,一般都需要引用TDataSetProvider控件,现对TDataSetProvider控件的Options属性值做一个简单的分析 ...

  4. 尚硅谷springboot学习15-日志框架1-入门

    引子 小张:开发一个大型系统 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? ​ 2.框架来记录系统的一些运行时信息:日志框架 : ...

  5. ETC2 区别于ETC的重要点

    ETC2 主要是对于NPOT却是4的倍数的贴图有较大压缩,比如一个1920X1080RGB的Loading图,ETC4压缩下不管用,大小5.9M,ETC2下压缩为1M

  6. 从初始化列表和构造函数谈C++的初始化机制

    来源:http://blog.csdn.net/theprinceofelf/article/details/20057359 前段时间被人问及“初始化列表和构造有什么区别?”我竟一时语塞,只好回头 ...

  7. Kubernetes 1.8.x 全手动安装教程----转自Kubernetes中文社区(部分内容根据实验环境做了些修改,特此感谢Kubernetes中文社区)

    Kubernetes 提供了许多云端平台与操作系统的安装方式,本章将以全手动安装方式来部署,主要是学习与了解 Kubernetes 创建流程.若想要了解更多平台的部署可以参考 Picking the ...

  8. python实现Excel删除特定行、拷贝指定行操作

    工作中遇到的,本来用VBA写的,操作很慢,尝试用Python实现, 任务需求: 从原始的两张表中拷贝行到五张表中,如下表所示: source1和source2是一样的格式:         one t ...

  9. search() 方法解析

    search()方法支持正则表达式的String对象的方法. 好,我们直接来贴代码,看效果,从实践理解透析方法的知识点和实际运用. var str="Visit W3School!" ...

  10. numpy-帮助文档 & 速查表.md

    目录 转相关资料: 速查表 速查代码 转相关资料: 官方手册 易佰教程 gitbook ZMonster's Blog 速查表 速查代码 # -*- coding: utf-8 -*- "& ...