题目描述

给出一个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. map的用法

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数:   map<stri ...

  2. 手机触屏触摸特效javascript-TouchSwipe(依赖于jquery库)中文说明

    by 郑州seo on 2013 年 7 月 6 日 in jquery, 网站建设 with 6 Comments 最近需要做一个手机小门户网站,因为目前主流的手机都是安卓和苹果的,他们的浏览器内核 ...

  3. linux笔记六-------文件权限设置

    1.文件(目录)权限本身划分:读read   写write   执行execute 权限从用户的角度划分:主人权限user    同组用户权限group    其他组用户的权限other music. ...

  4. MUI - 上拉刷新/下拉加载

    新闻信息列表必备的功能,支持Table,Ul等列表. 以下是DIV版本,在安卓端或者ios端必须使用双webview模式,传送门:http://dev.dcloud.net.cn/mui/pulldo ...

  5. 手动给控制器添加xib

    UIViewController绑定xib界面可视化,有两种方式: 1.第一种(自动化),在创建控制器时,勾选xib选项. 2.第二种手动创建一个Xib,然后再手动绑定到对应的控制器上

  6. java.io包中的字节流—— FilterInputStream和FilterOutputStream

    接着上篇文章,本篇继续说java.io包中的字节流.按照前篇文章所说,java.io包中的字节流中的类关系有用到GoF<设计模式>中的装饰者模式,而这正体现在FilterInputStre ...

  7. php中htmlspecialchars和htmlentiti

    Certain characters have special significance in HTML, and should be represented by HTML entities if ...

  8. 使用 Git@OSC 管理代码

    开源中国的 git 服务的地址是:http://git.oschina.net/ 以下记录 push 本地已有的项目至 git@osc 的过程. ① 注册登录之后,创建一个自己的项目: 创建好的默认项 ...

  9. 【java基础学习】GUI

    GUI 容器 布局管理器 组件 菜单 事件处理机制

  10. Linked List Cycle II || LeetCode

    /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...