SDUTOJ 3312
题目描述
给出一个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的更多相关文章
- sdutoj 2151 Phone Number
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2151 Phone Number Time Li ...
- sdutoj 2610 Boring Counting
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2610 Boring Counting Time ...
- sdutoj 2609 A-Number and B-Number
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2609 A-Number and B-Numbe ...
- sdutoj 2624 Contest Print Server
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2624 Contest Print Server ...
- sdutoj 2608 Alice and Bob
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...
- sdutoj 2623 The number of steps
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2623 The number of steps ...
- sdutoj 2606 Rubik’s cube
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2606 Rubik’s cube Time Li ...
- sdutoj 2605 A^X mod P
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2605 A^X mod P Time Limit ...
- 关键路径 SDUTOJ 2498
SDUTOJ 2498 AOE网上的关键路径 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 一个无环的有向图称为无环图(Dire ...
随机推荐
- CSS Bugs 解决方案
说明:本文档兼容性测试基础环境为:windows系统:IE6-IE10, Firefox6.0, Chrome13.0, Safari5.1, Opera11.51 Bugs及解决方案列表(以下实例默 ...
- SQL Server 中字符串中包含字符串变量的表示方法
在代码中有如下的需求:需要在数据库中使用 in 关键字做删除的时候,又需要使用到参数化,参数又是字符串,所以使用的时候就按照如下方式 StringBuilder sql = new StringBui ...
- PHP filesystem attack vectors
http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/ On Apr 07, 2008 I spoke with Kuza55 and ...
- C#编码标准
一.命名约定 1.PascalCasting PascalCasing 每一个单词第一个字母大写,其余字母均小写.例如:FileAccess,ArraySegment等. 除了参数.变量.常量外,所有 ...
- Socket请求和Http请求的各自特点、区别及适用场景
Socket实现服务器与客户端之间的物理连接,并进行数据传输.主要有TCP/UDP两个协议.Socket处于网络协议的传输层.TCP:传输控制协议,面向连接的的协议,稳定可靠.当客户和服务器彼此交换数 ...
- C#winform调用外部程序,等待外部程序执行完毕才执行下面代码
1.简单调用外部程序文件(exe文件,批处理等),只需下面一行代码即可 System.Diagnostics.Process.Start(“应用程序文件全路径”); 2.如果要等待调用外部程序执行完毕 ...
- SQL server2012怎么备份数据库(设置自动备份)
1.打开SQL server配置管理器,设置sql server服务里的SQL server代理服务为自动并启动 2.启动Master Data Services Configuration Mana ...
- 微信公众平台开发(26) ACCESS TOKEN
本文介绍微信公众平台下Access Token的概念及获取方法. 一.Access Token access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常 ...
- 解决 Virtual Box 启动 Cannot load R0 module supLoadModule returned VERR_LDR_MISMATCH_NATIVE Failed to register ourselves as a PCI Bus (VERR_MODULE_NOT_FOUND)
返回 代码:E_FAIL (0x80004005)组件:Console界面:IConsole {8ab7c520-2442-4b66-8d74-4ff1e195d2b6} 原因: 我新建了一个vdi文 ...
- For,Function,Lazy
package com.dtgroup.study import scala.io.Source object ForFunctionLazy { def main(args: Array[Strin ...