poj2155
poj2155
题意
二维区间更新,单点查询。
分析
二维线段树。
也可以用二维树状数组去做,维护矩阵前缀和。
code
#include<cstdio>
using namespace std;
typedef long long ll;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int MAXN = 4001;
int n, val[MAXN][MAXN];
int x, y, xl, xr, yl, yr;
void subBuild(int xrt, int l, int r, int rt) {
val[xrt][rt] = 0;
if(l != r) {
int m = l + r >> 1;
subBuild(xrt, lson);
subBuild(xrt, rson);
}
}
void build(int l, int r, int rt) {
subBuild(rt, 1, n, 1);
if(l != r) {
int m = l + r >> 1;
build(lson);
build(rson);
}
}
void subUpdate(int xrt, int l, int r, int rt) {
if(l >= yl && r <= yr) val[xrt][rt] ^= 1;
else {
int m = l + r >> 1;
if(yl <= m) subUpdate(xrt, lson);
if(yr > m) subUpdate(xrt, rson);
}
}
void update(int l, int r, int rt) {
if(l >= xl && r <= xr) subUpdate(rt, 1, n, 1);
else {
int m = l + r >> 1;
if(xl <= m) update(lson);
if(xr > m) update(rson);
}
}
int ans;
void subQuery(int xrt, int l, int r, int rt) {
ans ^= val[xrt][rt];
if(l != r) {
int m = l + r >> 1;
if(y <= m) subQuery(xrt, lson);
else subQuery(xrt, rson);
}
}
void query(int l, int r, int rt) {
subQuery(rt, 1, n, 1);
if(l != r) {
int m = l + r >> 1;
if(x <= m) query(lson);
else query(rson);
}
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
int q;
scanf("%d%d", &n, &q);
build(1, n, 1);
while(q--) {
char s[2];
scanf("%s", s);
if(s[0] == 'C') {
scanf("%d%d%d%d", &xl, &yl, &xr, &yr);
update(1, n, 1);
} else {
scanf("%d%d", &x, &y);
ans = 0;
query(1, n, 1);
printf("%d\n", ans);
}
}
if(T) puts("");
}
return 0;
}
poj2155的更多相关文章
- POJ2155 Matrix 【二维线段树】
题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...
- POJ-2155 Matrix---二维树状数组+区域更新单点查询
题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- poj2155 树状数组 Matrix
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14826 Accepted: 5583 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 ...
- POJ2155:Matrix(二维树状数组,经典)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- poj2155二维树状数组
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 ...
随机推荐
- 莫比乌斯反演题表II
bzoj3994:[SDOI2015]约数个数和 **很好推+有个小结论bzoj3309:DZY Loves Math ***很好推+线筛某函数/卡常bzoj4816:[Sdoi2017]数字表格 * ...
- SDOI 2009 学校食堂 状压dp
这个题的关键处1 紧跟着他的bi个人 —— 由此得出任意一个状态都可以表示为 有第一个人没吃到饭做分隔的前面所有人已吃饭,并用1<<8表示之后的(包括他)的八个人的状态2 信息仍然是上一个 ...
- strings用法小记
By francis_hao Feb 14,2017 打印文件中可打印字符,每个序列至少四(可配置)个字符长.主要用于显示非文本文件 概述 选项解释 -a --all - 扫描整个文件,不管 ...
- MySQL使用笔记(四)数据的操作
By francis_hao Dec 14,2016 数据的操作包括插入数据记录.更新数据记录和删除数据记录. 插入数据记录 插入单条数据记录 field表示的字段名和value表示数据要一一对 ...
- taotao用户注册前台页面
注册页面效果: 注册jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- MySQL:BlackHole
MySQL:BlackHole 顾名思义BlackHole就是黑洞,只有写入没有输出.现在就来实验一下吧 首先查看一下MySQL支持的存储引擎 mysql> show engines;+---- ...
- AWS nat monitor and route switch script
This script will monitor another NAT instance and take over its routes if communication with the oth ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- Spring--环境配置
目录 1.1 Spring jar包下载 1.2 Hello World 参考资料 1.1 Spring jar包下载 (1)进入官网http://repo.spring.io(或者 http://m ...
- 【Foreign】开锁 [概率DP]
开锁 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 ...