Matrix

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17226   Accepted: 6461

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

Source

POJ Monthly,Lou Tiancheng
 
题意:给出矩阵左上角和右下角坐标,矩阵里的元素 1变0 ,0 变1,然后给出询问,问某个点是多少
思路:二维树状数组
 //2017-10-25
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ; int bt[N][N], n, q; int lowbit(int x){
return x&(-x);
} void add(int x, int y, int v){
while(x <= n){
int j = y;
while(j <= n){
bt[x][j] += v;
j += lowbit(j);
}
x += lowbit(x);
}
} int sum(int x, int y){
int sm = ;
while(x > ){
int j = y;
while(j > ){
sm += bt[x][j];
j -= lowbit(j);
}
x -= lowbit(x);
}
return sm;
} int main()
{
int T;
cin>>T;
while(T--){
scanf("%d%d", &n, &q);
memset(bt, , sizeof(bt));
char op;
int x, y, x1, y1;
while(q--){
getchar();
scanf("%c%d%d", &op, &x, &y);
if(op == 'C'){
scanf("%d%d", &x1, &y1);
add(x, y, );
add(x, y1+, -);
add(x1+, y, -);
add(x1+, y1+, );
}else{
printf("%d\n", sum(x, y)%);
}
}if(T)printf("\n");
} return ;
}

POJ2155(二维树状数组)的更多相关文章

  1. poj2155二维树状数组

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

  2. poj2155二维树状数组区间更新

    垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...

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

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

  4. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

  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. poj2155一个二维树状数组

                                                                                                         ...

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

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

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

  9. POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】

    这道题是一个二维树状数组,思路十分神奇,其实还是挺水的 题目描述 给定一个N∗NN∗N的矩阵AA,其中矩阵中的元素只有0或者1,其中A[i,j]A[i,j]表示矩阵的第i行和第j列(1≤i,j≤N)( ...

随机推荐

  1. 聊一聊PHP的global

    众所周知,在PHP的函数中,如果想使用全局变量,一种是使用超全局变量$GLOBALS,另一种是在函数中使用global关键字声明,使用超全局变量$GLOBALS的方式大家都知道了,今天来好好聊一聊使用 ...

  2. 解决Java中There is no getter for property XXX'XXX' in 'class XXX'的问题

    当你出现There is no getter for property XXX'XXX' in 'class XXX'时, 就是在你的这个类中没有找到你这个属性. 检查两个地方 1.你的返回值类型是否 ...

  3. Nuxt 2 即将来临

    原文出处: 

  4. [EXP]Microsoft Windows - DfMarshal Unsafe Unmarshaling Privilege Escalation

    Windows: DfMarshal Unsafe Unmarshaling Elevation of Privilege (Master) Platform: Windows (not tested ...

  5. Linux下安装、启动、停止mongodb

    1.下载完安装包,并解压 tgz(以下演示的是 64 位 Linux上的安装) curl .tgz # 下载 tar .tgz # 解压 mv mongodb/ /usr/local/mongodb ...

  6. 剑指offer【03】- 从尾到头打印链表(4种实现方法)

    题目:从尾到头打印链表 考点:链表 题目描述:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 法一:ArrayList头插法 /** * public class ListNode ...

  7. Android项目创建.prorperties配置文件和调用方法

    刚接触Android开发不久,今天写项目发现里面的可变参数有点多,(主要是服务器访问路径), 如果路径改变或者改名字了的话,若都写在代码里,岂不是要炸了? 我想到了Java项目里的有个.prorper ...

  8. .Net #if DEBUG调试模式代码使用

    #if DEBUG      Console.WriteLine("DEBUG:11111111111"); #else       Console.WriteLine(" ...

  9. .NET图平台下的图像处理工具---强大的Emgucv

    图像一直与时代相伴,图形化的应用软件也重不可缺.对于MFC.Delphi.Lazarus.Qt大家可能已经耳熟能详.对于很多图像处理的开源库,很多都是用C\C++写的,而.Net下的开源库以前很少了解 ...

  10. Unity 5 使用Roslyn编译器支持C# 7

    Unity 2017可选使用新的Mono编译器,支持.NET 4.6和C# 6,解决bug的同时,代码优化更佳. Unity 5可以使用社区开源方案:https://bitbucket.org/ale ...