POJ 2155 2维线段树 || 2维BIT
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#define mp make_pair
#define pa pair<int,int>
#define pb push_back
#define fi first
#define se second
using namespace std;
inline void Get_Int(int &X)
{
X=; char ch=getchar(); int f=;
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {X=X*+ch-''; ch=getchar();} X*=f;
}
inline void Put_Int(int X)
{
char ch[]; int top=;
if (X==) ch[++top]='';
while (X) ch[++top]=X%+'',X/=;
while (top) putchar(ch[top--]); putchar('\n');
}
//=======================================
const int Maxn=;
int x1,x2,y1,y2,Ans;
bool Key[Maxn][Maxn];
int n,Q,Kase;
void Modify_Y(int ox,int oy,int l,int r,int p,int q)
{
if (l==p && r==q)
{
Key[ox][oy]^=;
return;
}
int mid=(l+r)>>;
if (q<=mid) Modify_Y(ox,oy<<,l,mid,p,q);
if (p>=mid+) Modify_Y(ox,oy<<|,mid+,r,p,q);
if (p<=mid && q>=mid+) Modify_Y(ox,oy<<,l,mid,p,mid),Modify_Y(ox,oy<<|,mid+,r,mid+,q); }
void Modify_X(int ox,int l,int r,int p,int q)
{
if (l==p && r==q)
{
Modify_Y(ox,,,n,y1,y2);
return;
}
int mid=(l+r)>>;
if (q<=mid) Modify_X(ox<<,l,mid,p,q);
if (p>=mid+) Modify_X(ox<<|,mid+,r,p,q);
if (p<=mid && q>=mid+) Modify_X(ox<<,l,mid,p,mid),Modify_X(ox<<|,mid+,r,mid+,q);
} void Query_Y(int ox,int oy,int l,int r)
{
Ans^=Key[ox][oy];
if (l==r) return;
int mid=(l+r)>>;
if (y1<=mid) Query_Y(ox,oy<<,l,mid);
if (y1>=mid+) Query_Y(ox,oy<<|,mid+,r);
}
void Query_X(int ox,int l,int r)
{
Query_Y(ox,,,n);
if (l==r) return;
int mid=(l+r)>>;
if (x1<=mid) Query_X(ox<<,l,mid);
if (x1>=mid+) Query_X(ox<<|,mid+,r);
}
int main()
{
Get_Int(Kase);
for (int kase=;kase<=Kase;kase++)
{
Get_Int(n),Get_Int(Q);
memset(Key,false,sizeof(Key));
for (int i=;i<=Q;i++)
{
char ch=getchar(); while (ch!='C' && ch!='Q') ch=getchar();
if (ch=='C')
{
Get_Int(x1),Get_Int(y1),Get_Int(x2),Get_Int(y2);
Modify_X(,,n,x1,x2);
}
if (ch=='Q')
{
Get_Int(x1),Get_Int(y1);
Ans=;
Query_X(,,n);
Put_Int(Ans);
}
}
putchar('\n');
}
return ;
}
线段树
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int Maxn=;
int n,Q,Kase,c[Maxn][Maxn],x1,x2,y1,y2;
inline int lowbit(int x) {return x&(-x);}
inline void Modify(int x,int y)
{
for (int i=x;i<=n;i+=lowbit(i))
for (int j=y;j<=n;j+=lowbit(j)) c[i][j]^=;
}
inline int Query(int x,int y)
{
int ret=;
for (int i=x;i;i-=lowbit(i))
for (int j=y;j;j-=lowbit(j)) ret^=c[i][j];
return ret;
}
int main()
{
// freopen("c.in","r",stdin);
scanf("%d",&Kase);
for (int kase=;kase<=Kase;kase++)
{
scanf("%d%d",&n,&Q);
memset(c,,sizeof(c));
for (int i=;i<=Q;i++)
{
char ch=getchar(); while (ch!='C' && ch!='Q') ch=getchar();
if (ch=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Modify(x1,y1),Modify(x1,y2+),Modify(x2+,y1),Modify(x2+,y2+);
}
if (ch=='Q')
{
scanf("%d%d",&x1,&y1);
printf("%d\n",Query(x1,y1));
}
}
puts("");
}
return ;
}
BIT
POJ 2155 2维线段树 || 2维BIT的更多相关文章
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- Wannafly Winter Camp 2020 Day 5I Practice for KD Tree - 二维线段树
给定一个 \(n \times n\) 矩阵,先进行 \(m_1 \leq 5e4\) 次区间加,再进行 \(m_2 \leq 5e5\) 次询问,每次询问要求输出矩形区间内的最大数.\(n \leq ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- POJ 2155 Matrix【二维线段树】
题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...
- POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询
本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样, ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
随机推荐
- hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系
hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...
- ggplot2.multiplot:将多个图形使用GGPLOT2在同一页上
一页多图 介绍 ggplot2.multiplot是一个易于使用的功能,将多个图形在同一页面上使用R统计软件和GGPLOT2绘图方法.这个功能是从easyGgplot2包. 安装并加载easyGgpl ...
- 挂载光盘与rpm安装
光驱----光盘(系统光盘or资料) linux服务器上有光驱,也有光盘在里面,在系统那里去看内容 挂载,mount ls -l d--目录- 文件l 链接文件b 块设备文件 光驱文件的位置:/dev ...
- python 列表常用操作
例子: list1 = [1, 2, 3, 4, 5, 6, [6, 7, 8,9], 'hi', 'hello', 6] list2 = [7, 8, 1, 2] list3 = ['good', ...
- SQL语句中&、单引号等特殊符号的处理
今天遇到一个insert语句,在SQL Tools(链接Oracle数据库)插入的某列值为“Computer Hardware & Software>>CPU",这样执行 ...
- 从C语言快速学PHP
PHP是解释性语言,是Web开发中常用的语言.对于web编程,建议学习时参考w3cschool的在线api手册. PHP和C语言及其相似,懂C的人只要稍加学习就能写出简单的PHP程序.以下是PHP与C ...
- 深入浅出设计模式——迭代器模式(Iterator Pattern)
模式动机 一个聚合对象,如一个列表(List)或者一个集合(Set),应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构.针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我 ...
- vs2010打包(带数据库)图文详解
最近刚刚打包发布了用VS2010开发的一个收费系统,借此讲一讲打包过程,供大家参考. 首先打开已经完成的工程,如图: 下面开始制作安装程序包. 第一步:[文件]——[新建]——[项目]——安装项目. ...
- Delphi IDE 设置
显示编译进度 Tools/Environment Options
- 聊天界面之进度条cell(一)
ProgressCell用于显示文件传输的进度,困难点在于根据下载进度更新cell的进度条,先后尝试了几种方法: 1.有新的下载进度时,直接调用reloadData() 2.使用reloadRowsA ...