zoukankan      html  css  js  c++  java
  • POJ

    Matrix

    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区域,m次操作 C 代表我们将区域转换状态,Q 为查询当前这个点状态
    思路:我们累加转换次数 偶数就还是为0 奇数为1 关键在于我们如何转换x,y,x1,y1这个区域。
    我们在翻转x,y,x1,y1的过程中我们也将x1,y1,n,n翻转了我们只需要将其他区域翻转回来即可 详细见代码
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<ctime>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    #define pb push_back
    #define pll pair<int,int>
    #define mp make_pair
    #define ll long long
    const int maxn = 1000+10;
    int tree[maxn][maxn];
    int n;
    int lowbit(int x)
    {
        return x&(-x);
    }
    void add(int x,int y)
    {
        for(int i=x; i<=n; i+=lowbit(i))
            for(int j=y; j<=n; j+=lowbit(j))
                tree[i][j]++;
    }
    int query(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+=tree[i][j];
        return ans;
    }
    int main()
    {
        int t,m,x1,x2,y1,y2;
        scanf("%d",&t);
        while(t--)
        {
            char ch[10];
            memset(tree,0,sizeof(tree));
            scanf("%d %d",&n,&m);
            while(m--)
            {
                scanf("%s",ch);
                if(ch[0]=='C')
                {
                    scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
                    add(x1,y1);
                    add(x1,y2+1);
                    add(x2+1,y1);
                    add(x2+1,y2+1);
                }
                else
                {
                    scanf("%d %d",&x1,&y1);
                    printf("%d
    ",query(x1,y1)&1);
                }
            }
            printf("
    ");
        }
    
    }
    View Code

     PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

  • 相关阅读:
    [daily][troubleshoot][archlinux][wps][font] wps文档中的图内容无法显示中文
    [troubleshoot][daily][archlinux][pacman] pacman 与 pip 包文件冲突
    [daily] 宇宙终极shell之zsh
    [knowledge][basic][hardware] 内存的硬件结构(转)
    [troubleshoot][archlinux][X] GPU HANG
    [daily]使用rdtsc指令,测量程序的运行速度 [转]
    [have_fun] 好玩哒小游戏又来啦
    [dpdk] 读开发指南(2)(内容长期整理中)
    [Virtualization][SDN] 讲的很好的SDN软件定义网络视频课程
    [Virtualization][SDN] VXLAN到底是什么 [转]
  • 原文地址:https://www.cnblogs.com/MengX/p/9350555.html
Copyright © 2011-2022 走看看