poj----2155 Matrix(二维树状数组第二类)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 16950 | Accepted: 6369 |
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<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1005
#define lowbit(x) ((x)&(-x))
int aa[maxn][maxn];
int nn;
void ope(int x ,int y ,int val)
{
for(int i=x ;i> ;i-=lowbit(i))
{
for(int j=y ;j> ;j-=lowbit(j))
{
aa[i][j]+=val;
}
}
}
int clac(int x,int y)
{
int ans=;
for(int i=x;i<=nn ;i+=lowbit(i))
{
for(int j=y ;j<=nn ;j+=lowbit(j))
{
ans+=aa[i][j];
}
}
return ans;
}
struct node
{
int x;
int y;
}; int main()
{
int tt,xx;
char str[];
node sa,sb;
scanf("%d",&xx);
while(xx--)
{
memset(aa,,sizeof(aa));
scanf("%d%d",&nn,&tt);
while(tt--)
{
scanf("%s",&str);
if(str[]=='C')
{
scanf("%d%d%d%d",&sa.x,&sa.y,&sb.x,&sb.y);
sa.x--; //左上角全体加1
sa.y--;
ope(sb.x,sb.y,);
ope(sa.x,sb.y,-);
ope(sb.x,sa.y,-);
ope(sa.x,sa.y,);
}
else
{
scanf("%d%d",&sa.x,&sa.y);
printf("%d\n",clac(sa.x,sa.y)&);
}
}
printf("\n");
}
return ;
}
改进版..
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1005
#define lowbit(x) ((x)&(-x))
int aa[maxn][maxn];
int nn;
void ope(int x ,int y )
{
for(int i=x ;i> ;i-=lowbit(i))
{
for(int j=y ;j> ;j-=lowbit(j))
{
aa[i][j]=aa[i][j]^;
}
}
}
int clac(int x,int y)
{
int ans=;
for(int i=x;i<=nn ;i+=lowbit(i))
{
for(int j=y ;j<=nn ;j+=lowbit(j))
{
ans+=aa[i][j];
}
}
return ans;
}
struct node
{
int x;
int y;
}; int main()
{
int tt,xx;
char str[];
node sa,sb;
scanf("%d",&xx);
while(xx--)
{
memset(aa,,sizeof(aa));
scanf("%d%d",&nn,&tt);
while(tt--)
{
scanf("%s",&str);
if(str[]=='C')
{
scanf("%d%d%d%d",&sa.x,&sa.y,&sb.x,&sb.y);
sa.x--; //左上角全体加1
sa.y--;
ope(sb.x,sb.y);
ope(sa.x,sb.y);
ope(sb.x,sa.y);
ope(sa.x,sa.y);
}
else
{
scanf("%d%d",&sa.x,&sa.y);
printf("%d\n",clac(sa.x,sa.y)&);
}
}
printf("\n");
}
return ;
}
采用树状数组第一种方法
传统的方法:
代码:435ms
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1005
#define lowbit(x) ((x)&(-x))
int aa[maxn][maxn];
int nn;
void ope(int x ,int y )
{
for(int i=x ;i<=nn ;i+=lowbit(i))
for(int j=y ;j<=nn ;j+=lowbit(j))
aa[i][j]=aa[i][j]^;
}
int clac(int x,int y)
{
int ans=,i,j;
for(i=x;i> ;i-=lowbit(i))
for(j=y ;j> ;j-=lowbit(j))
ans+=aa[i][j];
return ans;
}
struct node
{
int x,y;
};
int main()
{
int tt,xx;
char str[];
node sa,sb;
scanf("%d",&xx);
while(xx--)
{
memset(aa,,sizeof(aa));
scanf("%d%d",&nn,&tt);
while(tt--)
{
scanf("%s",&str);
if(str[]=='C')
{
scanf("%d%d%d%d",&sa.x,&sa.y,&sb.x,&sb.y);
sb.x++; //左上角全体加1
sb.y++;
ope(sb.x,sb.y);
ope(sa.x,sb.y);
ope(sb.x,sa.y);
ope(sa.x,sa.y);
}
else
{
scanf("%d%d",&sa.x,&sa.y);
printf("%d\n",clac(sa.x,sa.y)&);
}
}
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 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 ...
随机推荐
- springmvc最简单的搭建,初学者必看
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- Java中间件:淘宝网系统高性能利器
[TechTarget中国原创]淘宝网是亚太最大的网络零售商圈,其知名度毋庸置疑,吸引着越来越多的消费者从街头移步这里,成为其忠实粉丝.如此多的用户和交易量,也意味着海量的信息处理,其背后的IT架构的 ...
- 表单提交的3种方式,http post的contentType
application/x-www-form-urlencoded:窗体数据被编码为名称/值对.这是标准的编码格式.这是默认的方式 multipart/form-data:窗体数据被编码为一条消息,页 ...
- Android之LogUtil
提供debug与release的时候是否屏蔽打印信息,把信息选择性的进行保存,可以是否自动保存crash的堆栈信息.来自github上的一个开源项目,https://github.com/syxc/L ...
- ELK+Filebeat 安装配置入门
本文地址 http://www.cnblogs.com/jasonxuli/p/6397244.html https://www.elastic.co 上,elasticsearch,logsta ...
- go语言基础之append函数的使用
1.append函数的使用 作用:在原切片的末尾添加元素 示例: package main //必须有个main包 import "fmt" func main() { s1 := ...
- Android wifi无线调试App新玩法ADB WIFI
Wifi 调试App已经不是什么新鲜的事情了,之前也看过不少,不是使用麻烦就是需要root权限,今个我给大家介绍一款好用的android studio 插件--ADB WIFI. 安装 setting ...
- C# WCF 完整实例,winform 窗体作为 宿主
上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...
- C# 事件(Event)
事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动等等,或者是一些出现,如系统生成的通知.应用程序需要在事件发生时响应事件.例如,中断.事件是用于进程间通信. 通过事件使用委托 事件在 ...
- IE 之 应用小结
1. 导出收藏夹(IE11) 导出:打开浏览器, 文件(F) → 导入和导出(M)... → 导出到文件(E) → 指定导出内容 → 指定保存路径导入:打开浏览器,文件(F) → 导入和导出(M).. ...