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. [UVA1402]Robotic Sort;[SP2059]CERC07S - Robotic Sort([洛谷P3165][CQOI2014]排序机械臂;[洛谷P4402][Cerc2007]robotic sort 机械排序)

    题目大意:一串数字,使用如下方式排序: 先找到最小的数的位置$P_1$,将区间$[1,P_1]$反转,再找到第二小的数的位置$P_2$,将区间$[2,P_2]$反转,知道排序完成.输出每次操作的$P_ ...

  2. 洛谷 P2501 [HAOI2006]数字序列 解题报告

    P2501 [HAOI2006]数字序列 题目描述 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大. ...

  3. 【CF Round 434 A. k-rounding】

    Time limit per test1 second memory limit per test 256 megabytes input standard input output standard ...

  4. 【bzoj2038】[国家集训队2010]小Z的袜子 莫队

    莫队:就是一坨软软的有弹性的东西Duang~Duang~Duang~ 为了防止以左端点为第一关键字以右端点为第二关键字使右端点弹来弹去,所以让左端点所在块为关键字得到O(n1.5)的时间效率,至于分块 ...

  5. CentOS 7, 升级python到3.x

    By francis_hao    Apr 11,2017 使用源码安装方式 首先到官网https://www.python.org/downloads/source/ 下载python最新版本.当前 ...

  6. last_query_cost

    The total cost of the last compiled query as computed by the query optimizer. This is useful for com ...

  7. angular响应式编程

    1.响应式编程 例子import {Observable} from "rxjs/Observable"; Observable.from([1,2,3,4]) .filter(( ...

  8. 自建git服务器搭建使用记录

    git在push的时候出现insufficient permission for adding an object错误 //解决方法,在git库的目录下 //明明一开始创建user的时候有执行这个命令 ...

  9. Spring MVC框架下 从后台读取数据库并显示在前台页面【笔记自用 不推荐作为参考】

    1.书写jsp页面  people.jsp 1.设计显示格式以及内容显示 2.设计显示内容的范围 2.书写entity实体类 PeopleFormMap.java 书写传入的参数主要包括 要引用的数据 ...

  10. RPC-Thrift(三)

    TProtocol TProtocol定义了消息怎么进行序列化和反序列化的. TProtocol的类结构图如下: TBinaryProtocol:二进制编码格式: TCompactProtocol:高 ...