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
题解:这个PDF讲的挺好-> 浅谈信息学竞赛中的“0 ” 和 “1” ——二进制思想在信息学竞赛中的应用
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; int a[][];
int n,m;
char s[]; int lowbit(int x)
{
return x & (-x);
} int get(int x,int y)
{
int sum=;
for(int i=x; i>; i-=lowbit(i)){
for(int j=y; j>; j-=lowbit(j)){
sum+=a[i][j];
}
}
return sum;
} void add(int x,int y)
{
for(int i=x; i<=n; i+=lowbit(i)){
for(int j=y; j<=n; j+=lowbit(j)){
a[i][j]++;
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));
while(m--){
scanf("%s",s);
if(s[]=='C'){
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(x2+,y2+);
add(x1,y1);
add(x1,y2+);
add(x2+,y1);
}
else{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",get(x,y)%);
}
}
if(t)
printf("\n");
}
return ;
}
Matrix (二维树状数组)的更多相关文章
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 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 ...
- 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 ...
- poj 2155 Matrix (二维树状数组)
题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- Matrix 二维树状数组的第二类应用
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17976 Accepted: 6737 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 ...
- poj 2155 B - Matrix 二维树状数组
#include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...
随机推荐
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- 最简单的视频编码器:基于libx265(编码YUV为H.265)
===================================================== 最简单的视频编码器系列文章列表: 最简单的视频编码器:编译 最简单的视频编码器:基于libx ...
- 辛星和你解读PHP递归
事实上递归成其他的编程语言可以是初学者练习功能,但由于PHP特殊性,我们把它拿出来具体解释什么.关于什么是递归先说说.我是第一个承认正在寻求一些递归阶乘,例如,我们写一个函数,然后问多少的阶乘. 看以 ...
- makefile知识点归纳的
以一个样例開始,文件文件夹结构例如以下 ---------(当前文件夹)-----------main.c | |------- ...
- 深入浅出JMS(一)——JMS简要
假设手机只能实时通话.没有邮件和短信功能发生?一个电话回来.只是没有足够的时间去连接.然后传递这款手机的信息肯定是不接受. 么不能先将信息存下来.当用户须要查看信息的时候再去获得信息呢?伴随着这个疑惑 ...
- 【Linux探索之旅】第一部分第三课:测试并安装Ubuntu
内容简介 1.第一部分第三课:测试并安装Ubuntu 2.第一部分第四课预告:磁盘分区 测试并安装Ubuntu 大家好,经过前两个比较偏理论(是否想起了带着瓜皮帽,手拿折扇的老学究,或者腐儒)的课程, ...
- SQL Server管理员专用连接的使用
原文:SQL Server管理员专用连接的使用 作为一名DBA,经常会处理一些比较棘手的服务无响应问题,鉴于事态的严重性,多数DBA可能直接用“重启”大法,以便尽快的恢复生产环境的正常运转,但是多数情 ...
- 【Android基础】eclipse常用快捷键
Alt+/ 内容辅助键--列出相关内容 Ctrl+shift+o 导入所需的所有类包 Ctrl+1 对现有错误的n中解决方案 Crtl+d 删除所在行或者全部 CTRL+alt+上下键 上下复制 Cr ...
- 系统如何端子app弄root才干
最近由于调试USB OTG怪东西.这导致USB端口被占用,这项.虽然我是project版本号,但不能运行adb shell,这是不可能的debug该. 所以,我现在是一个系统终端apk,规划 http ...
- 【原创】构建高性能ASP.NET站点 开篇
原文:[原创]构建高性能ASP.NET站点 开篇 构建高性能ASP.NET站点 开篇 前言:有段时间没有写ASP.NET的东西了,心里总是觉得缺少了什么,毕竟自己对ASP.NET还是情有独钟的. 在本 ...