【POJ2155】【二维树状数组】Matrix
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
Source
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <map> const int MAXN = + ;
const int MAX = + ;
using namespace std;
int n, m;//m为操作次数
int C[MAXN][MAXN]; int lowbit(int x){return x&-x;}
/*int sum(int x, int y){
int cnt = 0, tmp;
while (x > 0){
tmp = y;
while (tmp > 0){
cnt += C[x][tmp];
tmp -= lowbit(tmp);
}
x -= lowbit(x);
}
return cnt;
}
void add(int x, int y, int val){
int tmp;
while (x <= 1000){
tmp = y;
while (tmp <= 1000){
C[x][tmp] += val;
tmp += lowbit(tmp);
}
x += lowbit(x);
}
return;
}*/
void add(int x,int y) {
int i,k;
for(i=x; i<=n; i+=lowbit(i))
for(k=y; k<=n; k+=lowbit(k))
C[i][k]++;
}
int sum(int x,int y) {
int i,k,cnt = ;
for(i=x; i>; i-=lowbit(i))
for(k=y; k>; k-=lowbit(k))
cnt += C[i][k];
return cnt;
} void work(){
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++){
char str[];
scanf("%s", str);
if (str[] == 'Q'){
int x, y;
scanf("%d%d", &x, &y);
//x++;y++;
printf("%d\n", sum(x, y)%);
}else if (str[] == 'C'){
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1++;y1++;x2++;y2++;
add(x2, y2);
add(x2, y1 - );
add(x1 - , y2);
add(x1 - , y1 - );
}
}
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d", &T);
while (T--){
memset(C, , sizeof(C));
work();
printf("\n");
}
return ;
}
【POJ2155】【二维树状数组】Matrix的更多相关文章
- poj2155二维树状数组
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 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- poj2155二维树状数组区间更新
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
- [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 ...
- 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(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】
这道题是一个二维树状数组,思路十分神奇,其实还是挺水的 题目描述 给定一个N∗NN∗N的矩阵AA,其中矩阵中的元素只有0或者1,其中A[i,j]A[i,j]表示矩阵的第i行和第j列(1≤i,j≤N)( ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
随机推荐
- 【转】Java ConcurrentModificationException 异常分析与解决方案--还不错
原文网址:http://www.2cto.com/kf/201403/286536.html 一.单线程 1. 异常情况举例 只要抛出出现异常,可以肯定的是代码一定有错误的地方.先来看看都有哪些情况会 ...
- 【转】Linux命令之查看文件占用空间大小-du,df
原文网址:http://blog.csdn.net/wangjunjun2008/article/details/19840671 du(disk usage),顾名思义,查看目录/文件占用空间大小 ...
- ubuntu下使用ngrok外网映射
好久之前想搞明白这个事情,可是就是不知道这个词叫外网映射,所以也一直不知怎么做,在慕课网看用java开发微信公众号的时候教程里提到了外网映射,查了一些资料终于把本地给映射到外网了,直接变成了80端口, ...
- 关于cocos2d和cocos2dx,还有iOS上的cocos2d的ARC问题
好吧,我承认这个我花了N个小时所做的努力都白费了. 事情的开始是这样的,今天在写cocos2dx的时候,测试发现总是出现溢出的问题,总是在main.m的autorelease报错.(好吧,如果我以后发 ...
- 关于python的开发工具——Canopy
在学习和使用scikit-learn过程中,官方文档中推荐了两个IDE(Canopy and Anaconda),我分别在win7和mac下安装后,发现Canopy是可以用的.Anaconda没搞明白 ...
- Away3D ATFTexture
之前在项目中贴图大量使用了 PNG 和 jpg 遇到了个问题.在使用BitmapTexture的时候发现 是必须MIP 不管你 是否开启或者关闭 MIP 他都会去创建.而每次MIP都会根据贴图大小去生 ...
- HDOJ 4696 Answers 乱搞
乱搞: rt.有1就能输出全部的数,否则仅仅能输出偶数 Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/1 ...
- Android GridView 一行显示数据(包括图片和文本),解决的办法是计算数据占该行的宽度是多少
最近在做图片的浏览功能,开始是使用Gallery做,但是,达不到我想要的效果,关于使用Gallery显示缩略图的缺点和优点,不在详述了.以下是一个完整的Demo代码,注意我的模拟器是640*960. ...
- UNIX环境高级编程第二版代码笔记
1. 第一个程序 gcc 1.1.c /tmp/ccbnJqcB.o: In function `main': 1.1.c:(.text+0x17): undefined reference to ...
- spring mvc DispatcherServlet详解之四---视图渲染过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...