POJ2155 Matrix 【二维树状数组】+【段更新点查询】
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17766 | Accepted: 6674 |
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
题意:对一个给定size且初始化为0的矩阵。运行一些命令,Q A B为查看arr[a][b]元素的值,C X1 Y1 X2 Y2为将(x1, y1) (x2, y2)矩形范围内的全部点0、1翻转。
题解:树状数组模式二的使用方法。段更新,点查询。update(x2, y2)表示从(1, 1)到(x2, y2)范围内的全部点都要翻转一次,可是这样会把给定范围外的一些点也翻转到,因此须要将这些点翻转回去。
#include <stdio.h>
#include <string.h>
#define maxn 1002 int size, tree[maxn][maxn]; int lowBit(int x){ return x & (-x); } //向下更新表示A[1]...A[i]每一个元素都要 += val,推广到二维同理
void update(int x, int y, int val)
{
int temp;
while(x > 0){
temp = y;
while(temp > 0){
tree[x][temp] += val;
temp -= lowBit(temp);
}
x -= lowBit(x);
}
} int query(int x, int y)
{
int sum = 0, temp;
while(x <= size){
temp = y;
while(temp <= size){
sum += tree[x][temp];
temp += lowBit(temp);
}
x += lowBit(x);
}
return sum;
} int main()
{
//freopen("stdin.txt", "r", stdin); int cas, q, a, b, c, d;
char com[2];
scanf("%d", &cas); while(cas--){
scanf("%d%d", &size, &q);
memset(tree, 0, sizeof(tree)); while(q--){
scanf("%s%d%d", com, &a, &b);
if(com[0] == 'C'){
scanf("%d%d", &c, &d);
update(c, b - 1, -1);
update(a - 1, d, -1);
update(a - 1, b - 1, 1);
update(c, d, 1);
}else printf("%d\n", query(a, b) & 1);
} if(cas) printf("\n");
}
return 0;
}
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 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- POJ2155:Matrix(二维树状数组,经典)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- 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 ...
- poj2155一个二维树状数组
...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
随机推荐
- 关于nested exception is org.apache.ibatis.binding.BindingException:Parameter '***' not found报错解决
几天晚上遇到的奇怪的问题 传入的参数名一直没有变 但是从mapper到xml似乎有一个找不到参数的报错,实际上只要在Mapper接口形参前加“@Param(“形参名称”)”就可以了
- 笔试算法题(42):线段树(区间树,Interval Tree)
议题:线段树(Interval Tree) 分析: 线段树是一种二叉搜索树,将一个大区间划分成单元区间,每个单元区间对应一个叶子节点:内部节点对应部分区间,如对于一个内部节点[a, b]而言,其左子节 ...
- [Python3网络爬虫开发实战] 1.7.3-Appium的安装
Appium是移动端的自动化测试工具,类似于前面所说的Selenium,利用它可以驱动Android.iOS等设备完成自动化测试,比如模拟点击.滑动.输入等操作,其官方网站为:http://appiu ...
- python3.x Day4 模块!!
json and pickle模块 用途是为了持久化信息,这种持久化方式可以和其他程序语言兼容,一般都支持json,json只能持久化数据,pickle是python特有的方式,可以持久化所有信息和数 ...
- Python之爬虫-中国大学排名
Python之爬虫-中国大学排名 #!/usr/bin/env python # coding: utf-8 import bs4 import requests from bs4 import Be ...
- 74-A/D指标,Accumulation/Distribution,积累/派发线,离散指标.(2015.7.1)
A/D指标,Accumulation/Distribution 积累/派发线,离散指标 观井映天 2015.7.1
- hadoop_exporter
1.下载安装go 1.下载二进制包:go1.4.linux-amd64.tar.gz. 2.将下载的二进制包解压至 /usr/local目录. tar -C /usr/local -xzf go1.4 ...
- 32道常见的Java基础面试题
1. 什么是 Java 虚拟机(JVM)?为什么 Java 被称作是“平台无关的编程语言”? Java 虚拟机是一个可以执行 Java 字节码的虚拟机进程.Java 源文件被编译成能被 Java 虚拟 ...
- matplotlib的使用--折线图--入门
目录 matplotlib应用介绍 一天天气变化图 两小时随机温度图 中文显示问题 个人交往统计图 多人交往统计图 总结 介绍: 举个例子(一天天气变化图): 假设一天中每隔两个小时(range(2, ...
- 九度oj 题目1056:最大公约数
题目1056:最大公约数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8068 解决:5317 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. ...