POJMatrix(二维树状数组)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 22058 | Accepted: 8219 |
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
二维树状数组,跟一维的差不多,
这个道题的思路就是看看x1,y1往上加一,同时方块右边,下面和右下方的区域再加1,只要保证他们那边加个偶数就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = + ;
int c[MAX][MAX];
int n;
int lowbit(int k)
{
return k & (-k);
}
void update(int x,int y,int num)
{
for(int i = x; i < n; i += lowbit(i))
{
for(int j = y; j < n; j += lowbit(j))
c[i][j] += num;
}
}
int sum(int x,int y)
{
int s = ;
for(int i = x; i > ; i -= lowbit(i))
{
for(int j = y; j > ; j -= lowbit(j))
s += c[i][j];
}
return s;
}
int main()
{
int t,q;
int num = ;
scanf("%d", &t);
while(t--)
{
if(num ++)
printf("\n");
scanf("%d%d", &n,&q);
memset(c,,sizeof(c));
char ch;
int x1,y1,x2,y2;
getchar();
while(q--)
{
scanf("%c", &ch);
if(ch == 'Q')
{
scanf("%d%d", &x1,&y1);
getchar();
int m = sum(x2,y2) - sum(x1 - , y2) - sum(x2,y1 - ) + sum(x1-,y1-);
if(m % == )
printf("0\n");
else
printf("1\n");
}
else if(ch == 'C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
getchar();
update(x1,y1,);
}
}
}
return ;
}
POJMatrix(二维树状数组)的更多相关文章
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [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: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- MooFest_二维树状数组
Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a s ...
随机推荐
- MySQL5.6 实现主从复制,读写分离,分散单台服务器压力
闲来无事,在本地搭建几台虚拟机,准备配一个mysql读写分离的主从配置,版本选用最新版的,mysql.5.6.28 版本,本处使用源码安装(鄙人一向喜欢源码安装,因为centos中鄙人不知道yum安装 ...
- 深入理解计算机系统(2.4)---C语言的有符号与无符号、二进制整数的扩展与截断
开篇请各位猿友允许LZ啰嗦几句,最近一直在写计算机系统原理这系列文章,也已经下定决心要把这本书的内容写完.主要目的其实是为了巩固LZ的理解,另外也想把这些内容分享给猿友们,毕竟LZ觉得这些内容对程序猿 ...
- 如何限制虚拟主机可使用的CPU资源
使用IIS 6.0运营虚拟主机的朋友们都会碰到这样一个问题,当某个网站占用大量CPU资源时,会把整个服务器都拖慢了,影响服务器上其他网站的访问速度,客户们的投诉也让系统管理员倍感头疼.我们知道,从II ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- webpack 插件: html-webpack-plugin
插件地址:https://www.npmjs.com/package/html-webpack-plugin 这个插件用来简化创建服务于 webpack bundle 的 HTML 文件,尤其是对于在 ...
- scrum阶段总结
项目预期计划: 确定编码规范. 根据原型图,设计并实现UI,添加各个界面,按钮,对话框,列表,窗口,导航等,理清各个界面的跳转逻辑. 学习测试技巧,编写测试用例. 实现需求文档中提出的功能,分别为:景 ...
- Android PNG透明图片转JPG格式背景变黑
Android PNG透明图片转JPG格式背景变黑 在上传图片是,需要把PNG格式转换成JPG格式的,但是在遇上透明背景时,转过来就变成黑色底图了! 原因是PNG支持透明图而 JPG格式不支持透明底 ...
- [转]CSS Display(显示) 与 Visibility(可见性)
CSS Display(显示) 与 Visibility(可见性) display属性设置一个元素应如何显示,visibility属性指定一个元素应可见还是隐藏. 隐藏元素 - display:non ...
- Django- 1- 数据库设置
更改配置文件中的 字段更改为 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', //按照自己的数据库配置配置,现在所配置 ...
- 【BZOJ 3672】【UOJ #7】【NOI 2014】购票
http://www.lydsy.com/JudgeOnline/problem.php?id=3672 http://uoj.ac/problem/7 链上的情况可以用斜率优化dp.树上用斜率优化d ...