poj 2155:Matrix(二维线段树,矩阵取反,好题)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17880 | Accepted: 6709 |
Description
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 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
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

- #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(二维线段树,矩阵取反,好题)的更多相关文章
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- poj 2155 matrix 二维线段树
题目链接 区间翻转, 单点查询, 查询操作我真是不太明白...... #include <iostream> #include <vector> #include <cs ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- POJ2155 Matrix 二维线段树
关键词:线段树 二维线段树维护一个 维护一个X线段的线段树,每个X节点维护一个 维护一个Y线段的线段树. 注意,以下代码没有PushDownX.因为如果要这么做,PushDownX时,由于当前X节点的 ...
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- poj 2155 Matrix (二维树状数组)
题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...
- POJ 2155 Matrix(二维BIT)
Matrix [题目链接]Matrix [题目类型]二维BIT &题解: bit只能单点更新,恰好,这题可以想一下就可以用单点更新解决了. 只不过最后我交上去居然T了,想了10多分钟,试了一下 ...
- POJ 2019 Cornfields 二维线段树的初始化与最值查询
模板到不行.. 连更新都没有.. .存个模板. 理解留到小结的时候再写. #include <algorithm> #include <iostream> #include & ...
- CodeForces 242E二维线段树
E. XOR on Seg ...
随机推荐
- 14 BasicHashTable基本哈希表类(一)——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- PHP使用curl替代file_get_contents
初学php的朋友们,很容易翻一个错误,在写采集程序或者调用api接口总会有线考虑到使用file_get_contents函数来或许内容,程序的访问量不大倒是没什么影响,但是访问量提升了那非常的悲剧了, ...
- lol 正在刷leetcode
letcode easy 刷了90%了 我要写个随笔庆祝下 挑着做的太不要脸了,接下来要做剩下的了 :) 剩下的决定直接参考答案了 :) 有些答案看着也好迷糊.水平太差了.(英文水平差,看不懂题目.. ...
- MDI窗体
1.设置父窗体 使用MDI窗体,需要先将父窗体的IsMdiContainer属性设置为True 2.生成用于MDI子窗体的窗体 1 frmTemp f1 = new frmTemp(); f1.Tex ...
- CEF3开发者系列之工程和代码结构
CEF支持一系列的编程语言和操作系统,并且能很容易地整合到新的或已有的工程中去.它的设计思想就是易用且兼顾性能. CEF3支持一系列的编程语言和操作系统,并且能很容易地整合到新的或已有的工程中去.它的 ...
- windows8 安装TortoiseSVN后的反应
因为工作需要,昨天安装了TortoiseSVN 64位版,没有马上重启. 随后,IIS中打开页面后浏览器一片空白,没有网页,没有地址,什么都没有.这时还没想到可能是TortoiseSVN的问题.后来实 ...
- Effective C++ -----条款09:绝不在构造和析构过程中调用virtual函数
在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层).
- 前端js模版 预编译工具Tmod js使用入门
1. 安装node js , 2. 用 npm install -g tmodjs 命令安装tmod 3.了解参数配置 4.运行测试例子->命令窗切换到当前文档位置 --->执行tomd ...
- xcode报错,svn : is not a workingCopy
解决方案: 1.点击对应的targets 2.选择build phases 3.在红框处有一个run script选项(截图中已经删除了.),点击下拉按钮,看看是否是与svn有关的东西, 如果是,删除 ...
- win7自动登录桌面
control userpasswords2 在开始菜单中搜索“运行”,回车打开,或者Win+R打开运行窗口. 键入“control userpasswords2”或者“rundll32 netplw ...