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 ...
随机推荐
- NS10.1 产品技术规范
NS10.1 产品技术规范 产品技术规范==================4层-7层流量管理 4层负载均衡(LB) 支持的协议TCP,UDP,FTP,HTTP,HTTPS,DNS(TC ...
- BZOJ 2820: YY的GCD | 数论
题目: 题解: http://hzwer.com/6142.html #include<cstdio> #include<algorithm> #define N 100000 ...
- NEYC 2017 自动取款机 atm Day6 T1
自动取款机 [问题描述] 小 ...
- TCP ------ TCP创建服务器中出现的套接字
在服务器端,socket()返回的套接字用于监听(listen)和接受(accept)客户端的连接请求.这个套接字不能用于与客户端之间发送和接收数据. accept()接受一个客户端的连接请求,并返回 ...
- Cannot read property 'resetFields' of undefined 问题及引申
问题描述: 使用element开发我的后台系统,编辑和新增使用了同一个弹出框<el-dialog><el-form></el-form></el-dialog ...
- Java类的声明和访问介绍
1.类的声明 类本身的声明:对类的声明来说,主要包括类的访问权限声明和非访问修饰符的使用.对于一个普通的Java类(POJO)来说,主要的访问权限修饰符只有两个public和默认权限,内部类可以有pr ...
- HASHMAP 深入解析
http://blog.csdn.net/ghsau/article/details/16843543/
- PCIe 调试
ISE 生成PCIe核之后, 在ipcore_dir目录下会产生以下文件目录 目录下包含内容如下: The doc folder contains the PCIe Endpoint Block da ...
- 转:js中javascript:void(0) 真正含义
from:http://www.jb51.net/article/71532.htm 在Javascript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. 我想使用过ajax的都常 ...
- yum软件包安装
使用yum安装软件 配置yum配置文件 cd /etc/yum.repos.d/ vim rhel7.repo [rhel7-source] name=rhel7-source baseurl=fil ...