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 ...
随机推荐
- Android网络框架源码分析一---Volley
转载自 http://www.jianshu.com/p/9e17727f31a1?utm_campaign=maleskine&utm_content=note&utm_medium ...
- Linux常用命令(三)
1.top 说明:即时显示 process 的动态 语法格式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]基本参数:d : 改变显示的更新速度,或是在交谈 ...
- Android自定义View自定义属性
1.引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleabl ...
- R开发环境(Eclipse+StatET)
引用:http://cos.name/2008/12/eclipse-statet-for-r-editor/ StatET(www.walware.de/goto/statet) 1. 安装软件 s ...
- R----plotly包介绍学习
plotly包:让ggplot2的静态图片变得可交互 Plotly 是个交互式可视化的第三方库,官网提供了Python,R,Matlab,JavaScript,Excel的接口,因此我们可以很方便地在 ...
- Oracle求连续的年份
SELECT ('2013') + ROWNUM year FROM dualCONNECT BY ROWNUM <= 2;
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的连接恢复和命令拦截
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第四篇:MVC程序中实体框架的连接恢复和 ...
- jsp标签精华(持续更新中)
<%@ taglib uri="/struts-tags" prefix="s" %> <%@ taglib uri="http:/ ...
- 纯css3圆角下拉菜单 都没敢用js
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- RMAN的入门篇
一.RMAN连接数据库 1. 连接本地数据库 [oracle@oracle hotbak]$ export oracle_sid=orcl [oracle@oracle hotbak]$ rman ...