题目描述

给出一个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. zju(2)vivi的配置编译和固化

    1.实验目的 熟悉vivi的知识和应用并使用交叉编译平台vivi引导并烧写到目标板. 二.实验内容 1. 在Ubuntu下配置vivi并进行交叉编译: 2. 将编译好的vivi烧写到目标板上. 三.主 ...

  2. 从客户端中检测到有潜在危险的 request.form值[解决方法]

    当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?如下图所示: 下面博主汇总出现这种错误的几种解决方法:问题原因:由于在asp.net中,Requ ...

  3. EmguCV 一些结构

    一.MCvTermCriteria epsilon Epsilon max_iter Maximum iteration type CV_TERMCRIT value 二.MCvScalar vo T ...

  4. Python的正则表达式笔记

    1. "先抓大再抓小": 遇到一个正则表达式无法一次性筛选出所需内容时, 可以先在一个范围内筛选第一次, 再在小范围中筛选第二次. 2. pattern = re.compile( ...

  5. bash 常用操作

    删除不为空的文件夹 rm -rf dir_name

  6. php5-fpm.sock failed (13: Permission denied) error

    In order to fix the php5-fpm.sock failed error follow these instructions 1) Make sure your virtual h ...

  7. iOS开发_统计xcode代码行数

    如果要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 find . -name "*.m" -or -name "*.h" -or -nam ...

  8. LeetCode Reconstruct Itinerary

    原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...

  9. TCP/IP详解--连接状态变迁图CLOSE_WAIT

    终止一个连接要经过4次握手.这由TCP的半关闭(half-close)造成的.既然一个TCP连接是全双工(即数据在两个方向上能同时传递,可理解为两个方向相反的独立通道),因此每个方向必须单独地进行关闭 ...

  10. smartdraw2013破解方法

    smartdraw是一个非常好的画图工作,最大的优点就是支持多种图形,采用模板的方式在线扩充,可以快速画出你想要的图形,具体的介绍见其他资料. 这里是我自己的破解办法. 网上的下载都包含破解工具,但是 ...