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 ...
随机推荐
- Android网络请求之OkHttp框架
首先声明权限 <uses-permission android:name="android.permission.INTERNET"/> 在build.gradle中加 ...
- 用java在客户端读取mongodb中的数据并发送至服务器
使用Java自带的socket端口来实现,程序如下: Client.java package com.cn.gao; import java.net.*; import java.io.*; impo ...
- 在Java程序中使用Hibernate
Hibernate是一种ORM框架,ORM全称为Object-Relative Database-Mapping,在Java对象与关系数据库之间建立某种映射,以实现直接存取Java对象(一般为实体类) ...
- [4] 圆锥(Cone)图形的生成算法
顶点数据的生成 bool YfBuildConeVertices ( Yreal radius, Yreal height, Yuint slices, YeOriginPose originPose ...
- 第十二章 springboot + mongodb(复杂查询)
简单查询:使用自定义的XxxRepository接口即可.(见 第十一章 springboot + mongodb(简单查询)) 复杂查询:使用MongoTemplate以及一些查询条件构建类(Bas ...
- QtWebKit
WekKit官网:http://www.webkit.org/ QtWebKit官网及安装:http://trac.webkit.org/wiki/QtWebKit#GettingInvolved Q ...
- Candy leetcode java
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- 超酷的响应式dribbble设计作品瀑布流布局效果
相信做设计的朋友肯定都知道dribbble.com,它是一个非常棒的设计师分享作品的网站,全世界数以万计的设计高手和行家都在这个网站上分享自己的作品,当然,如果你常在上面闲逛的话,经常得到一些免费的好 ...
- [转]VirtualBox 修改UUID实现虚拟硬盘复制
-------------------------------------------------------------------- 原文:https://www.cnblogs.com/find ...
- ios之开发者须知常见简写英文代表的含义
<span style="white-space:pre"> </span> //NS基本 //MK地图 //CG图形绘制 //AV视音频 //UI视图 / ...