POJ-2155 Matrix (二维树状数组 入门题)
来源:互联网 发布:3c认证淘宝 编辑:程序博客网 时间:2024/04/29 20:19
Matrix
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 29734 Accepted: 10865
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].
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.
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.
There is a blank line between every two continuous test cases.
Sample Input
12 10C 2 1 2 2Q 2 2C 2 1 2 1Q 1 1C 1 1 2 1C 1 2 1 2C 1 1 2 2Q 1 1C 1 1 2 1Q 2 1
Sample Output
1001
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <algorithm>using namespace std;int c[1002][1002];inline int lowbit(int x){return x & (-x);}void add(int x, int y, int n, int v){while(x <= n){int cur = y;while(cur <= n){c[x][cur] += v;cur += lowbit(cur);}x += lowbit(x);}}int query(int x, int y){int ans = 0;while(x){int cur = y;while(cur){ans += c[x][cur];cur -= lowbit(cur);}x -= lowbit(x);}return ans;}int main(){int T;scanf("%d", &T);while(T--){memset(c, 0, sizeof(c));int n, q, x1, x2, y1, y2;scanf("%d %d", &n, &q);char s;for(int i = 1; i <= q; ++i){getchar();s = getchar();if(s == 'C'){scanf("%d %d %d %d", &x1, &y1, &x2, &y2);add(x1, y1, n, 1);add(x1, y2 + 1, n, 1);add(x2 + 1, y1, n, 1);add(x2 + 1, y2 + 1, n, 1);}else{scanf("%d %d", &x1, &y1);printf("%d\n", query(x1, y1) % 2);}}if(T)printf("\n");}}/*题意:1000*1000的网格,5e4次操作,每次操作要么改变一个区块的数值,0改成1,1改成0;要么询问某点上得数字。思路:暴力的话是1000*1000*50000,突破天际。从复杂度来源上来看,可以在区块修改上做点文章。每个点上的数字只有0和1两种情况,每次修改是对整个区块做一个翻转,那么我们似乎只要做一个区块的翻转标记就好了,而不需要整体修改。按照题中的变量,假设原点在左上角,我们在(x1,y1)处做一个翻转标记,这个标记代表左上角为(x1,y1),右下角无限延生的一个区块都做了翻转。那么我们利用这个假设对一个有限区块做翻转标记只需要标记四个点就可以了?显然是的,那么怎么询问某个点(x, y)上当前的值呢?我们只需要统计(0, 0)到(x, y)中有多少个翻转标记即可,再结合初始值算一下。这个统计显然可以用二维树状数组来做统计。*/
阅读全文
0 0
- POJ-2155 Matrix (二维树状数组 入门题)
- poj 2155 Matrix(二维树状数组)
- poj -- 2155 Matrix (二维树状数组)
- POJ 2155 Matrix 二维树状数组
- POJ 2155 Matrix(二维树状数组)
- POJ 2155 Matrix ---二维树状数组
- poj 2155 Matrix 二维树状数组
- POJ 2155 Matrix (二维树状数组)
- poj 2155 Matrix (二维树状数组)
- POJ 2155 Matrix(二维树状数组)
- 【树状数组(二维)】poj 2155 Matrix
- POJ-2155 Matrix 二维树状数组
- POJ 2155 Matrix (二维树状数组)
- [poj 2155] Matrix(二维树状数组)
- Matrix - POJ 2155 二维树状数组
- POJ 2155 Matrix 二维树状数组
- POJ 2155 Matrix 【二维树状数组】
- POJ 2155 Matrix (二维树状数组)
- vue+webpack中动态绑定图片路径的3种方法
- 从网络上下载的Linux单文件程序,无法运行,用File查看出现 ELF 64-bit LSB executable
- DeepFashion: 服装公开数据集概述
- C/S和B/S两种架构区别与优缺点分析
- STM32内存大小与地址的对应关系以及计算方法
- POJ-2155 Matrix (二维树状数组 入门题)
- Cocopods在Mac ox 10.13报错
- Mybatis 缓存特性的使用及源码分析
- pb调用帮助文件的指定页面
- Asp.net之视图
- 机器学习要点(二):数据处理相关
- javaWeb文件上传
- flex布局中对齐项目和对齐内容的区别
- 数字图像去噪典型算法及matlab实现