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 ...
随机推荐
- Hibernate和Mybatis的对比
http://blog.csdn.net/jiuqiyuliang/article/details/45378065 Hibernate与Mybatis对比 1. 简介 Hibernate:Hiber ...
- UIDynamic(物理仿真)
简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象 如: 重力.弹性碰撞等现象 物理引 ...
- 对js中的Date扩展,格式化日期
/** * 对Date的扩展,将 Date 转化为指定格式的String 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) * 可以用 1-2 个占位符 年 ...
- 温故而知新 OOP
设计原则1: 找出应用中可能需要发生改变的地方,把它们独立出来,不要和那些不需要变化的代码混在一起换句话说,如果每次新的需求一来,都会使某方面的代码发生变化,此时你就可以确定,这部分代码属于不稳定代码 ...
- html 抽奖代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Eclipse安装jad插件进行反编译
1.下载的eclipse是免安装的工具: 打开eclipse后在windows-preference下没有找到jadClipse,希望显示这个插件: 方法一:Help-Eclipse Marketpl ...
- 创建一个LinkedList,然后在其中插入多个值,确保每个值都插入到List中间(偶数中间两个数之一,奇数在正中间)
这是Thinking in java 中的一道题,下面是我的解决方案: package test; import java.util.LinkedList; import java.util.List ...
- Java代理模式
java代理模式及动态代理类 1. 代理模式 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目 ...
- EmguCV 简单图形绘制
一.圆 public static void cvCircle( IntPtr img, System.Drawing.Point center, //Center of the circle int ...
- NEC学习 ---- 模块 - 左图右文图文列表
该模块效果图: 这个模块也是在开发中经常使用的一种: HTML代码: <div class="container"> <div class="m-lis ...