Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17880   Accepted: 6709

Description

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N).

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
2. Q x y (1 <= x, y <= n) querys A[x, y]. 

Input

The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case.

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above.

Output

For each querying output one line, which has an integer representing A[x, y].

There is a blank line between every two continuous test cases.

Sample Input

1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

Sample Output

1
0
0
1

Source

POJ Monthly,Lou Tiancheng

 
  二维线段树,矩阵取反,好题
 
  题意
  
  思路
  矩阵节点的值为是否取反,0为不取反,1为取反,暂称为取反值。
  取反操作的时候先找到这个矩阵代表的节点,然后将这个节点的值+1再模2,即取反。
  查询的时候,将(x,y)这个坐标经过的所有矩阵的取反值加起来,每次%2,最后那个值就为这个坐标最后的值。
  为什么%2,因为不是1就是0,矩阵记录了取反值,找这个坐标的过程中,经过的矩阵如果取反值为1,则结果变为0,在经过一个取反值为1的矩阵,结果又变为1…… 直到加到要找的坐标的取反值,这个结果记录的值就是这个坐标的取反值。这个时候输出结果。
 
  代码
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 1100 int tree[MAXN*][MAXN*],s; void Negate_y(int d,int dy,int L,int R,int y1,int y2) //取反操作
{
if(L==y1 && R==y2){ //将这个矩阵的所有元素记录为取反
tree[d][dy] = (tree[d][dy]+) % ;
return ;
} int mid = (L+R)>>;
if(mid>=y2)
Negate_y(d,dy<<,L,mid,y1,y2);
else if(mid<y1)
Negate_y(d,dy<<|,mid+,R,y1,y2);
else{
Negate_y(d,dy<<,L,mid,y1,mid);
Negate_y(d,dy<<|,mid+,R,mid+,y2);
}
} void Negate_x(int d,int L,int R,int x1,int y1,int x2,int y2) //取反操作
{
if(L==x1 && R==x2){ //找到行块
Negate_y(d,,,s,y1,y2);
return ;
} int mid = (L+R)>>;
if(mid>=x2)
Negate_x(d<<,L,mid,x1,y1,x2,y2);
else if(mid<x1)
Negate_x(d<<|,mid+,R,x1,y1,x2,y2);
else{
Negate_x(d<<,L,mid,x1,y1,mid,y2);
Negate_x(d<<|,mid+,R,mid+,y1,x2,y2);
}
} int Query_y(int d,int dy,int L,int R,int r) //查询
{
if(L==R) //找到要找的坐标,输出这个坐标对应的值
return tree[d][dy]; //没找到
int mid = (L+R)>>;
if(mid >= r)
return (Query_y(d,dy<<,L,mid,r)+tree[d][dy]) % ;
else
return (Query_y(d,dy<<|,mid+,R,r)+tree[d][dy]) % ;
} int Query_x(int d,int L,int R,int l,int r) //查询
{
if(L==R){ //找到要找的行块,继续查找列块
return Query_y(d,,,s,r);
} //没找到
int mid = (L+R)>>;
if(mid >= l)
return (Query_x(d<<,L,mid,l,r) + Query_y(d,,,s,r)) % ;
else
return (Query_x(d<<|,mid+,R,l,r) + Query_y(d,,,s,r)) % ;
} int main()
{
int X,T,x,y,x1,y1,x2,y2;
scanf("%d",&X);
while(X--){
memset(tree,,sizeof(tree));
scanf("%d%d",&s,&T);
while(T--){
char c[];
scanf("%s",c);
if(c[]=='C'){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
Negate_x(,,s,x1,y1,x2,y2);
}
else if(c[]=='Q'){
scanf("%d%d",&x,&y);
printf("%d\n",Query_x(,,s,x,y));
}
}
if(X!=)
printf("\n");
} return ;
}

Freecode : www.cnblogs.com/yym2013

poj 2155:Matrix(二维线段树,矩阵取反,好题)的更多相关文章

  1. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

  2. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  3. poj 2155 matrix 二维线段树

    题目链接 区间翻转, 单点查询, 查询操作我真是不太明白...... #include <iostream> #include <vector> #include <cs ...

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

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

  5. POJ2155 Matrix 二维线段树

    关键词:线段树 二维线段树维护一个 维护一个X线段的线段树,每个X节点维护一个 维护一个Y线段的线段树. 注意,以下代码没有PushDownX.因为如果要这么做,PushDownX时,由于当前X节点的 ...

  6. POJ 2155 Matrix(二维树状数组,绝对具体)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Descripti ...

  7. poj 2155 Matrix (二维树状数组)

    题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...

  8. POJ 2155 Matrix(二维BIT)

    Matrix [题目链接]Matrix [题目类型]二维BIT &题解: bit只能单点更新,恰好,这题可以想一下就可以用单点更新解决了. 只不过最后我交上去居然T了,想了10多分钟,试了一下 ...

  9. POJ 2019 Cornfields 二维线段树的初始化与最值查询

    模板到不行.. 连更新都没有.. .存个模板. 理解留到小结的时候再写. #include <algorithm> #include <iostream> #include & ...

  10. CodeForces 242E二维线段树

                                                                                           E. XOR on Seg ...

随机推荐

  1. BZOJ 4236: JOIOJI

    Description 给出一个字符串,只包含3个字母,询问最长的一个子串,3个字母出现次数相同. Sol map. 如果一个子串满足条件,那么它端点处的三个字母的个数两两差值都是一样的,直接存个状态 ...

  2. 3-python学习——变量

    变量是我所接触过的编程语言中都具有的一个概念,只是这个概念有的强有的弱罢了. 1.什么是python的变量 变量这个东西怎么解释呢?不怎么好说. 这么说吧,变量就相当于一个代名词,或者说是名字. 计算 ...

  3. django的分页--不全也未实现

    一.Django内置分页 Paginator 二.自定义分页 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输 ...

  4. 初识hibernate小案例

    使用hibernate前需要导入相关JAR包. 1.它可以接受词文法语言描述,并能产生识别这些语言的语句的程序 2.是一个Java的XML API,类似于jdom,用来读写XML文件的 3.支持注解配 ...

  5. xadmin 自定义视图在uwsgi部署时的一坑

    比如修改登录页的template,代码如下: xadmin.site.register(xadmin.views.LoginView, login_template="card_pool/s ...

  6. phpcms更换域名用户无法注册问题

    问题背景: 用户注册必须在后台开启phpsso,这个sso也就是单点登录了,之前做的站都没有带用户登录,也一直没注意,今天线下localhost用户登录注册都没有问题,可是移到线上测试却怎么都无法注册 ...

  7. Linux设置交换分区swap

    参考: http://www.vpser.net/opt/vps-add-swap.html https://www.zntec.cn/archives/vps-swap.html http://yz ...

  8. tcpdump for android L 5.x with pie support

    由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. 最近由于要测试在Android L上的运行情况发现,当运行该可执行文件时,报如下错误: error: only po ...

  9. OPCServer Modbus使用和配置

    一,安装KEPware.Enhanced.OPC.DDE.KEPServer.(PLC数据传送给KEPServer,开发的程序用OPCServer读KEPServer) 设置ip地址后面是指的plc站 ...

  10. 【python】下载远程内容到本地

    来源:http://www.jb51.net/article/42630.htm urllib模块 urlretrieve方法 urllib.urlretrieve(url[, filename[, ...