zoukankan      html  css  js  c++  java
  • POJ2155 树状数组

    Matrix
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 26650   Accepted: 9825

    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的矩阵初始每个元素值为0,左上角行列号最小,右下角行列号最大,两种操作:c x1 y1 x2 y2,将区间内的每个元素取非运算,Q x y表示询问xy点的值。
    代码:
    //可以画个图看一下,每次更新(x1,y1),(x2+1,y1),(x1,y2+1),(x2+1,y2+1)这4个点的值
    //时所有的点都不会被影响。求每个点的sum(并不真实),奇数是1,偶数是0.
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=1003;
    int A[maxn][maxn];
     int cas,n,m,x1,x2,y1,y2;
    int Lowbit(int x){
        return x&(-x);
    }
    void Add(int x,int y,int val)
    {
        for(int i=x;i<=1000;i+=Lowbit(i)){
            for(int j=y;j<=1000;j+=Lowbit(j)){
                A[i][j]+=val;
            }
        }
    }
    int Query(int x,int y)
    {
        int s=0;
        for(int i=x;i>0;i-=Lowbit(i)){
            for(int j=y;j>0;j-=Lowbit(j)){
                s+=A[i][j];
            }
        }
        return s;
    }
    int main()
    {
        scanf("%d",&cas);
        while(cas--){
            scanf("%d%d",&n,&m);
            memset(A,0,sizeof(A));
            char ch[3];
            while(m--){
                scanf("%s",ch);
                if(ch[0]=='C'){
                    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                    Add(x1,y1,1);
                    Add(x1,y2+1,1);
                    Add(x2+1,y2+1,1);
                    Add(x2+1,y1,1);
                }
                else{
                    scanf("%d%d",&x1,&y1);
                    int ans=Query(x1,y1);
                    //cout<<ans<<endl;
                    printf("%d
    ",ans&1);
                }
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    C#写文本文件,如何换行(添加换行符)
    C#使用oledb操作excel文件的方法
    winform之combobox
    vs2010快捷键
    可以下载一些书籍代码的网站
    对php和java里面的static函数和static的一些理解
    10.4-CMake find 模块
    6.25-Git 技巧
    6.4-Git Command
    2.25-CMake Tutorial
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6591394.html
Copyright © 2011-2022 走看看