zoukankan      html  css  js  c++  java
  • Matrix_二维树状数组

    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

    【题意】给出一个n*n的方阵,刚开始都为0;然后q个要求,c x1 y1 x2,y2表示左上角(x1,y1)右下角(x2,y2)的这个矩形里面的各方格与现在状态相反(即0变1,1变0),Q x1 y1问(x1,y1)的状态。

    【思路】二维树状数组,每次状态变化都加一,最后对2取余

    参考资料:http://www.cnblogs.com/lvpengms/archive/2010/04/24/1719133.html

    当对(x1,y1),(x2,y2)区间置反时,需要改动四个地方就是4个角就可以了。为什么呢?如下图,假设A区未需要置反的区域,因为改动A区的左上角时,由树状数组的性质知:A,B,C,D4个区域都是要被置反的,所以在依次置反BD,CD,D,这样,置反的总过程为ABCD,BD,CD,D,这样我们就会发现结果对2取模时,只有A区被置反,B,C,D三个区都没有变化。明白原理之后就好做了。

    A

    B

    C

    D

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    const int N=1005;
    int c[N][N];
    int q,n;
    int lowbit(int x)
    {
        return x&(-x);
    }
    int get_sum(int x,int y)
    {
        int ans=0;
        for(int i=x;i>0;i-=lowbit(i))
            for(int j=y;j>0;j-=lowbit(j))
            ans+=c[i][j];
        return ans;
    }
    void update(int x,int y,int data)
    {
        for(int i=x;i<=n;i+=lowbit(i))
            for(int j=y;j<=n;j+=lowbit(j))
            c[i][j]+=data;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&q);
            memset(c,0,sizeof(c));
            char op[10];
            int x1,y1,x2,y2;
            while(q--)
            {
                scanf("%s",op);
                if(op[0]=='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",&x1,&y1);
                    if(get_sum(x1,y1)%2==0) puts("0");
                    else puts("1");
                }
            }
            if(t>0) puts("");
        }
        return 0;
    }
  • 相关阅读:
    Python写出LSTM-RNN的代码
    TensorFlow 实现 RNN 入门教程
    RNN与应用案例:注意力模型与机器翻译
    RNN入门
    内积(又名点积)
    词袋模型(BOW, bag of words)
    softmax
    Dropout
    随机梯度下降法
    L1范式和L2范式
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5925980.html
Copyright © 2011-2022 走看看