题目描述

给出一个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. cloudsim安装,配置(到eclipse)

    现在基本成功了.所以将这个过程尽量详细的,准确的分享出来,以供大家的需要.       一.Jdk,Eclipse的安装与配置. 本人下载的jdk版本是1.8,jdk的相关配置网上有很多,我就不赘述了 ...

  2. bootstrap日期插件

    <!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...

  3. xml、文件操作功能类

    我一个项目中用到的,里面的方法不是太通用,但是可以从里面找到一些有用的代码,以后慢慢添补更新: FileUtil.xml package com.novel.util; import java.io. ...

  4. 打开网页自动弹出QQ对话框的实现办法

    Ian今天偶然进入一个公司的企业网站,然后QQ聊天窗口这里马上就弹出与那个公司客服聊天的窗口.怀着好奇的心态,Ian分析了该公司的网站源码,发现了实现网页弹出qq对话框的原理与实现方法,相信此时此刻你 ...

  5. fopen w c

    http://php.net/manual/en/function.fopen.php

  6. Java 隐藏和覆盖

    我们知道,在JAVA中,子类可以继承父类,如果子类声明的方法与父类有重名的情况怎么办,大伙儿都知道要是重写,但是实际上这又分为两种情况,就是方法和变量在继承时的覆盖和隐藏问题,这些概念性的东西看似无聊 ...

  7. 关于队列queue

    1.在多线程和多进程中都有queue.调用方式不同,使用方式一致: 线程中: import queue q = queue.Queue(maxsize = 2) 进程中: from multiproc ...

  8. C语言 ---- 数组 iOS学习-----细碎知识点总结

    #pragma mark - 数组:用来存放同一数据类型的数据 // 数组的定义:类型说明符 数组名[常量表达式] = {值1, 值2, 值3...};    // 定义一个float类型的数组,用来 ...

  9. VS2012+Win7网站发布详细步骤

    VS2012+Win7网站发布详细步骤 本机环境: 本文分三个部分介绍Web项目发布的常规方法,大神级别可以略过,主要是为了方便一些初学者. 第一部分:VS2012把项目发布到文件系统. 第二部分:I ...

  10. ios -- 教你如何轻松学习Swift语法(一)

    目前随着公司开发模式的变更,swift也显得越发重要,相对来说,swift语言更加简洁,严谨.但对于我来说,感觉swift细节的处理很繁琐,可能是还没适应的缘故吧.基本每写一句代码,都要对变量的数据类 ...