Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15125   Accepted: 5683

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

一个二维树状数组,好像题目说错了,其实是左下角和右上角的。
 #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
long long int a[][];
int m;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int x1,int y1)
{
int i,j;
for(i=x1;i>;i-=lowbit(i))
for(j=y1;j>;j-=lowbit(j))
a[i][j]++; for(i=x-;i>;i-=lowbit(i))
for(j=y-;j>;j-=lowbit(j))
a[i][j]++; for(i=x1;i>;i-=lowbit(i))
for(j=y-;j>;j-=lowbit(j))
a[i][j]++; for(i=x-;i>;i-=lowbit(i))
for(j=y1;j>;j-=lowbit(j))
a[i][j]++;
}
int fun(int x,int y)
{
int i,j;
int sum=;
for(i=x;i<=m;i+=lowbit(i))
for(j=y;j<=m;j+=lowbit(j))
sum+=a[i][j];
return sum%;
}
int main()
{
//freopen("int.txt","r",stdin);
int n;
int i,j;
scanf("%d",&n);
for(i=;i<n;i++)
{
memset(a,,sizeof(a));
int k;
scanf("%d%d",&m,&k);
char b;
for(j=;j<k;j++)
{
cin>>b;
int x1,x2,y1,y2;
if(b=='Q')
{
scanf("%d%d",&x1,&y1);
printf("%d\n",fun(x1,y1));
}
else
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update(x1,y1,x2,y2);
}
}
if(i!=n-)
printf("\n");
}
}

poj2155一个二维树状数组的更多相关文章

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

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

  2. POJ2155【二维树状数组,区间修改,点查询?】【又被输入输出坑】

    这题反反复复,到现在才过. 这道题就是树状数组的逆用,用于修改区间内容,查询点的值. 如果单纯就这个奇偶数来判的话,似乎这个思路比较好理解. 看了一下国家集训队论文(囧),<关于0与1在信息学奥 ...

  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. POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】

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

  6. bzoj 1452: [JSOI2009]Count (二维树状数组)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1452 思路: 对每个颜色开一个二维树状数组维护就好了 实现代码: #include<b ...

  7. 【二维树状数组】计数问题 @JSOI2009/upcexam5911

    时间限制: 1 Sec 内存限制: 128 MB 题目描述 一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入 ...

  8. BZOJ 1452 Count 【模板】二维树状数组

    对每种颜色开一个二维树状数组 #include<cstdio> #include<algorithm> using namespace std; ; ][maxn][maxn] ...

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

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

随机推荐

  1. Web.py 框架学习笔记 - ctx

    摘要: ctx用于存取web请求的环境变量,基于ThreadedDict类进行实例化.ThreadedDict类可实例化字典类型的对象,该对象某些属性可用于存取处理线程的id. 这样字典化实例的线程池 ...

  2. Spring-MVC请求参数值和向页面传值

    读取请求参数值 方式一:通过HttpServletRequest req做参数 DispatcherServlet在调用处理的方法之前,利用Java反射分析方法的结构,通过分析,将req对象传过来 方 ...

  3. webservice中jaxws:server 和jaxws:endpoint的区别

    今天在学习使用spring+cxf发布webservice时遇到个问题,我原来是用 <jaxws:endpoint id="helloWorld" implementor=& ...

  4. linux 增量备份命令Rsync 使用详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt320 Rsync的命令格式可以为以下六种: rsync [OPTION].. ...

  5. 利用HTML5的window.postMessage实现跨域通信

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp77   HTML5的window.postMessage简述 postM ...

  6. linq 起源

    在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...

  7. 蓝桥杯试题利用数学知识经典解法,1.三个空瓶子换一瓶水;2.猜最后一个字母——猎八哥FLY

    本博客为本人原创,转载请在醒目位置表明出处. 1.乐羊羊饮料厂正在举办一次促销优惠活动.乐羊羊C型饮料,凭3个瓶盖可以再换一瓶C型饮料,并且可以一直循环下 去,但不允许赊账.请你计算一下,如果小明不浪 ...

  8. ospf剩余笔记

    OSPF 流程图: 带宽 开销 10     100 100    19 1000   4 10000 2 区域的划分减少lsdb的大小 有利于网络管理员故障排除 网络故障不会影响到其他区域 邻接关系 ...

  9. springmvc注解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  10. 对#ifndef的理解

    由于对#ifndef的用法不太理解,在询问了老师#ifndef的含义以及查找资料后,对#ifndef总结了以下几点: <1> #ifndef是宏定义的一种,是三种预处理功能(宏定义,文件包 ...