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 ...
随机推荐
- bzoj1832: [AHOI2008]聚会--LCA
本来觉得这是一道挺水的题目,后来觉得出题人挺变态的= = 半个小时敲完后,内存超限它给我看TLE,还是0ms,后来才发现内存限制64m 然后卡了一个小时后AC了.. 题目大意是在一棵树上找三点的最短路 ...
- ThinkPHP 3.2.3 自动加载公共函数文件的方法
方法一.加载默认的公共函数文件 在 ThinkPHP 3.2.3 中,默认的公共函数文件位于公共模块 ./Application/Common 下,访问所有的模块之前都会首先加载公共模块下面的配置文件 ...
- js判断字符是否包含字母汉字
<script type="text/javascript"> function check(str) { if (escape(str).indexOf(" ...
- ExtJS笔记 Ext.Loader
Ext.Loader is the heart of the new dynamic dependency loading capability in Ext JS 4+. It is most co ...
- nodejs 初入
nodejs 模块路径 1.内置模块 如果传递给require函数的是NodeJS内置模块名称,不做路径解析,直接返回内部模块的导出对象,例:require('http'). 2. nodejs 支 ...
- css使input中的值自动变大写
<style type="text/css"> input { text-transform:uppercase; } </style>
- LeetCode Graph Valid Tree
原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...
- CSS样式 让你的输入的小写自动变成大写。
1.transform:uppercase 参考:http://www.w3school.com.cn/cssref/pr_text_text-transform.asp 2.可以让你实现输入的小写 ...
- Windwos服务器远程桌面不能复制粘贴的解决方法
今天使用远程桌面连接登陆服务器,发现不能在本地电脑和远程服务器之间复制粘贴文件了,复制粘贴文本也不行. 网上搜了一下,主要有两种情况: 1.复制粘贴功能原本可以用,突然失灵了2.从头到尾都无法使用这个 ...
- [原创]NDT方法在SLAM中的应用
NDT概念 正态分布变换(Normal Distribution Transformation , NDT)概率密度函数( Probability Density Function, PDF)Firs ...