Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 22058   Accepted: 8219

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往上加一,同时方块右边,下面和右下方的区域再加1,只要保证他们那边加个偶数就可以了。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = + ;
int c[MAX][MAX];
int n;
int lowbit(int k)
{
return k & (-k);
}
void update(int x,int y,int num)
{
for(int i = x; i < n; i += lowbit(i))
{
for(int j = y; j < n; j += lowbit(j))
c[i][j] += num;
}
}
int sum(int x,int y)
{
int s = ;
for(int i = x; i > ; i -= lowbit(i))
{
for(int j = y; j > ; j -= lowbit(j))
s += c[i][j];
}
return s;
}
int main()
{
int t,q;
int num = ;
scanf("%d", &t);
while(t--)
{
if(num ++)
printf("\n");
scanf("%d%d", &n,&q);
memset(c,,sizeof(c));
char ch;
int x1,y1,x2,y2;
getchar();
while(q--)
{
scanf("%c", &ch);
if(ch == 'Q')
{
scanf("%d%d", &x1,&y1);
getchar();
int m = sum(x2,y2) - sum(x1 - , y2) - sum(x2,y1 - ) + sum(x1-,y1-);
if(m % == )
printf("0\n");
else
printf("1\n");
}
else if(ch == 'C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
getchar();
update(x1,y1,);
}
}
}
return ;
}

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

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  2. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  4. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

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

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

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

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

  7. POJ 2155 Matrix (二维树状数组)

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

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

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

  9. MooFest_二维树状数组

    Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...

随机推荐

  1. 怎样修改 Openstack Horizon(Dashboard)的显示界面 (一)

    Openstack 有很多项目,比如 nova 是虚拟机管理,neutron 是虚拟网络管理, glance 是存储管理,而 horizon 是负责 Openstack 的统一界面.horizon 的 ...

  2. TextBoxFor控件的扩展---Bootstrap在mvc上的应用

    TextBoxFor控件的问题: 1:自带了样式,再用bootstrap样式会有冲突. 2:要加水印,js事件,限制输入长度比较麻烦. 因此需要对textboxfor控件进行扩展. 目标: 1:能使用 ...

  3. HTTP 错误 500.24 - Internal Server Error的解决方法

    错误提示: 最可能的原因:   system.web/identity@impersonate 设置为 true. 解决办法: 现在经典模式 连微软都几乎放弃了 原设想是为iis不断升级 提供的一种兼 ...

  4. 0921MySQL 报错 ERROR 1290 (HY000): running with the --secure-file-priv

    http://blog.itpub.net/26506993/viewspace-2121850/ mysql> show variables like '%secure%';+-------- ...

  5. 开发错误记录8:Unable to instantiate application com

    开发错误记录8:Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication 这是因为在And ...

  6. 【CodeVS 1199】【NOIP 2012】开车旅行

    http://codevs.cn/problem/1199/ 主要思想是倍增,对于第一个回答从后往前扫,依次插入平衡树中. 我写的splay,比较繁琐. #include<cmath> # ...

  7. Swift 高阶函数

    map.flatMap.filter和reduce,几乎实现lambda表达式的语言里都会在集合里增加这些方法, 见swift 学习(一)基础知识 (基本数据类型,操作符,流控制,集合)中的集合 ht ...

  8. 转:如何用EXCEL表运用FV函数

    转:http://zhidao.baidu.com/link?url=lKFCYBW-zMC-pp8GkFXZnmwQf3YL_csYLGo-0v2OAASSZwjw40QRgEO0V8s2Y3zCJ ...

  9. 操作系统中的IPC机制

    按发送路径来看,可分为直接通信和间接通信. 1. 直接通信 (1)进程必须正确的命名对方 send (P, message) – 发送信息到进程P receive(Q, message) – 从进程 ...

  10. 【BZOJ-2115】Xor 线性基 + DFS

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2142  Solved: 893[Submit][Status] ...