poj 2155 Matrix(二维树树状数组)

Matrix

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 19113   Accepted: 7193

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

题意:给你个矩阵里面开始全是0,然后给你两种指令:1:‘C x1,y1,x2,y2’就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0,;2:‘Q x1 y1‘,输出a[x1][y1]的值。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int N = 1010;

int n, arr[N][N];

int lowbit ( int x )
{
    return x & ( -x );
}

void update ( int i, int j, int val )
{
    while ( i <= n )
    {
        int tmpj = j;
        while ( tmpj <= n )
        {
            arr[i][tmpj] += val;
            tmpj += lowbit ( tmpj );
        }
        i += lowbit ( i );
    }
}

int Sum ( int i, int j )
{
    int ans = 0;
    while ( i > 0 )
    {
        int tmpj = j;
        while ( tmpj > 0 )
        {
            ans += arr[i][tmpj];
            tmpj -= lowbit ( tmpj );
        }
        i -= lowbit ( i );
    }
    return ans;
}

int main()
{
    //freopen("input.txt","r",stdin);
    int t, q;
    scanf ( "%d", &t );
    char op[3];
    while ( t-- )
    {
        scanf ( "%d%d", &n, &q );
        memset ( arr, 0, sizeof ( arr ) );
        int x1, y1, x2, y2;
        while ( q-- )
        {
            scanf ( "%s", op );
            if ( op[0] == 'C' )
            {
                scanf ( "%d%d%d%d", &x1, &y1, &x2, &y2 );
                update ( x2 + 1, y2 + 1, 1 );
                update ( x2 + 1, y1, 1 );
                update ( x1, y2 + 1, 1 );
                update ( x1, y1, 1 );
            }
            else if ( op[0] == 'Q' )
            {
                scanf ( "%d%d", &x1, &y1 );
                printf ( "%d\n", Sum ( x1, y1 ) % 2 );
            }
        }
        if ( t )
            printf ( "\n" );
    }
    return 0;
}

时间: 2024-11-29 10:11:28

poj 2155 Matrix(二维树树状数组)的相关文章

POJ 2155 Matrix (二维线段树)

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18143   Accepted: 6813 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. I

POJ 2155 Matrix 二维线段树+标记永久化?

题意:链接 方法:二维线段树+标记永久化 解析:题意是比较明朗的,算一下内存发现4000^2也是可以接受的,于是就开始yy二维线段树? 对于第一层x的线段树,每个节点开一整棵y线段树. 用个二维数组就实现了? 不过发现个问题啊,这题怎么pushdown啊,标记传不下去啊.如果给x打个标记那么怎么知道y传那段啊? 于是就学了新的东西?标记永久化. 本题是单点查询嘛,标记永久化就应该是把相应的区间直接异或,不用往下传?那查询的时候怎么办呢?只需要在查询的时候把所有路过该点的区间都异或起来就OK了.貌

POJ - 2155 Matrix (二维树状数组 + 区间改动 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)

POJ - 2155 Matrix Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 ha

POJ 2155 Matrix 二维树状数组

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 19174   Accepted: 7207 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

POJ 2155 Matrix(二维树状数组,绝对具体)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 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

POJ 2155 Matrix(二维树状数组,绝对详细)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 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

POJ2155 Matrix二维线段树经典题

题目链接 二维树状数组 1 #include<iostream> 2 #include<math.h> 3 #include<algorithm> 4 #include<stdlib.h> 5 using namespace std; 6 #define ll long long 7 #define re(i,n) for(int i=0;i<n;i++) 8 const int maxn = 1007; 9 int c[maxn][maxn]; 10

poj2155 Matrix 二维线段树

二维线段树区间更新和单点查询,由于二维线段树不能传递标记,所以区间更新和一维不太一样,需要用到被更新的值以及更新操作的一些性质,还有要注意对query的影响. 这里操作是翻转,而且是单点查询,所以就直接在矩形块内更新,不把标记传递下去,查询的时候做一下微调,把所有经过的路径的标记都判断一遍,看是否需要翻转,这样就解决了这道题. 上一道是单点更新和区间求和,当然就不用标记了,把所有的第一维的第二层的y点全部更新,查询的时候不用查到最底层也能查到了. 二维线段树还是比较灵活,但这已经是最简单的树套树

POJ2155 Matrix二维线段树

一,题意: 给你一个全为0的N * N的矩阵,对这个矩阵有两个操作(对于矩阵只有两个状态0,1) (1):"C x1,y1,x2,y2"   就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转. (2):"Q x1 y1"   输出a[x1][y1]的值. 二,解析: 该我主要应用令二位的树状数组,一个是行,一个是列. 三,代码: #include<iostream> #include<cstdio> #include