POJ2155:Matrix(二维树状数组,经典)「建议收藏」

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

Source


#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <list>#include <algorithm>#include <climits>using namespace std;#define lson 2*i#define rson 2*i+1#define LS l,mid,lson#define RS mid+1,r,rson#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 1005#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit(x) (x&-x)const int mod = 1e9+7;int c[N][N],n,m,cnt,s,t;int a[N][N];int sum(int x,int y){    int ret = 0;    int i,j;    for(i = x;i>=1;i-=lowbit(i))    {        for(j = y;j>=1;j-=lowbit(j))        {            ret+=c[i][j];        }    }    return ret;}void add(int x,int y){    int i,j;    for(i = x;i<=n;i+=lowbit(i))    {        for(j = y;j<=n;j+=lowbit(j))        {            c[i][j]++;        }    }}int main(){    int i,j,x,y,ans,t;    int x1,x2,y1,y2;    char op[10];    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        MEM(c,0);        MEM(a,0);        while(m--)        {            scanf("%s",op);            if(op[0]=='C')            {                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                x1++,y1++,x2++,y2++;                add(x2,y2);                add(x1-1,y1-1);                add(x2,y1-1);                add(x1-1,y2);            }            else            {                scanf("%d%d",&x1,&y1);                x2 = x1,y2 = y1;                x1++,y1++,x2++,y2++;                printf("%d\n",sum(x1,y1));            }        }        printf("\n");    }    return 0;}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116489.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 单射双射与满射

    单射双射与满射单射(injection):每一个x都有唯一的y与之对应;满射(surjection):每一个y都必有至少一个x与之对应;双射(又叫一一对应,bijection):每一个x都有y与之对应,每一个y都有x与之对应。把x比作萝卜,y比作坑:单射就是一个萝卜一个坑,有的坑有可能没萝卜;满射就是所有坑都有萝卜,有的坑可能有不止一个萝卜;双射就是严格的一个萝卜一个坑,一个坑一个萝卜,所有萝卜都有坑,所有坑都有萝卜。…

    2022年6月6日
    38
  • cglib BeanCopier的使用

    cglib BeanCopier的使用一、概述  选择Cglib的BeanCopier进行Bean拷贝的理由是,其性能要比Spring的BeanUtils,Apache的BeanUtils和PropertyUtils要好很多,尤其是数据量比较大的情况下。  之前的一篇文章:Easy-mapper教程——模型转换工具提到了Cglib的BeanCopier使用ASM字节码生成技术,所以性能会非常好。  下面的文章内容直接整理…

    2025年9月13日
    6
  • android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)

    在做一个view背景特效的时候被坐标的各个获取方法搞晕了,几篇抄来抄去的博客也没弄很清楚。现在把整个总结一下。其实只要把下面这张图看明白就没问题了。涉及到的方法一共有下面几个:view获取自身坐标:getLeft(),getTop(),getRight(),getBottom()view获取自身宽高:getHeight(),getWidth()motionEvent获取坐标:getX(),getY

    2022年3月11日
    62
  • 发布版sha1怎么获取_sha1.rar

    发布版sha1怎么获取_sha1.rar获取SHA1值首先,绝大多数App在调试时使用的签名文件(debugkeystore)和最终App发布使用的签名文件(自定义的keystore)是不同的,不同签名文件的SHA1值也是不同的。下面提供几种获取SHA1值的方式:通过Eclipse编译器获取SHA1使用adt22以上版本,可以在eclipse中直接查看。Windows:依次在eclipse中打开Window-&gt;…

    2022年8月11日
    13
  • dex字符串解密_DEX文件混淆加密

    dex字符串解密_DEX文件混淆加密现在部分app出于安全性(比如加密算法)或者用户体验(热补丁修复bug)会考虑将部分模块采用热加载的形式Load。所以针对这部分的dex进行加密是有必要的,如果dex是修复的加密算法,你总不想被人一下就反编译出来吧。当然也可以直接用一个加密算法对dex进行加密,Load前进行解密就可以了,但是最好的加密就是让人分不清你是否加密了。一般逆向过程中拿到一个可以直接反编译成java…

    2022年6月27日
    197
  • 高德地图实现多点标注marker和动态信息窗体[通俗易懂]

    高德地图实现多点标注marker和动态信息窗体[通俗易懂]先说一下项目对地图的需求:在后台新增地图管理模块,要求,每一辆车都在地图上有标注,而且点击标注时要显示出车辆的相关信息,比如车牌和车辆的当前状态。下图就是实现的效果。当然从高德地图api也能查看到这一块,比如多点标注,简单信息窗体,这些都有api,可以先看官网的,然后,再看我这篇文章,怎么融合一起,记住数据全部来自后台数据库,这样才能保证一切数据都是动态的,可实时更新的。首

    2022年5月11日
    76

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号