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的更多相关文章

  1. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  2. POJ-2155 Matrix---二维树状数组+区域更新单点查询

    题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...

  3. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  4. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  5. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

  6. poj2155 树状数组 Matrix

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14826   Accepted: 5583 Descripti ...

  7. 【POJ2155】【二维树状数组】Matrix

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  8. POJ2155:Matrix(二维树状数组,经典)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  9. 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 ...

随机推荐

  1. 【转】如何解决每次打开office2010都会出现正在配置以及使用KMS

    转自:http://jingyan.baidu.com/article/90895e0fb1525964ec6b0bb5.html 一.使用mini-KMS_Activator_v1.2_Office ...

  2. MySQL触发器写法

    触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/dele ...

  3. BZOJ1875: [SDOI2009]HH去散步 图上边矩乘

    这道题十分的坑…… 我作为一只连矩乘都不太会的渣渣看到这道题就只能神搜了….. 首先说一下普通的矩乘求方案,就是高出邻接矩阵然后一顿快速幂….. 矩乘一般就是一些秘制递推….. 再说一下这道题,我们可 ...

  4. git上传本地项目

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...

  5. ios上传图片显示方向错误问题

    IOS 上传图片方向显示错误问题 问题描述 在使用苹果手机上传图片的时候,发现传完的图片显示出来方向是错误的,竖着的图片会变成横着显示(少部分安卓手机也存在这个问题) 产生原因 ios 相机加入了方向 ...

  6. jquery.cookie.js 的使用指南

    转自:http://www.cnblogs.com/yjzhu/p/4359420.html 介绍: jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 coo ...

  7. (转)tableview的索引设置

    .感觉tableview的索引条将表视图往左边挤了一点?别担心,只是颜色问题.只要如此设置即可 //索引条背景的颜色(清空颜色就不会感觉索引条将tableview往左边挤) [_tableView s ...

  8. kettle基础操作

    ETL:抽取(extract).转换(transform).加载(load)至目的端的过程: Kettle是ETL工具代表之一,是pentaho中的一个数据整合的一个组件.Kettle里包括多个Job ...

  9. openstack中region、az、host aggregate、cell 概念

    1. region 更像是一个地理上的概念,每个region有自己独立的endpoint,regions之间完全隔离,但是多个regions之间共享同一个keystone和dashboard.(注:目 ...

  10. mysql五-2:多表查询

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 company.employeecompany.department #建表 create table department( id ...