【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description
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坐标图起初都为0,C:翻转左下和右上两个坐标围成的矩阵中所有点,Q:查询此点的0 1状态
【思路】
二维树状数组区间修改+单点查询模板题,先考虑一维。
一维单点查询就是前缀和,即query(x)。
区间修改先让s-n都加num,再让t+1-n减去num,即update(s, num),update(t+1, -num)。
二维的单点查询变成二维就好了query(x, y)。
区间修改update(x1, y1, num), update(x2+1, y1, -num), update(x1, y2+1, -num), update(x2+1, y2+1, num)。开始也是黑人问号,在纸上画几笔就看出来了。
这道题问得是0 1状态,那么只要统计翻转次数是偶次还是奇次就行了
【代码】
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = ;
int c[N][N], n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x, int y, int m)
{
for(int i = x; i <= n; i += lowbit(i))
for(int j = y; j <= n; j += lowbit(j))
c[i][j] += m;
}
int query(int x, int y)
{
int sum = ;
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j-= lowbit(j))
sum += c[i][j];
return sum;
}
int main()
{
int t, m;
cin>>t;
while(t--)
{
memset(c, , sizeof c);
scanf("%d%d", &n, &m);
while(m--)
{
char c;
scanf(" %c", &c);
if(c == 'C')
{
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
update(x1, y1, );
update(x1, y2+, -);
update(x2+, y1, -);
update(x2+, y2+, );
}
else
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query(x, y)&);
}
}
if(t) puts("");
}
return ;
}
【poj2155】Matrix(二维树状数组区间更新+单点查询)的更多相关文章
- POJ2155 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 an ...
 - POJ 2155 Matrix(二维树状数组+区间更新单点求和)
		
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
 - 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
		
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
 - [poj2155]Matrix(二维树状数组)
		
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
 - POJ-2155 Matrix---二维树状数组+区域更新单点查询
		
题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...
 - poj2155二维树状数组区间更新
		
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
 - poj----2155 Matrix(二维树状数组第二类)
		
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
 - HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
		
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
 - NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
		
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
 
随机推荐
- JSP--JSP语法--指令--include(动态包含/静态包含)--九大隐式对象--四大域对象--JSP内置标签--JavaBean的动作元素--MVC三层架构
			
一.JSP 原理:JSP其实就是一个servlet. Servlet负责业务逻辑处理,JSP只负责显示.开发中,JSP中不能有一行JAVA代码 二.JSP语法 1. JSP模板元素:JSP中HT ...
 - 配置YARN
			
1.配置yarn-site.xml(所有节点) 路径: /usr/local/hadoop-2.7.3/etc/hadoop/yarn-site.xml 配置项: <property> & ...
 - python全栈开发从入门到放弃之网络基础
			
一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...
 - Zend studio13 导入已有php文件夹
			
New -> orther -> faceted project 选好对应的文件夹 ,文件夹下的就都导入zend studio了.
 - JVM内存杂记1
			
大多数 JVM 将内存区域划分为 Method Area(Non-Heap)(方法区) ,Heap(堆) , Program Counter Register(程序计数器) , VM Stack( ...
 - java  小数转百分比
			
NumberFormat percent = NumberFormat.getPercentInstance(); percent.setMaximumFractionDigits(2); //保留多 ...
 - URAL 2078 Bowling game
			
题目: Bowling game In all asocial teams members ignore each other uniformly, each tight-knit team buil ...
 - 在线修改GTID模式
			
在线修改GTID模式 1. 在每一台机器上执行命令 SET @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN; 这是很重要的一步,必须确保服务器上没有违反GTID规范的 ...
 - 请求库之requests,selenium
			
requests模块 一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...
 - linux中的信号简介和trap命令
			
1.信号 linux通过信号来在运行在系统上的进程之间通信,也可以通过信号来控制shell脚本的运行 主要有一下信号 1 ##进程重新加载配置 2 ##删除进程在内存中的数据 3 ##删除鼠标在内存中 ...