poj2155 树状数组 Matrix
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14826 | Accepted: 5583 |
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
树状数组好强大,在这一题中,说是原始都是一些0 1,通过操作可以使1变0 0变1,在这一题中,我们可以发现,是一个区间的更新,然后是,求得一个点的值,这和我们一般用法刚好相反,其实,我们可以转换角度,其实,如果,是从最上向下更新,然后从下到上求和,这样,我们不就把一个点的值,转化成了求一个区间的值了么?也就基于这样的思想,我们在实际的用法中,要注意把向下的时候,+1然后,在重叠处-1,画画图就知道了!说也说不清楚!很好的题啊!原本是想弄线段树的,但是有的复杂,还有可以暴内存!
#include<iostream>
#include <string.h>
#include<stdio.h>
using namespace std;
#define MAXN 1005
int n;
int matrix[MAXN][MAXN];
int lowbit(int x)
{
return x&(-x);
}
int change(int x,int y,int val)//从上到下更新
{
int i,j;
for(i=x;i<=n;i=i+lowbit(i))
for(j=y;j<=n;j=j+lowbit(j))
{
matrix[i][j]+=val;
}
return 0;
}
int getsum(int x,int y)//从下向上求和
{
int i,j,re=0;
for(i=x;i>0;i=i-lowbit(i))
for(j=y;j>0;j=j-lowbit(j))
{
re+=matrix[i][j];
}
return re;
}
int main ()
{
int tcase,ask,x1,x2,y1,y2;
char c;
scanf("%d",&tcase);
while(tcase--)
{
memset(matrix,0,sizeof(matrix));
scanf("%d%d",&n,&ask);
getchar();
while(ask--)
{
c=getchar();
if(c=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x2++;y2++;
change(x1,y1,1);
change(x2,y2,1);
change(x1,y2,-1);
change(x2,y1,-1);
}
else
{
scanf("%d%d",&x1,&y1);
printf("%d\n",1&getsum(x1,y1));//判定奇偶
}
getchar();
}
printf("\n"); }
return 0;
}
poj2155 树状数组 Matrix的更多相关文章
- POJ2155 树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26650 Accepted: 9825 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----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 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 ...
- 【poj2155】【Matrix】二位树状数组
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34310873 Description Given ...
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- POJ2155 Matrix 【二维树状数组】+【段更新点查询】
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17766 Accepted: 6674 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)
题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出 ...
- poj 3792 Area of Polycubes (简单模拟)
题目 题意:在三维坐标系中,给定n个立方体的中心坐标,立方体的边长为1,按照输入顺序,后来输入的必须和之前输入的立方体有公共的边. 而且,不能和之前输入的立方体相同. 如果满足条件,输出表面积.如果不 ...
- hdu 3433 A Task Process(dp+二分)
题目链接 题意:n个人, 要完成a个x任务, b个y任务. 求,最短的时间 思路:由于时间较大,用 二分来找时间. dp[i][j]表示 i个人完成j个x任务, 最多能完成的y任务个数 这个题 不是很 ...
- I.MX6 git patch
/********************************************************************** * I.MX6 git patch * 说明: * 之前 ...
- Java之UncaughtExceptionHandler
概述: UncaughtExceptionHandler是为了捕获没有被捕获的异常,包括运行时异常,执行错误(内存溢出等),子线程抛出的异常等,你可以在uncaughtException(xx)里对后 ...
- Android-监听sdcard状态
public class MyService extends Service { private static final String TAG = "MyService"; Fi ...
- JavaScript基础篇最全
本章内容: 简介 定义 注释 引入文件 变量 运算符 算术运算符 比较运算符 逻辑运算符 数据类型 数字 字符串 布尔类型 数组 Math 语句 条件语句(if.switch) 循环语句(for.fo ...
- C# 一次查询多表,填充DataSet并指定表名
lhrhi 原文 NET 一次查询多表,填充DataSet并指定表名(DataSet指定DataTable名称的技巧) 现实中的场景,有时可能需要一次查询数据库中表张.在使用SqlDataAdapte ...
- java多线程学习笔记——详细
一.线程类 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...
- android 滑动菜单SlidingMenu的实现
首先我们看下面视图: 这种效果大家都不陌生,网上好多都说是仿人人网的,估计人家牛逼出来的早吧,我也参考了一一些例子,实现起来有三种方法,我下面简单介绍下: 方法一:其实就是对Gesture ...