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)
上一篇 2022年1月20日 下午6:00
下一篇 2022年1月20日 下午6:00


相关推荐

  • mysql 客户端_推荐一个好用的mysql的客户端工具?

    MySQL作为一个非常流行的关系型数据库,客户端软件其实非常多,下面我简单介绍几个,感兴趣的朋友可以尝试一下:Navicat这是一个比较常用的MySQL客户端软件,界面简洁、功能强大,可以直接查看数据库目录结构,建库建表也非常容易,支持SQL脚本导入导出、数据备份恢复及建模设计等,对于日常管理和维护MySQL数据库来说,是一个非常不错的工具:DBeaver这是一个非常有名的通用数据库管理…

    2022年4月4日
    67
  • Java多态原理

    Java多态原理Java 多态原理最近在准备面试 顺便复习以下 Java 最基础的东西仅作参考 Java 多态原理 Java 多态原理 0 什么是多态 1 jvm 内部类信息 2 多态的实现原理为了更好地理解多态的原理 首先必须对 jvm 内存模型 Java 方法调用等知识有点了解 0 什么是多态多态分为两种 本文着重讲解运行时多态 编译时多态 也叫做静态多态 指的是方法的重载 在同一个类中 同样的方法签名却有不同的参数 编译时通过静态绑定就能实现 运行时多态 也叫做动态多态 指的是方法的重写 在具有继承关系的类中 子类重

    2026年3月16日
    3
  • 第六章《MySQL查询》

    第六章《MySQL查询》

    2021年5月28日
    92
  • java注解-最通俗易懂的讲解

    java注解-最通俗易懂的讲解来源 秒懂 Java 注解 Annotation 你可以这样学 Annotation 中文译过来就是注解 标释的意思 在 Java 中注解是一个很重要的知识点 但经常还是有点让新手不容易理解 我个人认为 比较糟糕的技术文档主要特征之一就是 用专业名词来介绍专业名词 比如 Java 注解用于为 Java 代码提供元数据 作为元数据 注解不直接影响你的代码执行 但也有一些类型的

    2026年3月20日
    1
  • js 数组合并的方式

    js 数组合并的方式js 数组合并 letarr1 温情 刘聪 letarr2 杨和苏 邓紫棋 letarr3 周延 1 arr1 concat arr2 es5Array concat 合并两个数组 返回新数组 不会改变原数组 arr arr1 concat arr2 arr3 console log arr 温情 刘聪 杨和苏 邓紫棋 周延 2 arr1 arr2

    2026年3月17日
    3
  • 有没有自动生成代码的软件_源码编辑器作品展示

    有没有自动生成代码的软件_源码编辑器作品展示ACE:http://www.cnblogs.com/longboo/archive/2011/08/16/2140683.html#2187820官方网站:http://ace.ajax.org/https://github.com/cadorn/ace-extjs#readme

    2022年8月14日
    10

发表回复

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

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