The article directories

  • Tree array
  • sample
    • The question
    • Analysis of the
    • code
  • summary

Tree array


  • What is a tree array? In simple terms, it is a violent traversal of a number to solve the interval problem, etc., but the traversal of the path is compressed with bit operation, complexity O(log2(n)) so that it does not time out. .
  • Lowbit () operation

    At its core is the magical Lowbit operation, lowbit(x)=x&(-x)The last 1 in the binary number of x, the principle is represented by the complement of negative numbers, which is the inverse plus one of the original code. For example, x = 6 = 00000110 (2), – x = x = 11111010 (2), then lowbit (x) = x & (x) = 10 (2) = 2.

    Lowbit () : a[x] : a[x] : a[x] : a[x] : a[x]



    Graphical:



    Then through a [] arrays, and can sum, such as sum (8) = [8], a sum (7) = a + a + [6] [7] a [4], the sum (6) = a [6] + [4], so not is the path of the compression?

    Similarly, to update ax, update a[]; for example, to update A3, change a[3] first; Then 3+lowbit(3)=4, change a[4]; Then 4+lowbit(4)=8, change a[8].
  • Two dimensional tree array

    Accordingly, we can derive a two-dimensional tree array, such as the interval input coordinates (x,y) obtained by updating points, and the yellow region of sum obtained by (u,v), as shown in the figure below:



    By figure comes easily to the yellow area for interval to sum (u, v) – the sum (x – 1, v) – the sum (u, y – 1) + sum (1 x, y)

sample


POJ-2155

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.

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
Copy the code

Sample Output:

1
0
0
1
Copy the code

The question

Invert one submatrix at a time in an n by n matrix (0 becomes 1,1 becomes 0) and ask several times whether a point is 0 or 1.

Analysis of the

Update the interval to find the point, with a two-dimensional tree array to solve, each update submatrix +1, the final point %2 to get the result. (actually is the idea of two-dimensional array simulation, compression path only).

code

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1003;
char s[5];
int a[maxn][maxn], n;
int lowbit(int x) {
	return (x & (-x));
}
void update(int x, int y) {
	for (int i = x; i <= n; i += lowbit(i)) {
		for (int j = y; j <= n; j += lowbit(j)) { a[i][j]++; }}}int sum(int x, int y) {
	int res = 0;
	for (int i = x; i > 0; i -= lowbit(i)) {
		for (int j = y; j > 0; j -= lowbit(j))
			res += a[i][j];
	}
	return res;
}
int main(a) {
	int ca, t;
	int x, y, u, v;
	scanf("%d", &ca);
	while (ca--) {
		memset(a, 0.sizeof(a));
		scanf("%d%d", &n, &t);
		while (t--) {
			scanf("%s%d%d", s, &x, &y);
			if (s[0] = ='C') {
				scanf("%d%d", &u, &v);
				x++, y++;
				u++, v++;
				update(u, v);
				update(x - 1, y - 1);
				update(x - 1, v);
				update(u, y - 1);
			}
			else {
				printf("%d\n".sum(x, y) % 2); }}printf("\n");
	}
	return 0;
}
Copy the code

summary


  1. Any problem that can be solved by tree array can be solved by line tree, but the programming complexity of tree array is lower, and the completion efficiency is higher! (Please ignore me)

Original is not easy, please do not reprint (this is not rich visits add insult to injury) blogger home page: blog.csdn.net/qq_45034708 If the article is helpful to you, remember to focus on the likes collection ❤