Matrix

Time Limit: 3000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2155
64-bit integer IO format: %lld      Java class name: Main

 
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

 
解题:二维线段树,第一次玩这鬼玩意,调试了N久。。。挫。。。。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct subtree{
int lt,rt;
bool val;
};
struct node{
int lt,rt;
subtree sb[maxn<<];
};
node tree[maxn<<];
int n,m,ans;
void sub(int lt,int rt,int v,subtree *sb){
sb[v].lt = lt;
sb[v].rt = rt;
sb[v].val = false;
if(lt == rt) return;
int mid = (lt + rt)>>;
sub(lt,mid,v<<,sb);
sub(mid+,rt,v<<|,sb);
}
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
sub(,n,,tree[v].sb);
if(lt == rt) return;
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
}
void subupdate(int lt,int rt,int v,subtree *sb){
if(sb[v].lt >= lt && sb[v].rt <= rt){
sb[v].val ^= ;
return;
}
if(lt <= tree[v<<].rt) subupdate(lt,rt,v<<,sb);
if(rt >= tree[v<<|].lt) subupdate(lt,rt,v<<|,sb);
}
void update(int lt,int rt,int y1,int y2,int v){
if(tree[v].lt >= lt && tree[v].rt <= rt){
subupdate(y1,y2,,tree[v].sb);
return;
}
if(lt <= tree[v<<].rt) update(lt,rt,y1,y2,v<<);
if(rt >= tree[v<<|].lt) update(lt,rt,y1,y2,v<<|); }
void subquery(int y,int v,subtree *sb){
ans ^= sb[v].val;
if(sb[v].lt == sb[v].rt) return;
if(y <= sb[v<<].rt) subquery(y,v<<,sb);
else subquery(y,v<<|,sb);
}
void query(int x,int y,int v){
subquery(y,,tree[v].sb);
if(tree[v].lt == tree[v].rt) return;
if(x <= tree[v<<].rt) query(x,y,v<<);
else query(x,y,v<<|);
}
int main(){
int t,x1,y1,x2,y2;
char s[];
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
build(,n,);
for(int i = ; i < m; ++i){
scanf("%s",s);
if(s[] == 'Q'){
ans = ;
scanf("%d %d",&x1,&y1);
query(x1,y1,);
printf("%d\n",ans);
}else if(s[] == 'C'){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
update(x1,x2,y1,y2,);
}
}
puts("");
}
return ;
}

HDU 2155 Matrix的更多相关文章

  1. POJ 2155 Matrix (D区段树)

    http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 1 ...

  2. POJ poj 2155 Matrix

    题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...

  3. POJ 2155 Matrix【二维树状数组+YY(区间计数)】

    题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  4. HDU 4920 Matrix multiplication(bitset)

    HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...

  5. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  6. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

  7. hdu 5569 matrix dp

    matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5569 D ...

  8. hdu 2119 Matrix(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2119 Matrix Time Limit: 5000/1000 MS (Java/Others)    ...

  9. HDU 5671 Matrix 水题

    Matrix 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5671 Description There is a matrix M that has ...

随机推荐

  1. solrj 操作 solr 集群版

    一.添加 @Test public void testAddDocument() throws Exception{ //创建一个集群的连接,应该使用 CloudSolrServer,//zkHost ...

  2. 设计模式之二十:责任链模式(Chain of Responsibility)

    感觉这个设计模式和组合模式一样是一种非常巧妙的设计模式,在须要使用它的地方假设不使用这样的设计模式代码会变的非常复杂,可是这样的设计模式的基本原理又是非常easy的. 责任链模式: 通过使多个对象都有 ...

  3. POJ 1284

    想了很久,只想到枚举的方法,估计会超时吧. 原来有这样一条性质:p为素数,则p有phi(p-1)个原根 Orz... #include <iostream> #include <cs ...

  4. rails Installer之后的调整rails.bat等文件

    rails Installer之后的调整rails.bat文件 出现系统找不到指定路径 学习了:http://www.jianshu.com/p/065355a731ee 修改rails.bat为 @ ...

  5. 使用Swift和SpriteKit写一个忍者游戏

    这篇文章的游戏使用SpriteKit和Swift语言来完毕. SpriteKit是苹果自己的游戏引擎,更能贴合iOS系统底层的API,只是架构和实现上都是模仿了Cocos2D.所以使用上事实上区别不大 ...

  6. CCDirector导演类

    CCDirector类是Cocos2D-x游戏引擎的核心.它用来创建而且控制着屏幕的显示,同一时候控制场景的显示时间和显示方式. 在整个游戏里一般仅仅有一个导演.游戏的開始.结束.暂停都会调用CCDi ...

  7. [C#] 怎样分析stackoverflow等clr错误

    有时候由于无限递归调用等代码错误,w3wp.exe会报错退出.原因是clr.exe出错了. 这样的错误比較难分析,由于C#代码抓不住StackOverflowException等异常. 处理方法是:生 ...

  8. Visual studio 编译时copy文件、文件夹

    项目属性--生成事件 添加命令 xcopy /y /d "$(SolutionDir)Dll\Linphone\*.dll" "$(TargetDir)\Linphone ...

  9. maven冲突管理及依赖管理实践

    1.“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突 Maven采用“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突,即如果一个项目最终 ...

  10. USACO 2.1 Hamming Codes

    Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...