POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询
本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样,又不好打。。。也可能是我这是在套用一维线段树的思想,还有更好的二维线段树懒惰标记方法
反正到现在我还没搞定二维线段树的懒惰标记,因为这道题不用懒惰标记,因为是二进制序列,区间修改仅限于翻转操作,那就只要记录每次操作,最后查询的时候从上往下把所有修改都来上一遍,就可以了。就类似于树状数组的第二种用法,每次区间修改,最后查询的时候把前面的修改都给加起来。
即区间修改的时候,定位到某段某区间列进行标记,单独查询的时候,针对每个行段的该列所在区间都遍历一遍,把所有的修改都遍历到之后就是结果了
#include <iostream>
#include <cstdio>
#include <cstring>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int N=1010;
int d[N*3][N*3];
int flag[N*3][N*3];
int n,Q;
void buildc(int k,int rt,int l,int r)
{
d[k][rt]=0;
if (l>=r){
return;
}
int mid=(l+r)>>1;
buildc(k,lson);
buildc(k,rson);
}
void buildr(int rt,int l,int r)
{
buildc(rt,1,1,n);
if (l>=r){
return;
}
int mid=(l+r)>>1;
buildr(lson);
buildr(rson);
} void fixc(int k,int c1,int c2,int rt,int l,int r)
{
if (c1<=l && r<=c2){
d[k][rt]^=1;
return;
}
int mid=(l+r)>>1;
if (c1>mid) fixc(k,c1,c2,rson);
else
if (c2<=mid) fixc(k,c1,c2,lson);
else{
fixc(k,c1,c2,lson);
fixc(k,c1,c2,rson);
}
}
void fixr(int r1,int r2,int c1,int c2,int rt,int l,int r)
{
if (r1<=l && r<=r2){
fixc(rt,c1,c2,1,1,n);
return;
}
int mid=(l+r)>>1;
if (r1>mid) fixr(r1,r2,c1,c2,rson);
else
if (r2<=mid) fixr(r1,r2,c1,c2,lson);
else{
fixr(r1,r2,c1,c2,lson);
fixr(r1,r2,c1,c2,rson);
}
}
void queryc(int& ans,int k,int C,int rt,int l,int r)
{
if (d[k][rt]){
ans^=1;
}
if (l>=r){
return ;
}
int mid=(l+r)>>1;
if (mid>=C)
queryc(ans,k,C,lson);
else
queryc(ans,k,C,rson);
}
void queryr(int& ans,int R,int C,int rt,int l,int r)
{
queryc(ans,rt,C,1,1,n);
if (l>=r){
return;
}
int mid=(l+r)>>1;
if (R<=mid) queryr(ans,R,C,lson);
else
queryr(ans,R,C,rson);
}
int main()
{
char ch[5];
int a,b,c,d;
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&Q);
buildr(1,1,n);
while (Q--)
{
scanf("%s",ch);
if (ch[0]=='C'){
scanf("%d%d%d%d",&a,&b,&c,&d);
fixr(a,c,b,d,1,1,n);
}
else {
scanf("%d%d",&a,&b);
int ans=0;
queryr(ans,a,b,1,1,n);
printf("%d\n",ans);
}
}
if (t) puts("");
}
return 0;
}
POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询的更多相关文章
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- POJ 2155 2维线段树 || 2维BIT
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
- POJ 1195 2维线段树(树套树实现) 树状数组
1: #include <stdio.h> 2: #include <string.h> 3: #include <stdlib.h> 4: #include &l ...
- BZOJ4785 ZJOI2017树状数组(概率+二维线段树)
可以发现这个写挂的树状数组求的是后缀和.find(r)-find(l-1)在模2意义下实际上查询的是l-1~r-1的和,而本来要查询的是l~r的和.也就是说,若结果正确,则a[l-1]=a[r](mo ...
- 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.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- POJ 2155 Matrix【二维线段树】
题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...
随机推荐
- MacBook OSX VMWare Fusion 11安装 Tools For Windows
需要加载对应客户机系统的安装文件,可以在/Applications/VMware\ Fusion.app/Contents/Library/isoimages文件夹下找到: 设置虚拟机的光驱: 在虚拟 ...
- 还在用逆向工程?太Low了,试试通用Mapper吧!
什么是通用Mapper? 通用mapper 可以极大的方便开发人员进行ORM,提供极其方便的单表增删改查. 什么是通用mapper,一句话简单说,它就是个辅助mybatis极简单表开发的组件.它不是为 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:pig简介
实验目的 了解pig的该概念和原理 了解pig的思想和用途 了解pig与hadoop的关系 实验原理 1.Pig 相比Java的MapReduce API,Pig为大型数据集的处理提供了更高层次的抽象 ...
- Day2-G-Sticks-POJ1011
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- NSString 常见数据类型转换:转NSInteger , NSDate(互转)
1. NSString转NSInteger, 转int (float, double类似 ) 1.1正常情况 , NSString所包含内容确能转化为int的类型 NSString *sNumber ...
- 【LOJ2513】「BJOI2018」治疗之雨
题意 你现在有 \(m+1\) 个数:第一个为 \(p\) ,最小值为 \(0\) ,最大值为 \(n\) :剩下 \(m\) 个都是无穷,没有最小值或最大值.你可以进行任意多轮操作,每轮操作如下: ...
- ie brower 点击用默认浏览器打开链接
<script> function GetCurrentJumpUrl(){ var eleLink = document.getElementById('adLink'); if(ele ...
- 七 联系人与客户多对一配置&联系人列表&分页查询联系人
联系人管理: 联系人实体类: package com.mycrm.domain; /** * 联系人的实体 * @author jt *CREATE TABLE `cst_linkman` ( `lk ...
- 关于页面跳转之后获取不到session数据的问题
暂时的解决方法有两种,亲测有效: 方法一: 将页面跳转方式由a标签改为请求转发request.getRequestDispatcher("stu_list.jsp").forwa ...
- HiBench成长笔记——(9) Centos安装Maven
Maven的下载地址是:http://maven.apache.org/download.cgi 安装Maven非常简单,只需要将下载的压缩文件解压就可以了. cd /home/cf/app wget ...