POJ2155 Matrix(二维树状数组||区间修改单点查询)
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 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
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的矩阵里(左上是(1,1)),初始点的值都为0,C(x1,y1,x2,y2)表示将这个矩阵的点都异或,Q(x,y)表示查询点的值(0或者1)。
思路:
常见的二维树状数组是单点更新,区间查询; 而这里是区间更新,单点查询。
由于是单点查询,这里直接用差分的思想做的:a[i][j]表示坐标(i,j)到(n,m)增加多少。
如果矩形(x1,y1,x2,y2)加一,则a[x1][x2]+1;a[x1][y2+1]-1;a[x2][y1+1]-1,a[x2][y2]+1;那么所求点(i,j)的值就是前缀和。
(但如果是区间更新,区间查询,则要像上一题那样推公式,最后得到5个一维树状数组。上一题是3个一维树状数组。)
- #include<cstdio>
- #include<cstdlib>
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- using namespace std;
- int n,m,a[][];
- int lowbit(int x){return x&(-x);}
- void add(int x,int y,int val)
- {
- for(int i=x;i<=n;i+=lowbit(i))
- for(int j=y;j<=n;j+=lowbit(j))
- a[i][j]+=val;
- }
- int query(int x,int y)
- {
- int res=;
- for(int i=x;i;i-=lowbit(i))
- for(int j=y;j;j-=lowbit(j))
- res+=a[i][j];
- return res;
- }
- int main()
- {
- int T,x1,x2,y1,y2;char opt[];scanf("%d",&T);
- while(T--){
- scanf("%d%d",&n,&m);
- memset(a,,sizeof(a));
- for(int i=;i<=m;i++){
- scanf("%s",opt);
- if(opt[]=='Q'){
- scanf("%d%d",&x1,&y1);
- printf("%d\n",&(query(x1,y1)));
- }
- else {
- scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
- add(x1,y1,);add(x2+,y2+,);
- add(x2+,y1,-);add(x1,y2+,-);
- }
- }
- if(T) printf("\n");
- } return ;
- }
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 ...
- 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询
题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...
- 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询
题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- 【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers
http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状 ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- 【树状数组区间修改单点查询】HDU 4031 Attack
http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后 ...
- POJ2155:Matrix(二维树状数组,经典)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
随机推荐
- SQL Server -使用表触发器记录表插入,更新,删除行数
1.如何使用sql获取当前session用户名和机器名 Select CURRENT_USER,Host_name() 2.如何在表触发器中获取当前表名称 SELECT OBJECT_SCHEMA_N ...
- 37:密码截取(回文串manacher算法)
题目描述:Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比如 ...
- SpringBoot启动流程分析(四):IoC容器的初始化过程
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- eclipse-jee版配置tomcat
Eclipse作为一款优秀的java开发开源IDE,集成了许多优秀的开发控件.下来我就如何安装eclipse及插件进行说明: 一.JDK安装 JDK是作为整个java的核心,包括运行环境,编译工具 ...
- 基于友善之臂ARM-tiny4412--uboot源代码分析
/* * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core * * Copyright (c) 2004 Texas Instrument ...
- Spark源码分析之八:Task运行(二)
在<Spark源码分析之七:Task运行(一)>一文中,我们详细叙述了Task运行的整体流程,最终Task被传输到Executor上,启动一个对应的TaskRunner线程,并且在线程池中 ...
- Android中处理崩溃异常和记录日志(转)
现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了崩溃现象,开 ...
- CentOS Python 安装MySQL-python
一.安装mysql yum list | grep mysql >>yum install -y mysql-server mysql mysql-devel CentOS 7的yum源中 ...
- 获得手机的ip
本文转载至 http://blog.csdn.net/showhilllee/article/details/8746114 iosip手机 貌似ASI里获取ip地址的链接不可以了.也曾试过 ...
- href=http:// href=// 的区别,src=http:// src=// 的区别。 链接里不带http,链接里直接使用双斜线 // 有什么不同。http://和//有什么区别?
其实很简单,当一个连接用双斜线 // 开头时表示如果浏览器当前使用的是https协议,那么就加载https协议的脚本,否则使用http,这保证了页面所有资源使用同一协议. 其实是有人将其做为规范来实践 ...