POJ - 2155 Matrix

Matrix

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34283 Accepted: 12403
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

多组输入。给你一个 n n n*n 的矩阵,初始化全部为0,然后两种操作。Q x y询问矩阵中x行y列的元素是多少。C x1 y1 x2 y2,改变(x1,y1)(左上角),(x2,y2)(右下角)所构成的矩阵的值,改变方式为取反。

树状数组
然而作为蒟蒻还是看了《浅谈信息学竞赛中的“0”和“1”》才会做orz…

创建一个二维的树状数组,存改变了多少次。
每一行一个树状数组,每一个列是一个以 行树状数组的值 为值树状数组。
每次改变一个矩阵内的值的时候,我们改变(x1,y1) , (x1,y2+1) , (x2+1,y1) , (x2+1,y2+1)的值,使这四个顶点的值都+1。然后每次查询时查询这个点到点(1,1)的总和,总和即使更改的次数。比如说查询一个在(x1,y1) , (x2,y2)矩阵内的点,它包括了(x1,y1),所以+1.查询(x1+1,y1+1),它包括了(x1,y1) , (x2+1,y2+1)所以值不变。

#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;

int a[1001][1001] = {},n;

void change(int x,int y)
{
	int t = y;
	while (x <= n)
	{
		y = t;
		while (y <= n)
		{
			a[x][y]++;
			y += y & -y;
		}
		x += x & -x;
	}
}

int main()
{
	int p;
	scanf("%d",&p);
	while (p--)
	{
		int m;
		scanf("%d%d",&n,&m);
		memset(a,0,sizeof(a));
		while (m--)
		{
			getchar();
			char c = getchar();
			if (c == 'C')
			{
				int x1,y1,x2,y2;
				scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
				change(x1,y1); change(x1,y2+1); change(x2+1,y1); change(x2+1,y2+1);
			}
			else if (c == 'Q')
			{
				int x,y,sum = 0;
				scanf("%d%d",&x,&y);
				int t = y;
				while (x > 0)
				{
					y = t;
					while (y > 0)
					{
						sum+=a[x][y];
						y -= y & -y;
					}
					x -= x & -x;
				}
				printf("%d\n",sum % 2);
			}
		}
		printf("\n");
	} 
} 

蒟蒻的线段树学习orz

猜你喜欢

转载自blog.csdn.net/EIP_silly/article/details/88785686
今日推荐