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)上的数全部 ...
随机推荐
- 分享一个 jmeter ant的build.xml
<?xml version="1.0" encoding="UTF-8"?> <project name="ant-jmeter-t ...
- 向GitHub 提交你的源代码
之前的这篇文章「Git入门篇」相信大家都已经对 Git 的基本操作熟悉了,但是这篇文章只介绍了对本地 Git 仓库的基本操作,今天我就来介绍下如何跟远程仓库一起协作,教你们向 GitHub 上提交你们 ...
- 学习js函数--函数定义
函数的定义方法有三种 1.函数表达式 2.函数声明 3,new function构造函数 这边主要看下函数表达式和函数声明 函数表达式(未省略标志的) var alertName = function ...
- C语言中复数运算及调用blas,lapack中复数函数进行科学计算
C语言中常用的数据类型主要int, float ,double ,char 等,但在科学运算中复数扮演着重要角色.这里讲下C语言中的复数运算以及如何调用blas,lapack库中的复数函数来进行科学计 ...
- maven 自我学习笔记
1.常用网站: maven.apache.org http://mvnrepository.com/ 2.命令 mvn -v 查看maven的版本 mvn -compile 在项目的根目录下编译项 ...
- PHP(函数)
<script> // 获得日 var time = new Date(); var x = time.getDate(); document.write(x+"日," ...
- 【beta】阶段 第七次 Scrum Meeting
每日任务 1.本次会议为第七次 Meeting会议: 2.本次会议在下午14:45,课间休息时间在陆大楼召开,召开本次会议为10分钟. 一.今日站立式会议照片 二.每个人的工作 (有work item ...
- 5th-个人总结(Alpha阶段)
一. 总结自己的Alpha过程 1.团队的整体情况 在团队中这次担任队长的职务. alpha阶段完成情况还算理想,大家都完成了指定的任务.但是也少不了犯错,一些需求没有划分的足够细致,后来功能完成后发 ...
- Swing-JList选择事件监听器ListSelectionListener-入门
当JList中的元素被选中时,选择事件将被触发.对于JTable也是一样,你可以把它看做是多个并列的JList.那么,如果程序需要对该事件做出响应,需要以下步骤: (1)创建一个实现了 ListSel ...
- C语言之算法初步(汉诺塔--递归算法)
个人觉得汉诺塔这个递归算法比电子老鼠的难了一些,不过一旦理解了也还是可以的,其实网上也有很多代码,可以直接参考.记得大一开始时就做过汉诺塔的习题,但是那时代码写得很长很长,也是不理解递归的结果.现在想 ...