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 ...
随机推荐
- [bzoj5321] [Jxoi2017]加法
Description 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个 ...
- [CF895C]Square Subsets
题目大意:给一个集合$S$($1\leq S_i\leq 70$),选择一个非空子集,使它们的乘积等于某个整数的平方的方法的数量. 求方案数,若两种方法选择的元素的索引不同,则认为是不同的方法. 题解 ...
- [Leetcode] text justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- BS架构下使用消息队列的工作流程
异步通信 对于BS(Browser-Server 浏览器)架构,很多情景下server的处理时间较长. 如果浏览器发送请求后,保持跟server的连接,等待server响应,那么一方面会对用户的体验有 ...
- linux查看操作系统是多少位
有三种方法: 1.echo $HOSTTYPE 2.getconf LONG_BIT,此处不应该是getconf WORD_BIT命令,在64位系统中显示的是32 3.uname -a 出现" ...
- (转)tableview的索引设置
.感觉tableview的索引条将表视图往左边挤了一点?别担心,只是颜色问题.只要如此设置即可 //索引条背景的颜色(清空颜色就不会感觉索引条将tableview往左边挤) [_tableView s ...
- HDU2057
http://acm.hdu.edu.cn/showproblem.php?pid=2057 涉及到16进制内的加法,可以用%I64x直接来处理,要注意到16进制中负数是用补码来表示的.一个比较困惑的 ...
- [bzoj1798][Ahoi2009]Seq——线段树+多重标记下传
题意 请你写一个数据结构,支持: 子序列同加 子序列同乘 统计子序列和 题目 线段树裸题,但对于我这种初学者还是非常难写. 我们维护两个标记,一个是在这个节点上作过的所有乘法操作,一个是加法操作,始终 ...
- Python阶段复习 - part 2 - Python序列/持久化
1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 >>> import json >>> imp ...