POJ 2155 Matrix (二维树状数组)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17224 | Accepted: 6460 |
Description
We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.
1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2).
2. Q x y (1 <= x, y <= n) querys A[x, y].
Input
The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above.
Output
There is a blank line between every two continuous test cases.
Sample Input
1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1
Sample Output
1
0
0
1
很裸的题。
可以使用二维树状数组。
二维的写起来很方便,两重循环。
如果是要修改(x1,y1) - (x2,y2)的矩形区域。
那么可以在(x1,y1) 出加1,在(x2+1,y1)处加1,在(x1,y2+1)处加1,在(x2+1,y2+1)处加1 。。
画个图就知道了,查询单点就是求和。
/* ***********************************************
Author :kuangbin
Created Time :2014/5/23 22:34:04
File Name :E:\2014ACM\专题学习\数据结构\二维树状数组\POJ2155.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
int lowbit(int x)
{
return x&(-x);
}
int c[MAXN][MAXN];
int n;
int sum(int x,int y)
{
int ret = ;
for(int i = x;i > ;i -= lowbit(i))
for(int j = y;j > ;j -= lowbit(j))
ret += c[i][j];
return ret;
}
void add(int x,int y,int val)
{
for(int i = x;i <= n;i += lowbit(i))
for(int j = y;j <= n;j += lowbit(j))
c[i][j] += val;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
int q;
scanf("%d%d",&n,&q);
memset(c,,sizeof(c));
char op[];
int x1,y1,x2,y2;
while(q--)
{
scanf("%s",op);
if(op[] == 'C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(x1,y1,);
add(x2+,y1,);
add(x1,y2+,);
add(x2+,y2+,);
}
else
{
scanf("%d%d",&x1,&y1);
if(sum(x1,y1)% == )printf("0\n");
else printf("1\n");
}
}
if(T > )printf("\n");
}
return ;
}
POJ 2155 Matrix (二维树状数组)的更多相关文章
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- poj 2155 Matrix (二维树状数组)
题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- POJ 2029 (二维树状数组)题解
思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- poj 2155 B - Matrix 二维树状数组
#include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...
- POJ2155:Matrix(二维树状数组,经典)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- Matrix 二维树状数组的第二类应用
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17976 Accepted: 6737 Descripti ...
随机推荐
- 2.b统计字符串长度
import java.util.*;public class Main { public static void main(String args[]){ String a; Sc ...
- [cyber security][php]pfSense目录遍历漏洞分析
0×00 导言 pfSense是一个基于FreeBSD,专为防火墙和路由器功能定制的开源版本. 在本文中,我们将向大家介绍在pfSense的2.1.3以及更低版本中的CVE-2014-4690漏洞:对 ...
- ecshop教程:重置后台密码MD5+salt
ecshop密码加密方式: MD5 32位+salt,简单来说就是明文密码用MD5加密一次,然后在得到的MD5字符后边加上salt字段值(salt值为系统随机生成,生成以后不再改变)再进行一次MD5加 ...
- repeater没有数据显示暂无数据,无记录
方法就是在FooterTemplate加个Label并根据repeater.Items.Count判断是否有记录.关键代码如下: <FooterTemplate> <asp:Labe ...
- 安全快速修改Mysql数据库名的5种方法
1. RENAME DATABASE db_name TO new_db_name这个..这个语法在mysql 5.1.7中被添加进来,到了5.1.23又去掉了.据说有可能丢失数据.还是不要用的好.详 ...
- JSTL的test里的逻辑判断不能有空格
前天遇到了一个bug,有一个jstl的判断语句怎么都进不去,看了半天都没有发现什么问题,最后使用最原始的方式,一行一行的删除代码,重要找到了原因,原来是jstl的test逻辑判断里面不能有空格导致的.
- help和dir函数
help()函数是查看函数或模块用途的详细说明,比如:help('re'),help('re.split') 而dir()函数是查看函数或模块内的操作方法都有什么,输出的是方法列表.
- winform中DataGrid控件的宽度设置
最近修改一个win5.0的PDA程式,碰到一个问题.就是给DataGrid控件绑定数据的时候,这个控件的宽度不能调整,有时候数据较长,就显示不全.然后想在程式里自定义它的宽度,设置不成功.然后网上没找 ...
- 用file上传文件时,浏览器返回值总是自动添加<pre>的解决办法
在返回的JSon字符串里面总是莫名其妙的添加了<pre>标签,例如返回内容为"ok",在浏览器里面就变成了"<pre style="word- ...
- 约瑟夫(环)问题(Josephus problem)
问题描述:皇帝决定找出全国中最幸运的一个人,于是从全国选拔出 n 个很幸运的人,让这 n 个人围着圆桌进餐,可是怎么选择出其中最幸运的一个人呢?皇帝决定:从其中一个人从 1 开始报数,按顺序数到第 k ...