Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17226   Accepted: 6461

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

这题二维线段树也可以做。

二维线段树需要  给一个矩形加一个值

查询单个的值。

加值的时候直接加一个块。

查询的时候把这个点以及和这个点相关的都累加起来。

数据结构写法多样啊,重在理解

 /* ***********************************************
Author :kuangbin
Created Time :2014/5/23 23:08:19
File Name :E:\2014ACM\专题学习\数据结构\二维线段树\POJ2155.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
struct Nodey
{
int l,r;
int val;
};
int n;
int locx[MAXN],locy[MAXN];
struct Nodex
{
int l,r;
Nodey sty[MAXN*];
void build(int i,int _l,int _r)
{
sty[i].l = _l;
sty[i].r = _r;
sty[i].val = ;
if(_l == _r)
{
locy[_l] = i;
return;
}
int mid = (_l + _r)>>;
build(i<<,_l,mid);
build((i<<)|,mid+,_r);
}
void add(int i,int _l,int _r,int val)
{
if(sty[i].l == _l && sty[i].r == _r)
{
sty[i].val += val;
return;
}
int mid = (sty[i].l + sty[i].r)>>;
if(_r <= mid)add(i<<,_l,_r,val);
else if(_l > mid)add((i<<)|,_l,_r,val);
else
{
add(i<<,_l,mid,val);
add((i<<)|,mid+,_r,val);
}
}
}stx[MAXN*];
void build(int i,int l,int r)
{
stx[i].l = l;
stx[i].r = r;
stx[i].build(,,n);
if(l == r)
{
locx[l] = i;
return;
}
int mid = (l+r)>>;
build(i<<,l,mid);
build((i<<)|,mid+,r);
}
void add(int i,int x1,int x2,int y1,int y2,int val)
{
if(stx[i].l == x1 && stx[i].r == x2)
{
stx[i].add(,y1,y2,val);
return;
}
int mid = (stx[i].l + stx[i].r)/;
if(x2 <= mid)add(i<<,x1,x2,y1,y2,val);
else if(x1 > mid)add((i<<)|,x1,x2,y1,y2,val);
else
{
add(i<<,x1,mid,y1,y2,val);
add((i<<)|,mid+,x2,y1,y2,val);
}
}
int sum(int x,int y)
{
int ret = ;
for(int i = locx[x];i;i >>= )
for(int j = locy[y];j;j >>= )
ret += stx[i].sty[j].val;
return ret;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
int q;
scanf("%d%d",&n,&q);
build(,,n);
char op[];
int x1,x2,y1,y2;
while(q--)
{
scanf("%s",op);
if(op[] == 'C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add(,x1,x2,y1,y2,);
}
else
{
scanf("%d%d",&x1,&y1);
if(sum(x1,y1)% == )printf("0\n");
else printf("1\n");
}
}
if(T)printf("\n");
}
return ;
}

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. poj 2155:Matrix(二维线段树,矩阵取反,好题)

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

随机推荐

  1. 线段树(segment tree)

    线段树在一些acm题目中经常见到,这种数据结构主要应用在计算几何和地理信息系统中.下图就为一个线段树: (PS:可能你见过线段树的不同表示方式,但是都大同小异,根据自己的需要来建就行.) 1.线段树基 ...

  2. zend guard6的使用

    1.生成key edit->preferences->license Keys->generate 2.新建product license文件 3.新建Zend Guard项目文件 ...

  3. ArcGIS+API+for+JS测距

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 基于MVC4+EasyUI的Web开发框架形成之旅--基类控制器CRUD的操作

    在上一篇随笔中,我对Web开发框架的总体界面进行了介绍,其中并提到了我的<Web开发框架>的控制器的设计关系,Web开发框架沿用了我的<Winform开发框架>的很多架构设计思 ...

  5. RestEasy 3.x 系列之一:Hello world

    RestEasy 3.x改了不少,走了好多弯路才终于搞出来,做做笔记,陆续发布…… tomcat-7.0.50 java version "1.7.0_51" myeclipse ...

  6. python string

    string比较连接 >>> s1="python string" >>> len(s) 13 >>> s2=" p ...

  7. 禁用sqlserver的锁升级

    锁升级 SQLSERVER.DB2中的锁是内存里面实现的,这就有个资源消耗问题,当锁的数量达到一个阀值或内存有压力时,就会引发锁升级.实际的情况是从row lock直接升级到table lock,而不 ...

  8. Oracle调整联机日志大小

    近期一个项目反馈月底高峰期时系统整体性能下降,抓取对应的AWR和ASH查看,等待事件排第一位的竟然是redo日志切换.进一步看每秒的日志量是5M多,而日志文件大小仅有200M.建议项目上调整日志大小, ...

  9. java对象序列化byte[] and byte[]反序列化对象--转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  10. JAVA虚拟机类型转换学习

    Java虚拟机包括血多进行基本类型转换工作的操作码,这些执行转换工作的操作码后面没有操作数,转换的值从栈顶端获得.Java虚拟机从栈顶端弹出一个值,对它进行转换,然后再把转换结果压入栈.进行int.l ...