poj 2155- Matrix (树状数组,二维,更新区间,查询单点)

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 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

和上一道类似,也是更新区间,查询单点。用到了容斥原理。
/*************************************************************************
    > File Name: code/poj/2155.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年08月07日 星期五 00时42分38秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=1E3+7;
int c[N][N];
int n,m,x1,x2,y1,y2,x,y,t;

int lowbit ( int x)
{
    return x&(-x);
}
void update ( int x,int y ,int delta)
{
    for ( int i = x ; i  <= n ; i = i + lowbit(i))
    {
    for ( int j = y; j <= n ; j = j + lowbit(j))
    {
        c[i][j] = c[i][j] + delta;
    }
    }
}
int sum ( int x,int y)
{
    int res  = 0;
    for ( int i = x; i >= 1 ; i = i - lowbit (i))
    {
    for ( int j = y ; j >= 1 ; j = j - lowbit (j))
    {

        res  = res + c[i][j];
    }
    }
    return res;
}
int main()
{
    int T;
    cin>>T;
    while (T--)
    {
    memset(c,0,sizeof(c));
    scanf("%d %d",&n,&t);
    for ( int i = 1; i <=  t;  i ++ )
    {
        char cmd;
        cin>>cmd;
        if (cmd==‘C‘)
        {
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        update (x1,y1,1);
        update (x2+1,y1,1);
        update (x1,y2+1,1);
        update (x2+1,y2+1,1);
        }
        else
        {
        scanf("%d %d",&x,&y);
        int tmp;
        if (sum(x,y)%2==0)
            cout<<0<<endl;
        else cout<<1<<endl;
        }
    }
    cout<<endl;
    }

    return 0;
}
时间: 2024-10-10 08:35:25

poj 2155- Matrix (树状数组,二维,更新区间,查询单点)的相关文章

POJ 2155 Matrix (树状数组 &amp;&amp; 区间计数)

题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, y2)为右下角构成的矩形区域内的数全部进行取反操作, 即0变1.1变0.第二种是Q X Y, 表示查询现在这个矩阵的(X, Y)点到底是0还是1.总共有T次操作, 对于C操作进行相应的修改, 对于Q操作要对应输出! 分析 : 据说是楼教主出的题, 自己确实想不出什么高效的办法, 参考网上的题解, 才

poj 2155 Matrix(树状数组的应用)

http://poj.org/problem?id=2155 对于一个n*n(n <= 1000)的01矩阵,初始全为0,有两种操作. C x1 y1 x2 y2 ,分别代表矩阵的左上角和右下角,将这个矩阵中的01互换,原为0的变为1,原为1的变为0. Q x y询问A[x,y]现在是几. 因为只有01的互换,且原始为0,那么只需计算[x,y]处被换了几次就能确定现在这个格子是几.重点就是怎样快速计算[x,y]格子被换了几次.操作方法是将[x1,y1][x1,y2+1][x2+1,y1][x2+

HDU 1892(树状数组二维)

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 4167    Accepted Submission(s): 1311 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kn

树状数组 / 二维树状数组

一维树状数组 · 单点修改 + 单点查询: 直接使用即可 · 区间修改 + 单点查询: 另外维护一个维护前缀和的树状数组,查询时查询与原值相加即可. · 区间修改 + 区间查询: <div align=center> 居中 </div> 原文地址:https://www.cnblogs.com/Colythme/p/9853159.html

【树状数组二维区间加+区间查询模板】bzoj3132

新知识,其实和之前讲过的一维差不多,只要维护四个数组就行了,不过还是参考了别人的代码,还是要好好练练才行 #include<iostream> #include<cstdio> #include<cstring> #define maxn 2050 using namespace std; int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn],d[maxn][maxn]; int n,m; int lowbit(int x){ret

poj 2155 二进制0 1反转---二维树状数组

http://poj.org/problem?id=2155 上午自己搞了很久胡思乱想了很久,然后没思路-----看了论文<浅谈信息学竞赛中的"0"和"1"--二进制思想在信息学竞赛中的应用>,豁然开朗啊,,马上A掉---PE了一次o(╯□╰)o 通过论文学到的两点: 1.多维不会的时候,从一维尝试类比: 2.想法的证明,情况数不多的时候,分类讨论证明很好 #include <cstdio> #include <cstring>

POJ 2309 BST 树状数组基本操作

Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until t

Poj 2299 Ultra-QuickSort 树状数组 解法

本题的树状数组稍微有点特点,就是需要所谓的离散化一下,开始听这个名称好像很神秘的,不过其实很简单. 就是把一个数组arr的值,其中的值是不连续的,变成一组连续的值,因为这样他们的顺序是不变的,所以,不影响结果. 例如:9 1 0 5 4 ->变为:5 2 1 4 3看出他们的相对位置不变的. 9和5为最大值在第一个位置,1和2为第二大的值在第二个位置,0和1在第一个位置等,看出对应顺序了吗? 对,就是这么简单的方法, 就叫做离散化. 如果你对counting sort熟悉的话,那么这样的思想理解

树状数组成段更新——POJ 3468

A Simple Problem with IntegersCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3468 Description You have N integers, A1, A2, ... , AN. You need to deal wit

POJ 2481 Cows(树状数组)

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. Farmer John has N cows (we number the cows from 1 to N). Ea