poj2155二维树状数组
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
题意:给一个0,1矩阵,有两种操作一种是把子矩阵进行非,一种是求子矩阵是否有1
题解:二维树状数组,用 add(x1,y1);
add(x1,y2+1);
add(x2+1,y1);
add(x2+1,y2+1);
求解时%2即可
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int c[N][N]; void add(int x1,int y1)
{
for(int i=x1;i<N;i+=i&(-i))
for(int j=y1;j<N;j+=j&(-j))
c[i][j]++;
}
int sum(int x,int y)
{
int ans=;
for(int i=x;i>;i-=i&(-i))
for(int j=y;j>;j-=j&(-j))
ans+=c[i][j];
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int n;
int t,k;
cin>>t;
while(t--){
cin>>n>>k;
memset(c,,sizeof c);
while(k--){
string s;
cin>>s;
if(s[]=='C')
{
int x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
add(x1,y1);
add(x1,y2+);
add(x2+,y1);
add(x2+,y2+);
}
else
{
int x,y;
cin>>x>>y;
cout<<sum(x,y)%<<endl;
}
}
cout<<endl;
}
return ;
}
poj2155二维树状数组的更多相关文章
- POJ2155(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- poj2155二维树状数组区间更新
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- 【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一个二维树状数组
...
- 【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 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】
这道题是一个二维树状数组,思路十分神奇,其实还是挺水的 题目描述 给定一个N∗NN∗N的矩阵AA,其中矩阵中的元素只有0或者1,其中A[i,j]A[i,j]表示矩阵的第i行和第j列(1≤i,j≤N)( ...
随机推荐
- web从入门开始(5)-----表单
1. 表单的概念 是用来获取客户端用户数据的(信息)的.如:注册表单,查询表单,登录表单等. 2. 表单的工作原理 1.浏览有表单的网页,填写一些必要的信息,然后单击某个按钮,进行提交. 2.这 ...
- Servlet+jsp的分页案例
查询的分页,在web中经常用到.一般,分页要维护的信息很多,我们把这些相关的信息,分装到一个类中,PageBean.具体如下: package cn.itcast.utils; import java ...
- HNOI(湖南省选试题)——永无乡
今天写了一道十分巧妙的数据结构题---永无乡 (看的题解......) 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 ...
- Linux云自动化运维第四课
Linux云自动化运维第四课 一.vim 1.vim光标移动 1)在命令模式下 :数字 ###移动到指定的行 G ###文件最后一行 gg ###文件第一行 2)在插入模式下 i ###光标所 ...
- nvm安装和配置详细教程
nvm是nodejs的版本管理工具,为什么要用nvm,你能百度到这篇文章相比是遇到不得不用的原因了,我们知道nodejs官方更新的速度非常快,有时候业务需要需要用某某版本,如果用的是msi安装,虽然安 ...
- 转:js,jQuery 排序的实现,网页标签排序的实现,标签排序
js,jQuery 排序的实现: 重点: 想要实现排序,最简单的方法就是 先把标签用jQuery读进对象数组 用js排序好对象数组 (针对对象数组进行排序, 不要试图直接对网页的内容进行直接更改) 用 ...
- Vue.use自定义自己的全局组件
通常我们在vue里面使用别人开发的组件,第一步就是install,第二步在main.js里面引入,第三步Vue.use这个组件.今天我简单的也来use一个自己的组件. 这里我用的webpack-sim ...
- 【转】JS判断SWF,JPG加载完毕、兼容(Activex,plugIn)所有浏览器
JS判断SWF,JPG加载完毕.兼容(Activex,plugIn)所有浏览器 这里主要说下监听SWF的加载. 网上流传已久的监听方法,只能在IE(Activex插件下)下实现.在使用plugin的浏 ...
- Java与面向对象之随感(1)
大一下学期上完了c++课程,当时自我感觉很良好,认为对面向对象编程已经是身经百战了,但是上了院里HuangYu老师的Java课之后,才发现自己对于面向对象的编程风格的理解只在皮毛,着实惭愧不已. 假设 ...
- javascript数组常用方法详解
1,splice(). array.splice(index,many,list1,list2....) 参数1.index位置 负数为从结尾处算,倒数第一为-1:参数2,many要删除的项目, ...