zoukankan      html  css  js  c++  java
  • POJ2155 Matrix <树套树/二维树状数组>

    Matrix
    Time Limit: 3000MS Memory Limit: 65536K

    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

    标签:线段树套线段树/二维树状数组

    此题最简单的方法是二维树状数组,但因为二维树状数组没太大用,所以练习线段树的树套树。
    此题用作树套树的模板题再合适不过。

    AC代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define MAX_N 1000
    using namespace std;
    int n, m;
    int tr[(MAX_N<<2)+5][(MAX_N<<2)+5];
    void modify_y(int v1, int v2, int s, int t, int l, int r) {
    	if (s >= l && t <= r) {
    		tr[v1][v2] ^= 1;
    		return;
    	}
    	int mid = s+t>>1;
    	if (l <= mid)	modify_y(v1, v2<<1, s, mid, l, r);
    	if (r >= mid+1)	modify_y(v1, v2<<1|1, mid+1, t, l, r);
    }
    void modify_x(int v, int s, int t, int x1, int y1, int x2, int y2) {
    	if (s >= x1 && t <= x2) {
    		modify_y(v, 1, 1, n, y1, y2);
    		return;
    	}
    	int mid = s+t>>1;
    	if (x1 <= mid)	modify_x(v<<1, s, mid, x1, y1, x2, y2);
    	if (x2 >= mid+1)	modify_x(v<<1|1, mid+1, t, x1, y1, x2, y2);
    }
    int query_y(int v1, int v2, int s, int t, int pos) {
    	if (s == t)	return tr[v1][v2];
    	int mid = s+t>>1;
    	if (pos <= mid)	return tr[v1][v2]^query_y(v1, v2<<1, s, mid, pos);
    	else	return tr[v1][v2]^query_y(v1, v2<<1|1, mid+1, t, pos);
    }
    int query_x(int v, int s, int t, int x, int y) {
    	if (s == t)	return query_y(v, 1, 1, n, y);
    	int mid = s+t>>1;
    	if (x <= mid)	return query_y(v, 1, 1, n, y)^query_x(v<<1, s, mid, x, y);
    	else	return query_y(v, 1, 1, n, y)^query_x(v<<1|1, mid+1, t, x, y);
    }
    int main() {
    	int T;
    	scanf("%d", &T);
    	while (T--) {
    		memset(tr, 0, sizeof(tr));
    		scanf("%d%d", &n, &m);
    		while (m--) {
    			char ch;
    			cin >> ch;
    			if (ch == 'C') {
    				int x1, y1, x2, y2;
    				scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    				modify_x(1, 1, n, x1, y1, x2, y2);
    			}
    			if (ch == 'Q') {
    				int x, y;
    				scanf("%d%d", &x, &y);
    				printf("%d
    ", query_x(1, 1, n, x, y));
    			}
    		}
    		printf("
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    ZooKeeper概述(转)
    ZooKeeper典型应用场景(转)
    部署与管理ZooKeeper(转)
    Hbase分布式安装部署过程
    HBase安装
    使用info命令查看Redis信息和状态
    java定时调度器解决方案分类及特性介绍
    谈谈应用层切面设计
    七层协议和四层协议
    HTTP协议详解
  • 原文地址:https://www.cnblogs.com/AzraelDeath/p/7561754.html
Copyright © 2011-2022 走看看