Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17766   Accepted: 6674

Description

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. 

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

题意:对一个给定size且初始化为0的矩阵。运行一些命令,Q A B为查看arr[a][b]元素的值,C X1 Y1 X2 Y2为将(x1, y1) (x2, y2)矩形范围内的全部点0、1翻转。

题解:树状数组模式二的使用方法。段更新,点查询。update(x2, y2)表示从(1, 1)到(x2, y2)范围内的全部点都要翻转一次,可是这样会把给定范围外的一些点也翻转到,因此须要将这些点翻转回去。

#include <stdio.h>
#include <string.h>
#define maxn 1002 int size, tree[maxn][maxn]; int lowBit(int x){ return x & (-x); } //向下更新表示A[1]...A[i]每一个元素都要 += val,推广到二维同理
void update(int x, int y, int val)
{
int temp;
while(x > 0){
temp = y;
while(temp > 0){
tree[x][temp] += val;
temp -= lowBit(temp);
}
x -= lowBit(x);
}
} int query(int x, int y)
{
int sum = 0, temp;
while(x <= size){
temp = y;
while(temp <= size){
sum += tree[x][temp];
temp += lowBit(temp);
}
x += lowBit(x);
}
return sum;
} int main()
{
//freopen("stdin.txt", "r", stdin); int cas, q, a, b, c, d;
char com[2];
scanf("%d", &cas); while(cas--){
scanf("%d%d", &size, &q);
memset(tree, 0, sizeof(tree)); while(q--){
scanf("%s%d%d", com, &a, &b);
if(com[0] == 'C'){
scanf("%d%d", &c, &d);
update(c, b - 1, -1);
update(a - 1, d, -1);
update(a - 1, b - 1, 1);
update(c, d, 1);
}else printf("%d\n", query(a, b) & 1);
} if(cas) printf("\n");
}
return 0;
}

POJ2155 Matrix 【二维树状数组】+【段更新点查询】的更多相关文章

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

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

  2. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  3. poj----2155 Matrix(二维树状数组第二类)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16950   Accepted: 6369 Descripti ...

  4. POJ2155:Matrix(二维树状数组,经典)

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

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

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

  6. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

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

  8. poj2155一个二维树状数组

                                                                                                         ...

  9. POJ 2155 Matrix(二维树状数组,绝对具体)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Descripti ...

  10. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

随机推荐

  1. 微信小程序中使用echarts

    一.效果图 二.代码 import * as echarts from '../../component/ec-canvas/echarts'; const app = getApp(); var x ...

  2. Laravel Excel模板导出-带图片

    Laravel Excel版本 3.1 1.数据准备 建个2个表,加点数据,控制器中查数据,给模板使用. 表1-order:id, order_no, img_path, note 表2-order_ ...

  3. Sql Server cross apply和outer apply

    with result as( select t.str from( ' str union all ' str union all ' str union all ' str union all ' ...

  4. nginx配置实现负载均衡

    一.负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正 ...

  5. hdfs深入:10、hdfs的javaAPI操作

    /** * 递归遍历hdfs中所有的文件路径 */ @Test public void getAllHdfsFilePath() throws URISyntaxException, IOExcept ...

  6. 关于OpenMP的归约操作reduction

    这里提一个重要的点 像这样 ; void ff() { sum += 0.5; } //main() #pragma omp parallel for reduction(+:sum) ; i < ...

  7. win10下硬盘安装CentOS7

    安装环境: 1.系统:Windows 10 2.硬盘:SSD(已装好Win 10) + HHD(用来装CentOS 7) 准备工作: 1.DiskGenius(分区工具):用来给硬盘做分区: 2.系统 ...

  8. js 技巧 (十)广告JS代码效果大全 【1】

    广告JS代码效果大全 1.[普通效果]     现在很多网站广告做的如火如荼,现在我就来介绍一下常见的对联浮动广告效果的代码使用方法,介绍的这种效果,在1024*768分辨率下正常显示,在800*60 ...

  9. 树莓派-3 启用root

    默认是user: pi,  password: raspberry 通过如下设置root密码并启用 pi@raspberrypi:~ $ sudo passwd root Enter new UNIX ...

  10. 定义一个复数类Complex

    #include<iostream> #include<math.h> using namespace std; class Complex{ public: Complex( ...