题目描述

给出一个n*n的矩阵,矩阵中只有0和1,现在有两种操作:

1 x y 将第x行第y列的数字改变(0变1,1变0)

2 x1 y1 x2 y2求由左上角(x1,y1)到右下角(x2,y2)组成的矩形中的1的个数。

现在初始的矩阵全是0,之后有一系列操作。保证数据输入合法。

输入

第一行输入一个正整数T,代表测试组数。(T <= 10)

每组测试数据的第一行有两个数n,m。(1 <= n <= 500 , 1 <= m <= 10000)

之后是连续m行,代表m次操作。(1 <= x1,y1 <= x2,y2 <= n)

输出

对每次询问输出(x1,y1)到(x2,y2)矩形内的1的个数

示例输入

1

3 3

1 2 2

1 1 1

2 1 1 3 3

示例输出

2

标准的二维树状数组

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
int n,m;
int a[550][550];
int Map[550][550];
int lowbit(int x)
{
return x&(-x);
} void update(int x,int y,int d)
{
int i=x,j;
while(i<=n)
{
j=y;
while(j<=n)
{
a[i][j]+=d;
j+=lowbit(j);
}
i+=lowbit(i);
}
}
int Query(int x,int y)
{
int sum=0;
int i=x,j;
while(i>0)
{
j=y;
while(j>0)
{
sum+=a[i][j];
j-=lowbit(j);
}
i-=lowbit(i);
}
return sum;
} int main()
{
int T;
int flag;
int x1,y1,x2,y2,d;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(Map,0,sizeof(Map));
memset(a,0,sizeof(a));
for(int i=1;i<=m;i++)
{
scanf("%d",&flag);
if(flag==1)
{
scanf("%d %d",&x1,&y1);
if(Map[x1][y1]==1)
{
Map[x1][y1]=0;
d=-1;
}
else
{
Map[x1][y1]=1;
d=1;
}
update(x1,y1,d);
}
else
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
printf("%d\n",Query(x2,y2)-Query(x1-1,y2)-Query(x2,y1-1)+Query(x1-1,y1-1));
}
}
}
return 0;
}

SDUTOJ 3312的更多相关文章

  1. sdutoj 2151 Phone Number

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2151 Phone Number Time Li ...

  2. sdutoj 2610 Boring Counting

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610 Boring Counting Time ...

  3. sdutoj 2609 A-Number and B-Number

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2609 A-Number and B-Numbe ...

  4. sdutoj 2624 Contest Print Server

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2624 Contest Print Server ...

  5. sdutoj 2608 Alice and Bob

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...

  6. sdutoj 2623 The number of steps

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2623 The number of steps ...

  7. sdutoj 2606 Rubik’s cube

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2606 Rubik’s cube Time Li ...

  8. sdutoj 2605 A^X mod P

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605 A^X mod P Time Limit ...

  9. 关键路径 SDUTOJ 2498

    SDUTOJ 2498 AOE网上的关键路径 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 一个无环的有向图称为无环图(Dire ...

随机推荐

  1. ob_clean()

    我在本地写的系统,部署到虚拟机上的时候,验证码出现问题不显示.加上这个函数之后,验证码出现了.具体的该学习去了.

  2. Spring的quartz定时器同一时刻重复执行二次的问题解决

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...

  3. hadoop-1.2.1安装配置

    1.准备三台节点 hnd1  hnd2  hnd3 下载 hadoop 下载地址:http://apache.fayea.com/hadoop/common/ API文档:http://hadoop. ...

  4. JS开发windows phone8.1系列之2

    http://msdn.microsoft.com/zh-cn/library/windows/apps/dn629636.aspx Windows.Storage.ApplicationData.r ...

  5. A Great Alchemist

    Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB ProblemCarol is a great alchemist. In ...

  6. 安装Arch Linux(桌面环境)

    安装xorg-server # pacman -S xorg-server xorg-server-utils xorg-xinit 安装显卡驱动 如果不知道是什么显卡,就使用以下命令查看 # lsp ...

  7. [办公自动化]一次制作、多场合多次使用的PPT

    面试或汇报总结时,你可能需要制作一份PPT来介绍自己.但是这些场合你不一定能讲完所有的内容. 你可以尝试分1分钟.3分钟.5分钟.10分钟的版本来准备. 使用同一份PPT. 没错. 在office20 ...

  8. Indexing and Hashing

    DATABASE SYSTEM CONCEPTS, SIXTH EDITION11.1 Basic ConceptsAn index for a file in a database system wo ...

  9. Population Mean

    Probability and Statistics > Moments > History and Terminology > Disciplinary Terminology & ...

  10. sublime添加PHP语法检查

    1.找到php文件目录 如E:\xampp\php 放到环境变量的path中   2.sublime 工具-编译系统-新编译系统  {     "cmd": ["php& ...