poj2155一个二维树状数组
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15125 | Accepted: 5683 |
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
一个二维树状数组,好像题目说错了,其实是左下角和右上角的。
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
long long int a[][];
int m;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int x1,int y1)
{
int i,j;
for(i=x1;i>;i-=lowbit(i))
for(j=y1;j>;j-=lowbit(j))
a[i][j]++; for(i=x-;i>;i-=lowbit(i))
for(j=y-;j>;j-=lowbit(j))
a[i][j]++; for(i=x1;i>;i-=lowbit(i))
for(j=y-;j>;j-=lowbit(j))
a[i][j]++; for(i=x-;i>;i-=lowbit(i))
for(j=y1;j>;j-=lowbit(j))
a[i][j]++;
}
int fun(int x,int y)
{
int i,j;
int sum=;
for(i=x;i<=m;i+=lowbit(i))
for(j=y;j<=m;j+=lowbit(j))
sum+=a[i][j];
return sum%;
}
int main()
{
//freopen("int.txt","r",stdin);
int n;
int i,j;
scanf("%d",&n);
for(i=;i<n;i++)
{
memset(a,,sizeof(a));
int k;
scanf("%d%d",&m,&k);
char b;
for(j=;j<k;j++)
{
cin>>b;
int x1,x2,y1,y2;
if(b=='Q')
{
scanf("%d%d",&x1,&y1);
printf("%d\n",fun(x1,y1));
}
else
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
update(x1,y1,x2,y2);
}
}
if(i!=n-)
printf("\n");
}
}
poj2155一个二维树状数组的更多相关文章
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ2155【二维树状数组,区间修改,点查询?】【又被输入输出坑】
这题反反复复,到现在才过. 这道题就是树状数组的逆用,用于修改区间内容,查询点的值. 如果单纯就这个奇偶数来判的话,似乎这个思路比较好理解. 看了一下国家集训队论文(囧),<关于0与1在信息学奥 ...
- 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 ...
- POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】
这道题是一个二维树状数组,思路十分神奇,其实还是挺水的 题目描述 给定一个N∗NN∗N的矩阵AA,其中矩阵中的元素只有0或者1,其中A[i,j]A[i,j]表示矩阵的第i行和第j列(1≤i,j≤N)( ...
- bzoj 1452: [JSOI2009]Count (二维树状数组)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1452 思路: 对每个颜色开一个二维树状数组维护就好了 实现代码: #include<b ...
- 【二维树状数组】计数问题 @JSOI2009/upcexam5911
时间限制: 1 Sec 内存限制: 128 MB 题目描述 一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入 ...
- BZOJ 1452 Count 【模板】二维树状数组
对每种颜色开一个二维树状数组 #include<cstdio> #include<algorithm> using namespace std; ; ][maxn][maxn] ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- C++栈和堆的生长方向
C++内存区域分为5个区域.分别是堆,栈,自由存储区,全局/静态存储区和常量存储区. 栈:由编译器在需要的时候分配,在不需要的时候自动清除的变量存储区.里面通常是局部变量,函数参数等. 堆:由new分 ...
- ABP 框架学习-01篇
从来没有自己写过太多的技术性文章,博客里面的文章都是拷贝别人的东西,做一个笔记功能给自己用的.最近觉得应该写点自己的学习博客 https://aspnetboilerplate.com/ ABP框架, ...
- JS自定义对象以及相关成绩系统完整案例演示
[自定义对象] 1.基本概念 ①对象是拥有一系列无无序属性和方法的集合 ②键值对:对象中的数据,用以键值对的形式存在,对象的每个属性和方法,都对应一个键值,以键取值 ③属性:描述对象特征的一系列变量称 ...
- 结构体(struct)大小
结构体(struct)大小 本文参考链接:C语言结构体(struct)常见使用方法,链接中的实例代码经实践有几处不准确,本文在引用时已做更改 注意:在结构体定义时不能申请空间(除非是结构体变量),不可 ...
- SNS团队第三次站立会议(2017.04.24)
一.当天站立式会议照片 本次会议主要内容:汇报工作进度,根据完成情况调整进度 二.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 相关数据库文件建立起来 完善数据库文件 龚晓婷 研 ...
- 201521123104 《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 1. List中指定元素的删除(题目4-1) 1.1 实验总结 这道题的关键是如何删除元素.一 ...
- 201521123016 《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 2.1代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...
- java is-a、has-a和like-a、组合、聚合和继承 两组概念的区别
is a 代表的是类之间的继承关系,比如PC机是计算机,工作站也是计算机.PC机和工作站是两种不同类型的计算机,但都继承了计算机的共同特性.因此在用 Java语言实现时,应该将PC机和工作站定义成两种 ...
- Java:java中BufferedReader的read()及readLine()方法的使用心得
BufferedReader的readLine()方法是阻塞式的, 如果到达流末尾, 就返回null, 但如果client的socket末经关闭就销毁, 则会产生IO异常. 正常的方法就是使用sock ...
- mongoose api 图表整理
一.背景 今天看 mongoose 的基础 API,参考了下面的链接做了图表以供查阅. 参考资料: http://www.cnblogs.com/xiaohuochai/p/7215067.html ...