POJ2155 Matrix
分析
好博客
区间修改,单点查询的题,可以用经典的树状数组的转化,把它化为单点修改,区间查询。
方法是在一些点上加1,最后查询单点的前缀和模2即为答案。相当于维护的是一个异或差分,利用了容斥。
可对查询的前缀矩阵对翻转矩阵的覆盖情况分类讨论须要在哪些点上加1。
最后发现若翻转子矩阵\((x_1,y_1)\rightarrow(x_2,y_2)\),只须在\((x_1,y_1),(x_1,y_2+1),(x_2+1,y_1),(x_2+1,y_2+1)\)这些点上加1,即可满足我们的需要。
代码
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
const int MAXN=1123;
int a[MAXN][MAXN],n;
int lowbit(int x)
{
return x&-x;
}
void add(int x,int y,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
a[i][j]+=v;
}
int sum(int x,int y)
{
int res=0;
for(int i=x;i;i-=lowbit(i))
for(int j=y;j;j-=lowbit(j))
res+=a[i][j];
return res;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int T;
read(T);
while(T--)
{
int m;
read(n);read(m);
memset(a,0,sizeof(a));
while(m--)
{
char opt[2];
scanf("%s",opt);
if(opt[0]=='C')
{
int x1,y1,x2,y2;
read(x1);read(y1);read(x2);read(y2);
add(x1,y1,1);
add(x1,y2+1,1);
add(x2+1,y1,1);
add(x2+1,y2+1,1);
}
else
{
int x,y;
read(x);read(y);
printf("%d\n",sum(x,y)%2);
}
}
if(T)
puts("");
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
Hint
多组数据一定要初始化数组。
POJ2155 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: 16950 Accepted: 6369 Descripti ...
- POJ2155 Matrix 【二维线段树】
题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...
- 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 Matrix 【二维树状数组】+【段更新点查询】
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17766 Accepted: 6674 Descripti ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- POJ2155 Matrix 二维线段树
关键词:线段树 二维线段树维护一个 维护一个X线段的线段树,每个X节点维护一个 维护一个Y线段的线段树. 注意,以下代码没有PushDownX.因为如果要这么做,PushDownX时,由于当前X节点的 ...
随机推荐
- 雷林鹏分享:C# 匿名方法
C# 匿名方法 我们已经提到过,委托是用于引用与其具有相同标签的方法.换句话说,您可以使用委托对象调用可由委托引用的方法. 匿名方法(Anonymous methods) 提供了一种传递代码块作为委托 ...
- 关于C和C++
最开始学的就是C和C++,但只是学过,根本就不知道怎么使用. 后来接触了Python和Perl才知道怎么将编程应用于实际需求当中,读取文件,存放到数据结构,处理,输出. 但脚本语言有其固有的缺点,不能 ...
- ps命令各个内容信息详解
USER 用户名 PID 进程ID(Process ID) %CPU 进程的cpu占用率 %MEM 进程的内存占用率 VSZ 进程所使用的虚存的大小(Vi ...
- 3-23Agile Web Development,3-24(chapter: 6)
第2章 Instant Gratification 复习 和 练习第一章,新建rails web页面. 重点: 知道了类,方法,实例变量 在rails是怎么用的. rails generate con ...
- CentOS 7 Install Gitlab CE
https://hostpresto.com/community/tutorials/how-to-install-and-setup-gitlab-on-centos-7/ http://linux ...
- 『cs231n』神经网络组件
- 使用opatch工具 打补丁Patch 21352635 -(Database Patch Set Update 11.2.0.4.8)
Patch 21352635 - Database Patch Set Update 11.2.0.4.8 一.OPatch工具检查及升级 OPatch工具包,在安装目录$ORACLE_HOME下,P ...
- Oracle性能诊断艺术-读书笔记
create table test0605 as select * from dba_objects; select t1.owner,t1.object_name,t1.object_id from ...
- python daal test
import os import sys from daal.algorithms import low_order_moments from daal.data_management import ...
- SQL触发器实例(下)
基本语法: Create Trigger [TriggerName] ON [TableName] FOR [Insert][,Delete][,Update] AS --触发器要执行的操作语句. G ...