poj 2155 Matrix(二维树状数组,好题)中等难度题目,更新区域,查询单点

本文主要是介绍poj 2155 Matrix(二维树状数组,好题)中等难度题目,更新区域,查询单点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、http://poj.org/problem?id=2155

2、题目大意:

有一个n*n的矩阵,初始值时0,现在对该矩阵做两种操作,C x1 y1 x2 y2,是将这一区域的值是0的换成1,是1的换成0,Q x y查询(x,y)值是多少?

这道题目一看很简单,抬手就写了个for循环,相当于5000*1000*1000的循环,果断超时了,这样的题目就要想到用树状数组来做了,但是这道题目跟以前的树状数组还是不同的,这个是更新一个区域的值,查询一个点的值,想都想不出来哪里可以用树状数组来做,下面看网上大神的思路,很奇妙的。。。

经典的二维树状数组

这道题跟一般的树状数组操作有点不同,它是修改一片区域的值,求的是其中的一个点,我们可以这样来想!!

当我们修改一片区域的时候,我们可以分段去修改,也就是想当于树状数组中的 getsum(x,y)

当我们求其中的一个点的时候,我们可以变为统计这个点的翻转次数,凡是跟这个点相关的区间都要统计一下,

比如说在一维的情况中,

我们要使区间(a, b)内的点 + c,只需要使区间(1, b)内的点+c,而区间(1, a-1)内的点-c即可。

如果是二维的,修改矩阵(x1, y1)到(x2, y2),即(x2, y2)+c, (x1-1,y2)-c, (x2, y1-1)-c, (x1-1,y1-1)+c。

即我们可以用getsum(x, c)修改(1, x)这个区间内的点的值,而用update(x)来求该点的值

在这里c[]数组记录了翻转次数,只不过题目要求的是01变换,所以c[]只要记录0,1就行了,奇数为1,偶数为0

3、题目:

Matrix
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 16547 Accepted: 6227

Description

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N).

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 the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case.

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

For each querying output one line, which has an integer representing A[x, y].

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

POJ Monthly,Lou Tiancheng

 

4、二维树状数组AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005
int c[N][N];
int n;
int lowbit(int i)
{return i&(-i);
}
void getsum(int x,int y)
{for(int i=x;i>0;i-=lowbit(i)){for(int j=y;j>0;j-=lowbit(j)){c[i][j]=c[i][j]^1;}}
}
int update(int x,int y)
{//sum统计替换的次数,次数是偶数表示输出应该是0,否则是1int sum=0;for(int i=x;i<=n;i+=lowbit(i)){for(int j=y;j<=n;j+=lowbit(j)){sum+=c[i][j];}}if(sum%2==0)return 0;elsereturn 1;
}
int main()
{int t,m,x1,y1,x2,y2,x,y;char ch[3];scanf("%d",&t);int flag=0;while(t--){if(flag==0){flag=1;}elseprintf("\n");scanf("%d%d",&n,&m);memset(c,0,sizeof(c));for(int i=1;i<=m;i++){scanf("%s",ch);if(ch[0]=='C'){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);getsum(x2,y2);getsum(x1-1,y2);getsum(x2,y1-1);getsum(x1-1,y1-1);}else{scanf("%d%d",&x,&y);printf("%d\n",update(x,y));}}}return 0;
}
/*
2
2 10
C 2 1 2 2
Q 2 2
C 1 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 2 2 2
Q 1 1
C 1 1 2 1
Q 2 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
*/


 

5、简单实现超时的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005
int a[N][N];
int main()
{int t,n,m,x1,y1,x2,y2,x,y;char ch[3];scanf("%d",&t);int flag=0;while(t--){if(flag!=0){printf("\n");flag=1;}scanf("%d%d",&n,&m);memset(a,0,sizeof(a));for(int i=1;i<=m;i++){scanf("%s",ch);if(ch[0]=='C'){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);for(int i=x1;i<=x2;i++){for(int j=y1;j<=y2;j++){a[i][j]=a[i][j]^1;}}}else{scanf("%d%d",&x,&y);printf("%d\n",a[x][y]);}}}return 0;
}


 

 

这篇关于poj 2155 Matrix(二维树状数组,好题)中等难度题目,更新区域,查询单点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369641

相关文章

Security OAuth2 单点登录流程

单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自独立的软件系统,提供访问控制的属性。当拥有这项属性时,当用户登录时,就可以获取所有系统的访问权限,不用对每个单一系统都逐一登录。这项功能通常是以轻型目录访问协议(LDAP)来实现,在服务器上会将用户信息存储到LDAP数据库中。相同的,单一注销(single sign-off)就是指

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO