Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17224   Accepted: 6460

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

很裸的题。

可以使用二维树状数组。

二维的写起来很方便,两重循环。

如果是要修改(x1,y1)  -  (x2,y2)的矩形区域。

那么可以在(x1,y1) 出加1,在(x2+1,y1)处加1,在(x1,y2+1)处加1,在(x2+1,y2+1)处加1 。。

画个图就知道了,查询单点就是求和。

 /* ***********************************************
Author :kuangbin
Created Time :2014/5/23 22:34:04
File Name :E:\2014ACM\专题学习\数据结构\二维树状数组\POJ2155.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
int lowbit(int x)
{
return x&(-x);
}
int c[MAXN][MAXN];
int n;
int sum(int x,int y)
{
int ret = ;
for(int i = x;i > ;i -= lowbit(i))
for(int j = y;j > ;j -= lowbit(j))
ret += c[i][j];
return ret;
}
void add(int x,int y,int val)
{
for(int i = x;i <= n;i += lowbit(i))
for(int j = y;j <= n;j += lowbit(j))
c[i][j] += val;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
int q;
scanf("%d%d",&n,&q);
memset(c,,sizeof(c));
char op[];
int x1,y1,x2,y2;
while(q--)
{
scanf("%s",op);
if(op[] == 'C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(x1,y1,);
add(x2+,y1,);
add(x1,y2+,);
add(x2+,y2+,);
}
else
{
scanf("%d%d",&x1,&y1);
if(sum(x1,y1)% == )printf("0\n");
else printf("1\n");
}
}
if(T > )printf("\n");
}
return ;
}

POJ 2155 Matrix (二维树状数组)的更多相关文章

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

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

  2. poj 2155 Matrix (二维树状数组)

    题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...

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

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

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

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

  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 2029 (二维树状数组)题解

    思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...

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

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

  8. poj 2155 B - Matrix 二维树状数组

    #include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...

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

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

  10. Matrix 二维树状数组的第二类应用

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17976   Accepted: 6737 Descripti ...

随机推荐

  1. 论文笔记之: Hierarchical Convolutional Features for Visual Tracking

    Hierarchical Convolutional Features for Visual Tracking  ICCV 2015 摘要:跟卢湖川的那个文章一样,本文也是利用深度学习各个 layer ...

  2. systemtap

    http://www.ibm.com/developerworks/library/l-systemtap/index.html http://wiki.eclipse.org/Linux_Tools ...

  3. 【转载】Hadoop机架感知

    转载自http://www.cnblogs.com/ggjucheng/archive/2013/01/03/2843015.html 背景 分布式的集群通常包含非常多的机器,由于受到机架槽位和交换机 ...

  4. ajax往后台传json格式数据报415错误

    问题描述: ajax往后台传json格式数据报415错误,如下图所示 页面代码 function saveUser(){ var uuId = document.getElementById(&quo ...

  5. .NET垃圾回收:非托管资源,IDispose和析构函数的结合

    http://blog.jobbole.com/85436/ 原文出处: 田小计划   欢迎分享原创到伯乐头条 前面一篇文章介绍了垃圾回收的基本工作原理,垃圾回收器并不是可以管理内存中的所有资源.对于 ...

  6. Productivity Power Tools 的使用

    免费的精品: Productivity Power Tools 动画演示 Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率 ...

  7. numpy.distutils.system_info.NotFoundError: no lapack/blas resources found

    python35用pip安装scipy的时候报错 numpy.distutils.system_info.NotFoundError: no lapack/blas resources found 原 ...

  8. MSSQL FOR MXL PATH 运用(转载)

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  9. Python入门1

    简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承.Python ...

  10. TCP/UDP的接收包方式

    UDP udp不是流式的,每次接收一个包,长度不超过(65535-28,总包长65535字节,包头28字节).所以UDP方式下不需要填写任何参数直接调用 $client->recv() 即可.注 ...