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< ...
随机推荐
- 本地jar在打包时打入到项目中去
<dependency> <groupId>com.hxyc</groupId> <artifactId>hxyc-common</artifac ...
- 【快学springboot】在springboot中写单元测试[Happyjava]
前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...
- vue iviem UI grid布局
Grid 栅格 概述 我们采用了24栅格系统,将区域进行24等分,这样可以轻松应对大部分布局问题.使用栅格系统进行网页布局,可以使页面排版美观.舒适. 我们定义了两个概念,行row和列col,具体使用 ...
- python join 和setDaemon 简介
Python多线程编程时,经常会用到join()和setDaemon()方法 1.join ()方法:主线程A中,创建了子线程B,并且在主线程A中调用了B.join(),那么,主线程A会在调用的地方等 ...
- VUE框架下安装自带http协议
在控制台CMD 中输入 npm install vue-resource --save-dev
- java#tostring
通常使用apache-commons 来生成tostring方法,但是对于类型为java.util.Date的字段打印效果并不是我们想要的. @Override public String toStr ...
- IOS导航器 + 表控制器 常用功能函数/属性
1. 设置标题栏(顶部)颜色 在表控制器中 e.g -(void)viewDidLoad中添加 self.navigationController.navigationBar.barTintColor ...
- Lesson 46 Hobbies
Who, according to the authour, are 'Fortune's favoured children'? A gifted American psychologist has ...
- jdk环境变量、maven环境变量、Mysql环境变量配置
jdk官网地址:http://www.oracle.com/index.htmlhttp://www.java.sun.com 一.配置 jdk环境变量1.新建JAVA_HOME,在变量值复制JDK安 ...
- iOS中html打开APP传参
1.在项目info.plist中添加URL Types以供html调用 2.html代码 <html> <head lang="en"> <meta ...