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节点的 ...
随机推荐
- JPA、SpringData JPA 、Hibernate和Mybatis 的区别和联系
一.JPA 概述 1. Java Persistence API(Java 持久层 API):用于对象持久化的 API 2. 作用:使得应用程序以统一的方式访问持久层 3. 前言中提到了 Hibern ...
- springboot集成mybatis及mybatis generator工具使用
原文链接 前言mybatis是一个半自动化的orm框架,所谓半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernat ...
- SSH Secure Shell Client--- the host may be dow
the host may be down,or there may be a problem with the network connection. Sometimes such problems ...
- python-day20--正则表达式与re模块
1.通过re模块可以做一些关于正则的相关操作 2.正则表达式:做字符串匹配的规则 1)字符组:在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[ ]表示 [0-9][a-f][A-F] ...
- httpclient妙用一 httpclient作为客户端调用soap webservice(转)
前面有一篇使用HttpClient调用带参数的post接口方法,这里找到一篇使用HttpClient调用Soap协议接口的方式. 原文地址:httpclient妙用一 httpclient作为客户端调 ...
- 以DefaultFuture为例,看类变量初始化顺序
https://stackoverflow.com/questions/8517121/java-what-is-the-difference-between-init-and-clinit# < ...
- delete symlink in subversion using svn delete command
# svn delete etc/systemd/system/getty.target.wants/serial-getty@ttyS3.service@ D etc/systemd ...
- python3 scrapy 爬取腾讯招聘
安装scrapy不再赘述, 在控制台中输入scrapy startproject tencent 创建爬虫项目名字为 tencent 接着cd tencent 用pycharm打开tencent项目 ...
- java中数组是不是对象?
[转自知乎]:http://www.zhihu.com/question/26297216 JAVA中的数组是对象吗? public class test { public static void m ...
- Django(四)模板文件中的循环
编辑views.py from django.shortcuts import render from django.shortcuts import HttpResponse #此行增加 # Cre ...